Skip to content

Commit 0085890

Browse files
committed
Add JSDoc based types
1 parent e64f656 commit 0085890

9 files changed

+64
-57
lines changed

Diff for: .gitignore

+1
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/

Diff for: index.js

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1+
/**
2+
* @typedef {import('unist').Node} Node
3+
* @typedef {import('mdast').Definition} Definition
4+
* @typedef {import('unist-util-visit').Visitor<Definition>} DefinitionVisitor
5+
*/
6+
17
import {visit} from 'unist-util-visit'
28

39
var own = {}.hasOwnProperty
410

5-
// Get a definition in `node` by `identifier`.
11+
/**
12+
*
13+
* @param {Node} node
14+
*/
615
export function definitions(node) {
7-
var cache = {}
16+
/** @type {Object.<string, Definition>} */
17+
var cache = Object.create(null)
818

919
if (!node || !node.type) {
1020
throw new Error('mdast-util-definitions expected node')
@@ -14,20 +24,30 @@ export function definitions(node) {
1424

1525
return getDefinition
1626

27+
/** @type {DefinitionVisitor} */
1728
function ondefinition(definition) {
1829
var id = clean(definition.identifier)
1930
if (id && !own.call(cache, id)) {
2031
cache[id] = definition
2132
}
2233
}
2334

24-
// Get a node from the bound definition-cache.
35+
/**
36+
* Get a node from the bound definition-cache.
37+
*
38+
* @param {string} identifier
39+
* @returns {Definition|null}
40+
*/
2541
function getDefinition(identifier) {
2642
var id = clean(identifier)
2743
return id && own.call(cache, id) ? cache[id] : null
2844
}
2945
}
3046

47+
/**
48+
* @param {string} [value]
49+
* @returns {string}
50+
*/
3151
function clean(value) {
3252
return String(value || '').toUpperCase()
3353
}

Diff for: package.json

+18-8
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,36 @@
2929
"sideEffects": false,
3030
"type": "module",
3131
"main": "index.js",
32-
"types": "types/index.d.ts",
32+
"types": "index.d.ts",
3333
"files": [
34-
"types",
34+
"index.d.ts",
3535
"index.js"
3636
],
3737
"dependencies": {
38+
"@types/mdast": "^3.0.0",
39+
"@types/unist": "^2.0.0",
3840
"unist-util-visit": "^3.0.0"
3941
},
4042
"devDependencies": {
41-
"@types/mdast": "^3.0.0",
43+
"@types/tape": "^4.0.0",
4244
"c8": "^7.0.0",
4345
"prettier": "^2.0.0",
4446
"remark": "^13.0.0",
4547
"remark-cli": "^9.0.0",
4648
"remark-preset-wooorm": "^8.0.0",
49+
"rimraf": "^3.0.0",
4750
"tape": "^5.0.0",
51+
"type-coverage": "^2.0.0",
52+
"typescript": "^4.0.0",
4853
"xo": "^0.39.0"
4954
},
5055
"scripts": {
56+
"prepack": "npm run build && npm run format",
57+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
5158
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
5259
"test-api": "node test.js",
5360
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
54-
"test": "npm run format && npm run test-coverage"
61+
"test": "npm run build && npm run format && npm run test-coverage"
5562
},
5663
"prettier": {
5764
"tabWidth": 2,
@@ -64,16 +71,19 @@
6471
"xo": {
6572
"prettier": true,
6673
"rules": {
74+
"capitalized-comments": "off",
6775
"no-var": "off",
6876
"prefer-arrow-callback": "off"
69-
},
70-
"ignore": [
71-
"types/"
72-
]
77+
}
7378
},
7479
"remarkConfig": {
7580
"plugins": [
7681
"preset-wooorm"
7782
]
83+
},
84+
"typeCoverage": {
85+
"atLeast": 100,
86+
"detail": true,
87+
"strict": true
7888
}
7989
}

Diff for: test.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1+
/**
2+
* @typedef {import('unist').Node} Node
3+
*/
4+
15
import test from 'tape'
26
import remark from 'remark'
37
import {definitions} from './index.js'
48

59
test('mdast-util-definitions', function (t) {
10+
/** @type {Node} */
611
var tree
712

813
t.throws(
914
function () {
15+
// @ts-ignore runtime
1016
definitions()
1117
},
1218
/mdast-util-definitions expected node/,
@@ -52,7 +58,7 @@ test('mdast-util-definitions', function (t) {
5258
)
5359

5460
/* eslint-disable-next-line no-use-extend-native/no-use-extend-native */
55-
t.equal({}.type, undefined, 'should not polute the prototype')
61+
t.equal({}.type, undefined, 'should not polute the prototype') // type-coverage:ignore-line
5662

5763
t.deepEqual(
5864
definitions(tree)('toString'),

Diff for: tsconfig.json

+15
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+
}

Diff for: types/index.d.ts

-17
This file was deleted.

Diff for: types/mdast-util-definitions-tests.ts

-10
This file was deleted.

Diff for: types/tsconfig.json

-10
This file was deleted.

Diff for: types/tslint.json

-8
This file was deleted.

0 commit comments

Comments
 (0)