Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions .github/workflows/package-smoke.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: Package smoke

on: [push, pull_request]

jobs:
verify:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: ['20.x', '22.x', '24.x']
name: Package smoke (Node ${{ matrix.node-version }})
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: yarn
- run: yarn build
- name: Verify packaged exports
run: |
set -euo pipefail
TGZ=$(npm pack)
TMPDIR=$(mktemp -d)
pushd "$TMPDIR" >/dev/null
npm init -y >/dev/null
npm install --silent "$GITHUB_WORKSPACE/$TGZ" typescript >/dev/null
node - <<'NODE'
const pkg = require('tree-visit')
if (typeof pkg.visit !== 'function') {
throw new Error('Expected visit export in CJS bundle')
}
console.log('CJS consumer OK')
NODE
node --input-type=module - <<'NODE'
import * as mod from 'tree-visit'
if (typeof mod.visit !== 'function') {
throw new Error('Expected visit export in ESM bundle')
}
console.log('ESM consumer OK')
NODE
cat <<'TS' > smoke.ts
import type { VisitOptions } from 'tree-visit'
import { visit } from 'tree-visit'

type Node = { value: number; children?: Node[] }

const tree: Node = { value: 1, children: [{ value: 2 }] }

visit<Node>(
tree,
{
includeTraversalContext: true,
getChildren: (node) => node.children ?? [],
onEnter(node) {
console.log(node.value)
},
} satisfies VisitOptions<Node>
)
TS
npx tsc smoke.ts --moduleResolution nodenext --module nodenext --target es2020 --noEmit
popd >/dev/null
rm "$GITHUB_WORKSPACE/$TGZ"
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@
"testEnvironment": "node",
"testPathIgnorePatterns": [
"lib"
]
],
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.js$": "$1"
}
},
"exports": {
".": {
Expand Down
4 changes: 2 additions & 2 deletions src/access.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IndexPath } from './indexPath'
import { BaseOptions, TraversalContext } from './options'
import { IndexPath } from './indexPath.js'
import { BaseOptions, TraversalContext } from './options.js'

/**
* Returns a node by its `IndexPath`.
Expand Down
6 changes: 3 additions & 3 deletions src/ancestors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IndexPath } from './indexPath'
import { comparePathsByComponent } from './sort'
import { KeyPath } from './types'
import { IndexPath } from './indexPath.js'
import { comparePathsByComponent } from './sort.js'
import { KeyPath } from './types.js'

type AncestorPathsOptions<TPath extends KeyPath | IndexPath> = {
/**
Expand Down
45 changes: 24 additions & 21 deletions src/defineTree.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,57 @@
import { access, accessPath, ancestors, get } from './access'
import { diagram, DiagramOptions } from './diagram'
import { entries } from './entries'
import { access, accessPath, ancestors, get } from './access.js'
import { diagram, DiagramOptions } from './diagram.js'
import { entries } from './entries.js'
import {
find,
findAll,
findAllPaths,
FindOptions,
FindOptionsTyped,
findPath,
} from './find'
import { flat } from './flat'
import { IndexPath } from './indexPath'
} from './find.js'
import { flat } from './flat.js'
import { IndexPath } from './indexPath.js'
import {
insert,
InsertOptions,
insertWithPathTracking,
InsertWithPathTrackingOptions,
} from './insert'
import { flatMap, FlatMapOptions, map, MapOptions } from './map'
import { move, MoveOptions } from './move'
import { BaseOptions, MutationBaseOptions, TraversalContext } from './options'
import { reduce, ReduceOptions } from './reduce'
} from './insert.js'
import { flatMap, FlatMapOptions, map, MapOptions } from './map.js'
import { move, MoveOptions } from './move.js'
import {
BaseOptions,
MutationBaseOptions,
TraversalContext,
} from './options.js'
import { reduce, ReduceOptions } from './reduce.js'
import {
remove,
RemoveOptions,
removeWithPathTracking,
RemoveWithPathTrackingOptions,
} from './remove'
import { replace, ReplaceOptions } from './replace'
} from './remove.js'
import { replace, ReplaceOptions } from './replace.js'
import {
splice,
SpliceOptions,
spliceWithPathTracking,
SpliceWithPathTrackingOptions,
} from './splice'
import { ExtractRequiredKeys, OptionCheck, Prettify } from './types'
import { visit, VisitOptions } from './visit'
} from './splice.js'
import { OptionCheck, Prettify } from './types.js'
import { visit, VisitOptions } from './visit.js'

type WithoutBase<T> = Omit<T, keyof BaseOptions<T>>

type MutationOptions<T> = WithoutBase<MutationBaseOptions<T>>

type DiagramOptionsWB<T> = WithoutBase<DiagramOptions<T>>
type DiagramRequiredOptions<T> = Pick<
DiagramOptionsWB<T>,
ExtractRequiredKeys<DiagramOptionsWB<T>>
>
type DiagramRequiredOptions<T> = {
getLabel: DiagramOptionsWB<T>['getLabel']
}
type DiagramOptionalOptions<T> = Omit<
DiagramOptionsWB<T>,
ExtractRequiredKeys<DiagramOptionsWB<T>>
keyof DiagramRequiredOptions<T>
>
type FindOptionsWB<T> = WithoutBase<FindOptions<T>>
type VisitOptionsWB<T> = WithoutBase<VisitOptions<T>>
Expand Down
8 changes: 4 additions & 4 deletions src/diagram.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IndexPath } from './indexPath'
import { BaseOptions } from './options'
import { boxDiagram } from './diagram/boxDiagram'
import { directoryDiagram } from './diagram/directoryDiagram'
import { IndexPath } from './indexPath.js'
import { BaseOptions } from './options.js'
import { boxDiagram } from './diagram/boxDiagram.js'
import { directoryDiagram } from './diagram/directoryDiagram.js'

export type DiagramType = 'directory' | 'box'

Expand Down
4 changes: 2 additions & 2 deletions src/diagram/boxDiagram.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IndexPath } from '../indexPath'
import { DiagramOptions } from '../diagram'
import { IndexPath } from '../indexPath.js'
import { DiagramOptions } from '../diagram.js'

enum BoxDrawing {
TopLeft = '┌',
Expand Down
4 changes: 2 additions & 2 deletions src/diagram/directoryDiagram.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DiagramOptions } from '../diagram'
import { IndexPath } from '../indexPath'
import { DiagramOptions } from '../diagram.js'
import { IndexPath } from '../indexPath.js'

enum LinePrefix {
Child = `├── `,
Expand Down
6 changes: 3 additions & 3 deletions src/entries.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IndexPath } from './indexPath'
import { BaseOptions } from './options'
import { visit } from './visit'
import { IndexPath } from './indexPath.js'
import { BaseOptions } from './options.js'
import { visit } from './visit.js'

export function entries<T>(node: T, options: BaseOptions<T>): [IndexPath, T][] {
let result: [IndexPath, T][] = []
Expand Down
6 changes: 3 additions & 3 deletions src/find.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IndexPath } from './indexPath'
import { BaseOptions, TraversalDirection } from './options'
import { STOP, visit } from './visit'
import { IndexPath } from './indexPath.js'
import { BaseOptions, TraversalDirection } from './options.js'
import { STOP, visit } from './visit.js'

export type FindOptions<T> = BaseOptions<T> & {
/**
Expand Down
4 changes: 2 additions & 2 deletions src/flat.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseOptions } from './options'
import { reduce } from './reduce'
import { BaseOptions } from './options.js'
import { reduce } from './reduce.js'

/**
* Returns an array containing the root node and all of its descendants.
Expand Down
40 changes: 20 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
export * from './access'
export * from './ancestors'
export * from './defineTree'
export * from './diagram'
export * from './entries'
export * from './find'
export * from './flat'
export * from './indexPath'
export * from './insert'
export * from './map'
export * from './move'
export * from './options'
export * from './reduce'
export * from './remove'
export * from './replace'
export * from './sort'
export * from './splice'
export * from './transformPath'
export * from './visit'
export * from './withOptions'
export * from './access.js'
export * from './ancestors.js'
export * from './defineTree.js'
export * from './diagram.js'
export * from './entries.js'
export * from './find.js'
export * from './flat.js'
export * from './indexPath.js'
export * from './insert.js'
export * from './map.js'
export * from './move.js'
export * from './options.js'
export * from './reduce.js'
export * from './remove.js'
export * from './replace.js'
export * from './sort.js'
export * from './splice.js'
export * from './transformPath.js'
export * from './visit.js'
export * from './withOptions.js'
6 changes: 3 additions & 3 deletions src/insert.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { IndexPath } from './indexPath'
import { IndexPath } from './indexPath.js'
import {
applyOperations,
getInsertionOperations,
transformPathsByOperations,
} from './operation'
import { MutationBaseOptions } from './options'
} from './operation.js'
import { MutationBaseOptions } from './options.js'

export type InsertOptions<T> = MutationBaseOptions<T> & {
nodes: T[]
Expand Down
6 changes: 3 additions & 3 deletions src/map.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IndexPath } from './indexPath'
import { BaseOptions } from './options'
import { visit } from './visit'
import { IndexPath } from './indexPath.js'
import { BaseOptions } from './options.js'
import { visit } from './visit.js'

export type MapOptions<T, U> = BaseOptions<T> & {
/**
Expand Down
10 changes: 5 additions & 5 deletions src/move.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { access } from './access'
import { ancestorPaths } from './ancestors'
import { IndexPath } from './indexPath'
import { access } from './access.js'
import { ancestorPaths } from './ancestors.js'
import { IndexPath } from './indexPath.js'
import {
applyOperations,
getInsertionOperations,
getRemovalOperations,
} from './operation'
import { MutationBaseOptions } from './options'
} from './operation.js'
import { MutationBaseOptions } from './options.js'

export type MoveOptions<T> = MutationBaseOptions<T> & {
paths: IndexPath[]
Expand Down
10 changes: 5 additions & 5 deletions src/operation.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ancestorPaths } from './ancestors'
import { IndexPath } from './indexPath'
import { map } from './map'
import { MutationBaseOptions } from './options'
import { transformPath } from './transformPath'
import { ancestorPaths } from './ancestors.js'
import { IndexPath } from './indexPath.js'
import { map } from './map.js'
import { MutationBaseOptions } from './options.js'
import { transformPath } from './transformPath.js'

export type NodeOperation<T> =
| {
Expand Down
2 changes: 1 addition & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IndexPath } from './indexPath'
import { IndexPath } from './indexPath.js'

export type TraversalContext<T> = {
getRoot(): T
Expand Down
4 changes: 4 additions & 0 deletions src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "module"
}

6 changes: 3 additions & 3 deletions src/reduce.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IndexPath } from './indexPath'
import { BaseOptions } from './options'
import { visit } from './visit'
import { IndexPath } from './indexPath.js'
import { BaseOptions } from './options.js'
import { visit } from './visit.js'

export type ReduceOptions<T, R> = BaseOptions<T> & {
/**
Expand Down
6 changes: 3 additions & 3 deletions src/remove.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { IndexPath } from './indexPath'
import { IndexPath } from './indexPath.js'
import {
applyOperations,
getRemovalOperations,
transformPathsByOperations,
} from './operation'
import { MutationBaseOptions } from './options'
} from './operation.js'
import { MutationBaseOptions } from './options.js'

export type RemoveOptions<T> = MutationBaseOptions<T> & {
paths: IndexPath[]
Expand Down
6 changes: 3 additions & 3 deletions src/replace.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IndexPath } from './indexPath'
import { applyOperations, getReplaceOperations } from './operation'
import { MutationBaseOptions } from './options'
import { IndexPath } from './indexPath.js'
import { applyOperations, getReplaceOperations } from './operation.js'
import { MutationBaseOptions } from './options.js'

export type ReplaceOptions<T> = MutationBaseOptions<T> & {
path: IndexPath
Expand Down
4 changes: 2 additions & 2 deletions src/sort.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IndexPath } from './indexPath'
import { KeyPath } from './types'
import { IndexPath } from './indexPath.js'
import { KeyPath } from './types.js'

export function comparePathsByComponent<TPath extends IndexPath | KeyPath>(
a: TPath,
Expand Down
6 changes: 3 additions & 3 deletions src/splice.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { IndexPath } from './indexPath'
import { IndexPath } from './indexPath.js'
import {
applyOperations,
getInsertionOperations,
getRemovalOperations,
transformPathsByOperations,
} from './operation'
import { MutationBaseOptions } from './options'
} from './operation.js'
import { MutationBaseOptions } from './options.js'

export type SpliceOptions<T> = MutationBaseOptions<T> & {
path: IndexPath
Expand Down
Loading