Skip to content

Commit

Permalink
Add JSDoc based types
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Apr 13, 2021
1 parent 1c06a3a commit 5461680
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
*.d.ts
*.log
coverage/
node_modules/
Expand Down
39 changes: 33 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
export function visitChildren(callback) {
return visitor
/**
* @typedef {import('unist').Parent} Parent
* @typedef {import('unist').Node} Node
*
* @callback Visitor
* @param {Node} node
* @param {number} index
* @param {Parent} parent
* @returns {void}
*
* @callback Visit
* @param {Parent} node
* @returns {void}
*/

// Visit `parent`, invoking `callback` for each child.
function visitor(parent) {
/**
* Wrap `visitor` to be called for each child in the nodes later given to
* `visit`.
*
* @param {Visitor} visitor
* @returns {Visit}
*/
export function visitChildren(visitor) {
return visit

/**
* Visit `parent`, calling `visitor` for each child.
*
* @param {Parent} parent
* @returns {void}
*/
function visit(parent) {
var index = -1
var children = parent && parent.children

if (!children) {
throw new Error('Missing children in `parent` for `visitor`')
throw new Error('Missing children in `parent` for `visit`')
}

while (++index in children) {
callback(children[index], index, parent)
visitor(children[index], index, parent)
}
}
}
18 changes: 17 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,33 @@
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js"
],
"dependencies": {
"@types/unist": "^2.0.0"
},
"devDependencies": {
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"rimraf": "^3.0.0",
"tape": "^5.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"xo": "^0.38.0"
},
"scripts": {
"prepack": "npm run build && npm run format",
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
"test": "npm run format && npm run test-coverage"
"test": "npm run build && npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -60,5 +71,10 @@
"plugins": [
"preset-wooorm"
]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true
}
}
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ There is no default export.

### `visit = visitChildren(visitor)`

Wrap [`visitor`][visitor] to be invoked for each [child][] in the nodes later
Wrap [`visitor`][visitor] to be called for each [child][] in the nodes later
given to [`visit`][visit].

#### `function visitor(child, index, parent)`

Invoked if [`visit`][visit] is called on a [parent][] node for each [child][].
Called if [`visit`][visit] is called on a [parent][] node for each [child][].

#### `function visit(parent)`

Invoke [`visitor`][visitor] for each [child][] of the [parent][].
Called [`visitor`][visitor] for each [child][] of the [parent][].

## Related

Expand Down
47 changes: 34 additions & 13 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function noop() {}
test('visitChildren()', function (t) {
t.throws(
function () {
// @ts-ignore runtime.
visitChildren(noop)()
},
/Missing children in `parent`/,
Expand All @@ -14,22 +15,26 @@ test('visitChildren()', function (t) {

t.throws(
function () {
// @ts-ignore runtime.
visitChildren(noop)({})
},
/Missing children in `parent`/,
'should throw without parent'
)

t.test('should invoke `fn` for each child in `parent`', function (st) {
var values = [0, 1, 2, 3]
var context = {}
t.test('should call `fn` for each child in `parent`', function (st) {
var children = [
{type: 'x', value: 0},
{type: 'x', value: 1},
{type: 'x', value: 2},
{type: 'x', value: 3}
]
var context = {type: 'y', children}
var n = -1

context.children = values

visitChildren(function (value, index, parent) {
visitChildren(function (child, index, parent) {
n++
st.strictEqual(value, values[n])
st.strictEqual(child, children[n])
st.strictEqual(index, n)
st.strictEqual(parent, context)
})(context)
Expand All @@ -38,19 +43,35 @@ test('visitChildren()', function (t) {
})

t.test('should work when new children are added', function (st) {
var values = [0, 1, 2, 3, 4, 5, 6]
var children = [
{type: 'x', value: 0},
{type: 'x', value: 1},
{type: 'x', value: 2},
{type: 'x', value: 3},
{type: 'x', value: 4},
{type: 'x', value: 5},
{type: 'x', value: 6}
]
var n = -1

visitChildren(function (value, index, parent) {
visitChildren(function (child, index, parent) {
n++

if (index < 3) {
parent.children.push(parent.children.length)
parent.children.push({type: 'x', value: parent.children.length})
}

st.strictEqual(value, values[n])
st.strictEqual(index, values[n])
})({children: [0, 1, 2, 3]})
st.deepEqual(child, children[n])
st.deepEqual(index, n)
})({
type: 'y',
children: [
{type: 'x', value: 0},
{type: 'x', value: 1},
{type: 'x', value: 2},
{type: 'x', value: 3}
]
})

st.end()
})
Expand Down
15 changes: 15 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"include": ["*.js"],
"compilerOptions": {
"target": "ES2020",
"lib": ["ES2020"],
"module": "ES2020",
"moduleResolution": "node",
"allowJs": true,
"checkJs": true,
"declaration": true,
"emitDeclarationOnly": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true
}
}

0 comments on commit 5461680

Please sign in to comment.