Skip to content

Commit e81e650

Browse files
committed
Refactor types
1 parent 2a3be52 commit e81e650

File tree

4 files changed

+47
-23
lines changed

4 files changed

+47
-23
lines changed

index.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,26 @@ module.exports = find
2323
/**
2424
* Unist node finder utility.
2525
*
26-
* @param tree
27-
* Node to search.
28-
* @param condition
29-
* Condition used to test each node.
30-
* @returns
31-
* The first node that matches condition, or undefined if no node matches.
32-
* @type {<V extends Node>(tree: Node, condition: TestStr | TestObj | TestFn) => V | undefined}
26+
* @template {Node} V
27+
* Node to search for.
28+
* @param {Node} tree
29+
* Tree to search in.
30+
* @param {TestStr | TestObj | TestFn} condition
31+
* Condition used to test each node, which matches `V`.
32+
* @returns {V | undefined}
33+
* The first node that matches condition, or `undefined` if no node matches.
3334
*/
3435
function find(tree, condition) {
3536
if (!tree) throw new Error('unist-util-find requires a tree to search')
3637
if (!condition) throw new Error('unist-util-find requires a condition')
3738

3839
const predicate = iteratee(condition)
40+
/** @type {V | undefined} */
3941
let result
4042

4143
visit(tree, function (node) {
4244
if (predicate(node)) {
45+
// @ts-expect-error: assume `predicate` checks for `V`.
4346
result = node
4447
return false
4548
}

package.json

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
],
1212
"sideEffects": false,
1313
"scripts": {
14-
"prepack": "npm run build",
14+
"prepack": "npm run build && npm run format",
15+
"build": "tsc --build --clean && tsc --build && type-coverage",
1516
"format": "prettier . -w --loglevel warn && xo --fix",
1617
"test-api": "node --conditions development test.js",
17-
"test": "npm run format && npm run test-api",
18-
"build": "tsc"
18+
"test": "npm run build && npm run format && npm run test-api"
1919
},
2020
"type": "commonjs",
2121
"keywords": [
@@ -33,11 +33,14 @@
3333
"license": "MIT",
3434
"devDependencies": {
3535
"@types/lodash.iteratee": "^4.7.7",
36+
"@types/mdast": "^3.0.0",
37+
"@types/tape": "^5.0.0",
3638
"@types/unist": "^2.0.6",
3739
"prettier": "^2.0.0",
3840
"remark": "^13.0.0",
3941
"tape": "^5.3.1",
40-
"typescript": "^4.6.4",
42+
"type-coverage": "^2.0.0",
43+
"typescript": "^5.0.0",
4144
"xo": "^0.54.0"
4245
},
4346
"dependencies": {
@@ -52,6 +55,12 @@
5255
"trailingComma": "none",
5356
"useTabs": false
5457
},
58+
"typeCoverage": {
59+
"atLeast": 100,
60+
"detail": true,
61+
"ignoreCatch": true,
62+
"strict": true
63+
},
5564
"xo": {
5665
"rules": {
5766
"unicorn/prefer-module": "off"

test.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,42 @@
1+
/**
2+
* @typedef {import('mdast').Root} Root
3+
*/
4+
5+
const assert = require('node:assert/strict')
16
const test = require('tape')
27
const remark = require('remark')
38
const find = require('./index.js')
49

510
test('unist-find', function (t) {
6-
const tree = remark().parse('Some _emphasis_, **strongness**, and `code`.')
11+
const tree = /** @type {Root} */ (
12+
remark().parse('Some _emphasis_, **strongness**, and `code`.')
13+
)
14+
assert(tree.type === 'root')
15+
const paragraph = tree.children[0]
16+
assert(paragraph.type === 'paragraph')
717

818
t.throws(function () {
19+
// @ts-expect-error: check that an error is thrown at runtime.
920
find()
1021
}, 'should fail without tree')
1122

1223
t.throws(function () {
24+
// @ts-expect-error: check that an error is thrown at runtime.
1325
find(tree)
1426
}, 'should fail without condition')
1527

1628
t.test('should find with string condition', function (st) {
1729
const result = find(tree, 'value')
1830

19-
st.equal(result, tree.children[0].children[0])
31+
st.equal(result, paragraph.children[0])
2032

2133
st.end()
2234
})
2335

2436
t.test('should find with object condition', function (st) {
2537
const result = find(tree, {type: 'emphasis'})
2638

27-
st.equal(result, tree.children[0].children[1])
39+
st.equal(result, paragraph.children[1])
2840

2941
st.end()
3042
})
@@ -34,7 +46,7 @@ test('unist-find', function (t) {
3446
return node.type === 'inlineCode'
3547
})
3648

37-
st.equal(result, tree.children[0].children[5])
49+
st.equal(result, paragraph.children[5])
3850

3951
st.end()
4052
})

tsconfig.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"compilerOptions": {
3-
"target": "ES2020",
4-
"module": "ES2020",
5-
"moduleResolution": "node",
6-
"strict": true,
7-
"allowJs": true,
83
"checkJs": true,
9-
"allowSyntheticDefaultImports": true,
4+
"customConditions": ["development"],
105
"declaration": true,
116
"emitDeclarationOnly": true,
12-
"skipLibCheck": true
7+
"exactOptionalPropertyTypes": true,
8+
"lib": ["es2020"],
9+
"module": "node16",
10+
"strict": true,
11+
"target": "es2020"
1312
},
14-
"include": ["index.js"]
13+
"exclude": ["coverage/", "node_modules/"],
14+
"include": ["**/*.js"]
1515
}

0 commit comments

Comments
 (0)