Skip to content

Commit 4d2f462

Browse files
committed
Add JSDoc based types
1 parent 1a5f4b9 commit 4d2f462

File tree

14 files changed

+139
-15
lines changed

14 files changed

+139
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.DS_Store
2+
*.d.ts
23
*.log
34
coverage/
45
node_modules/

index.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,50 @@
1+
/**
2+
* @typedef {import('unist').Node} Node
3+
* @typedef {import('unist').Parent} Parent
4+
* @typedef {import('unist-util-visit').Visitor<Node>} Visitor
5+
*
6+
* @typedef ZoneInfo
7+
* @property {number} start
8+
* @property {number} end
9+
* @property {Parent|null} parent
10+
*
11+
* @callback Handler
12+
* @param {Node|undefined} start
13+
* @param {Array.<Node>} between
14+
* @param {Node|undefined} end
15+
* @param {ZoneInfo} info
16+
*/
17+
118
import {commentMarker} from 'mdast-comment-marker'
219
import {visit} from 'unist-util-visit'
320

21+
/**
22+
* @param {Node} node
23+
* @param {string} name
24+
* @param {Handler} callback
25+
*/
426
export function zone(node, name, callback) {
27+
/** @type {number} */
528
var level
29+
/** @type {Node} */
630
var marker
31+
/** @type {Parent} */
732
var scope
833

934
visit(node, gather)
1035

11-
// Gather one dimensional zones.
36+
/**
37+
* Gather one dimensional zones.
38+
* @type {Visitor}
39+
*/
1240
function gather(node, index, parent) {
1341
var info = commentMarker(node)
1442
var match =
1543
info && info.name === name && info.attributes.match(/(start|end)\b/)
1644
var type = match && match[0]
45+
/** @type {number} */
1746
var start
47+
/** @type {Array.<Node>} */
1848
var result
1949

2050
if (type) {

package.json

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,38 @@
2929
"sideEffects": false,
3030
"type": "module",
3131
"main": "index.js",
32+
"types": "index.d.ts",
3233
"files": [
34+
"index.d.ts",
3335
"index.js"
3436
],
3537
"dependencies": {
38+
"@types/mdast": "^3.0.3",
39+
"@types/unist": "^2.0.3",
3640
"mdast-comment-marker": "^2.0.0",
3741
"unist-util-visit": "^3.0.0"
3842
},
3943
"devDependencies": {
44+
"@types/tape": "^4.0.0",
4045
"c8": "^7.0.0",
4146
"is-hidden": "^2.0.0",
4247
"prettier": "^2.0.0",
4348
"remark": "^13.0.0",
4449
"remark-cli": "^9.0.0",
4550
"remark-preset-wooorm": "^8.0.0",
51+
"rimraf": "^3.0.0",
4652
"tape": "^5.0.0",
53+
"type-coverage": "^2.0.0",
54+
"typescript": "^4.0.0",
4755
"xo": "^0.39.0"
4856
},
4957
"scripts": {
58+
"prepack": "npm run build && npm run format",
59+
"build": "rimraf \"{test/**,}*.d.ts\" && tsc && type-coverage",
5060
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
5161
"test-api": "node test/index.js",
5262
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test/index.js",
53-
"test": "npm run format && npm run test-coverage"
63+
"test": "npm run build && npm run format && npm run test-coverage"
5464
},
5565
"prettier": {
5666
"tabWidth": 2,
@@ -63,6 +73,7 @@
6373
"xo": {
6474
"prettier": true,
6575
"rules": {
76+
"capitalized-comments": "off",
6677
"no-var": "off",
6778
"prefer-arrow-callback": "off"
6879
}
@@ -71,5 +82,10 @@
7182
"plugins": [
7283
"preset-wooorm"
7384
]
85+
},
86+
"typeCoverage": {
87+
"atLeast": 100,
88+
"detail": true,
89+
"strict": true
7490
}
7591
}

test/fixtures/mismatched/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
export default function assertion(t, zone, tree) {
1+
/**
2+
* @param {import('tape').Test} _
3+
* @param {import('../../../index.js').zone} zone
4+
* @param {import('unist').Node} tree
5+
*/
6+
export default function assertion(_, zone, tree) {
27
zone(tree, 'foo', handle)
38

49
function handle() {

test/fixtures/nodes/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* @param {import('tape').Test} t
3+
* @param {import('../../../index.js').zone} zone
4+
* @param {import('unist').Node} tree
5+
*/
16
export default function assertion(t, zone, tree) {
27
var count = 0
38

@@ -6,9 +11,11 @@ export default function assertion(t, zone, tree) {
611

712
zone(tree, 'foo', handle)
813

9-
function handle(start, nodes) {
14+
/** @type {import('../../../index.js').Handler} */
15+
function handle(_, nodes) {
1016
st.equal(nodes.length, 1)
1117
st.equal(nodes[0].type, 'paragraph')
18+
// @ts-ignore hush
1219
st.equal(nodes[0].children.length, 1)
1320
st.equal(nodes[0].children[0].type, 'text')
1421
st.equal(nodes[0].children[0].value, 'Foo.')

test/fixtures/non-marker/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
export default function assertion(t, zone, tree) {
1+
/**
2+
* @param {import('tape').Test} _
3+
* @param {import('../../../index.js').zone} zone
4+
* @param {import('unist').Node} tree
5+
*/
6+
export default function assertion(_, zone, tree) {
27
zone(tree, 'foo', handle)
38

49
function handle() {

test/fixtures/range-children/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* @param {import('tape').Test} t
3+
* @param {import('../../../index.js').zone} zone
4+
* @param {import('unist').Node} tree
5+
*/
16
export default function assertion(t, zone, tree) {
27
var count = 0
38

@@ -6,7 +11,8 @@ export default function assertion(t, zone, tree) {
611

712
zone(tree, 'foo', handle)
813

9-
function handle(start, nodes) {
14+
/** @type {import('../../../index.js').Handler} */
15+
function handle(_, nodes) {
1016
st.equal(nodes.length, 1)
1117
st.equal(nodes[0].type, 'blockquote')
1218
st.equal(++count, 1)
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
export default function assertion(t, zone, tree) {
1+
/**
2+
* @param {import('tape').Test} _
3+
* @param {import('../../../index.js').zone} zone
4+
* @param {import('unist').Node} tree
5+
*/
6+
export default function assertion(_, zone, tree) {
27
zone(tree, 'foo', handle)
38

4-
function handle(start, nodes, end) {
9+
/** @type {import('../../../index.js').Handler} */
10+
function handle(start, _, end) {
511
return [start, end]
612
}
713
}

test/fixtures/remove/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
export default function assertion(t, zone, tree) {
1+
/**
2+
* @param {import('tape').Test} _
3+
* @param {import('../../../index.js').zone} zone
4+
* @param {import('unist').Node} tree
5+
*/
6+
export default function assertion(_, zone, tree) {
27
zone(tree, 'foo', handle)
38

49
function handle() {

test/fixtures/replace-children/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
export default function assertion(t, zone, tree) {
1+
/**
2+
* @param {import('tape').Test} _
3+
* @param {import('../../../index.js').zone} zone
4+
* @param {import('unist').Node} tree
5+
*/
6+
export default function assertion(_, zone, tree) {
27
zone(tree, 'foo', handle)
38

4-
function handle(start, nodes, end) {
9+
/** @type {import('../../../index.js').Handler} */
10+
function handle(start, _, end) {
511
return [
612
start,
713
{

test/fixtures/replace/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
export default function assertion(t, zone, tree) {
1+
/**
2+
* @param {import('tape').Test} _
3+
* @param {import('../../../index.js').zone} zone
4+
* @param {import('unist').Node} tree
5+
*/
6+
export default function assertion(_, zone, tree) {
27
zone(tree, 'foo', handle)
38

49
function handle() {

test/fixtures/simple/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
/**
2+
* @param {import('tape').Test} t
3+
* @param {import('../../../index.js').zone} zone
4+
* @param {import('unist').Node} tree
5+
*/
16
export default function assertion(t, zone, tree) {
27
t.test('range', function (st) {
38
st.plan(5)
49

510
zone(tree, 'foo', handle)
611

12+
/** @type {import('../../../index.js').Handler} */
713
function handle(start, nodes, end) {
814
st.equal(start.type, 'html')
915
st.equal(start.value, '<!--foo start bar="baz"-->')

test/index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/**
2+
* @typedef {import('tape').Test} Test
3+
* @typedef {import('unist').Node} Node
4+
* @typedef {import('../index.js').zone} Zone
5+
*/
6+
17
import fs from 'fs'
28
import path from 'path'
39
import test from 'tape'
@@ -9,8 +15,11 @@ test('mdast-zone', async function (t) {
915
var root = path.join('test', 'fixtures')
1016
var fixtures = fs.readdirSync(root)
1117
var index = -1
18+
/** @type {string} */
1219
var output
20+
/** @type {string} */
1321
var name
22+
/** @type {(t: Test, zone: Zone, node: Node) => void} */
1423
var mod
1524

1625
while (++index < fixtures.length) {
@@ -20,13 +29,15 @@ test('mdast-zone', async function (t) {
2029
if (isHidden(name)) continue
2130

2231
try {
23-
output = fs.readFileSync(path.join(root, name, 'output.md'))
32+
output = String(fs.readFileSync(path.join(root, name, 'output.md')))
2433
} catch {}
2534

35+
/* eslint-disable no-await-in-loop */
2636
mod =
27-
/* eslint-disable-next-line no-await-in-loop */
37+
// @ts-ignore hush.
2838
(await import(new URL('fixtures/' + name + '/index.js', import.meta.url)))
29-
.default
39+
.default // type-coverage:ignore-line
40+
/* eslint-enable no-await-in-loop */
3041

3142
remark()
3243
.use(() => (tree) => {

tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"include": ["*.js", "test/**/*.js"],
3+
"compilerOptions": {
4+
"target": "ES2020",
5+
"lib": ["ES2020"],
6+
"module": "ES2020",
7+
"moduleResolution": "node",
8+
"allowJs": true,
9+
"checkJs": true,
10+
"declaration": true,
11+
"emitDeclarationOnly": true,
12+
"allowSyntheticDefaultImports": true,
13+
"skipLibCheck": true
14+
}
15+
}

0 commit comments

Comments
 (0)