Skip to content

Commit f348f13

Browse files
committed
Add JSDoc based types
1 parent d222773 commit f348f13

9 files changed

+90
-86
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: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,41 @@
1+
/**
2+
* @typedef {import('unist').Node} Node
3+
* @typedef {Node & {position: undefined}} NodeWithUndefinedPosition
4+
* @typedef {Omit<Node, 'position'>} NodeWithoutPosition
5+
*/
6+
17
import {visit} from 'unist-util-visit'
28

3-
export function removePosition(node, force) {
4-
visit(node, remove)
9+
export const removePosition =
10+
/**
11+
* @type {(
12+
* ((tree: Node, force?: false) => NodeWithUndefinedPosition) &
13+
* ((tree: Node, force: true) => NodeWithoutPosition)
14+
* )}
15+
*/
16+
(
17+
/**
18+
* Utility to remove positions from a tree
19+
*
20+
* @param {Node} node the unist tree
21+
* @param {boolean} [force=false] if `force` is given, uses `delete`, otherwise, sets positions to `undefined`.
22+
* @returns {NodeWithUndefinedPosition}
23+
*/
24+
function (node, force) {
25+
visit(node, remove)
526

6-
return node
27+
// @ts-ignore hush TS, we know what we’re doing.
28+
return node
729

8-
function remove(node) {
9-
if (force) {
10-
delete node.position
11-
} else {
12-
node.position = undefined
30+
/**
31+
* @param {Node} node the unist tree
32+
*/
33+
function remove(node) {
34+
if (force) {
35+
delete node.position
36+
} else {
37+
node.position = undefined
38+
}
39+
}
1340
}
14-
}
15-
}
41+
)

index.test-d.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {expectType} from 'tsd'
2+
import {Node} from 'unist'
3+
import remark from 'remark'
4+
import {
5+
removePosition,
6+
NodeWithUndefinedPosition,
7+
NodeWithoutPosition
8+
} from './index.js'
9+
10+
const tree: Node = remark().parse(
11+
'Some _emphasis_, **importance**, and `code`.'
12+
)
13+
14+
expectType<NodeWithUndefinedPosition>(removePosition(tree))
15+
expectType<NodeWithUndefinedPosition>(removePosition(tree, false))
16+
expectType<NodeWithoutPosition>(removePosition(tree, true))
17+
18+
// Used as a plugin
19+
void remark()
20+
.use(() => (tree: Node) => removePosition(tree))
21+
.process('Some _emphasis_, **importance**, and `code`.')

package.json

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,37 @@
2828
"sideEffects": false,
2929
"type": "module",
3030
"main": "index.js",
31-
"types": "types/index.d.ts",
31+
"types": "index.d.ts",
3232
"files": [
33-
"types/index.d.ts",
33+
"index.d.ts",
3434
"index.js"
3535
],
3636
"dependencies": {
37+
"@types/unist": "^2.0.0",
3738
"unist-util-visit": "^3.0.0"
3839
},
3940
"devDependencies": {
41+
"@types/tape": "^4.0.0",
4042
"c8": "^7.0.0",
4143
"prettier": "^2.0.0",
4244
"remark": "^13.0.0",
4345
"remark-cli": "^9.0.0",
4446
"remark-preset-wooorm": "^8.0.0",
47+
"rimraf": "^3.0.0",
4548
"tape": "^5.0.0",
49+
"tsd": "^0.14.0",
50+
"type-coverage": "^2.0.0",
51+
"typescript": "^4.0.0",
4652
"unist-builder": "^3.0.0",
4753
"xo": "^0.38.0"
4854
},
4955
"scripts": {
56+
"prepack": "npm run build && npm run format",
57+
"build": "rimraf \"*.d.ts\" && tsc && tsd && type-coverage",
5058
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
5159
"test-api": "node test.js",
5260
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
53-
"test": "npm run format && npm run test-coverage"
61+
"test": "npm run build && npm run format && npm run test-coverage"
5462
},
5563
"prettier": {
5664
"tabWidth": 2,
@@ -65,14 +73,16 @@
6573
"rules": {
6674
"no-var": "off",
6775
"prefer-arrow-callback": "off"
68-
},
69-
"ignores": [
70-
"types/"
71-
]
76+
}
7277
},
7378
"remarkConfig": {
7479
"plugins": [
7580
"preset-wooorm"
7681
]
82+
},
83+
"typeCoverage": {
84+
"atLeast": 100,
85+
"detail": true,
86+
"strict": true
7787
}
7888
}

tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"include": ["*.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+
}

types/index.d.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

types/tsconfig.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

types/tslint.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

types/unist-util-remove-position-test.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)