Skip to content

Commit 4c4df4d

Browse files
committed
Add JSDoc based types
1 parent d4e9db7 commit 4c4df4d

File tree

5 files changed

+56
-6
lines changed

5 files changed

+56
-6
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: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
1+
/**
2+
* @typedef {import('hast').Parent['children'][number]|import('hast').Root} HastNode
3+
* @typedef {import('hast').Text} HastText
4+
* @typedef {import('hast').Element & {tagName: 'br'}} HastBreakElement
5+
*/
6+
17
var search = /\r?\n|\r/g
28

3-
// Implementation of the `innerText` setter:
4-
// <https://html.spec.whatwg.org/#the-innertext-idl-attribute>
5-
// Note that `innerText` only exists on element.
6-
// In this utility, we accept all parent nodes and handle them as elements, and
7-
// for all literals we set the `value` of the given node the the given value.
9+
/**
10+
* Implementation of the `innerText` setter:
11+
* <https://html.spec.whatwg.org/#the-innertext-idl-attribute>
12+
* Note that `innerText` only exists on element.
13+
* In this utility, we accept all parent nodes and handle them as elements, and
14+
* for all literals we set the `value` of the given node the the given value.
15+
*
16+
* @template {HastNode} T
17+
* @param {T} node
18+
* @param {string} [content]
19+
* @returns {T}
20+
*/
821
export function fromText(node, content) {
922
var value = content === undefined || content === null ? '' : String(content)
23+
/** @type {Array.<HastBreakElement|HastText>} */
1024
var nodes = []
1125
var start = 0
26+
/** @type {RegExpMatchArray} */
1227
var match
28+
/** @type {number} */
1329
var end
1430

1531
if ('children' in node) {

package.json

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,35 @@
2828
"sideEffects": false,
2929
"type": "module",
3030
"main": "index.js",
31+
"types": "index.d.ts",
3132
"files": [
33+
"index.d.ts",
3234
"index.js"
3335
],
36+
"dependencies": {
37+
"@types/hast": "^2.0.0"
38+
},
3439
"devDependencies": {
40+
"@types/tape": "^4.0.0",
3541
"c8": "^7.0.0",
3642
"hastscript": "^7.0.0",
3743
"prettier": "^2.0.0",
3844
"remark-cli": "^9.0.0",
3945
"remark-preset-wooorm": "^8.0.0",
46+
"rimraf": "^3.0.0",
4047
"tape": "^5.0.0",
48+
"type-coverage": "^2.0.0",
49+
"typescript": "^4.0.0",
4150
"unist-builder": "^3.0.0",
4251
"xo": "^0.39.0"
4352
},
4453
"scripts": {
54+
"prepack": "npm run build && npm run format",
55+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
4556
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
4657
"test-api": "node test.js",
4758
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
48-
"test": "npm run format && npm run test-coverage"
59+
"test": "npm run build && npm run format && npm run test-coverage"
4960
},
5061
"prettier": {
5162
"tabWidth": 2,
@@ -66,5 +77,10 @@
6677
"plugins": [
6778
"preset-wooorm"
6879
]
80+
},
81+
"typeCoverage": {
82+
"atLeast": 100,
83+
"detail": true,
84+
"strict": true
6985
}
7086
}

test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import {fromText} from './index.js'
55

66
test('hast-util-from-text', function (t) {
77
t.deepEqual(
8+
// @ts-ignore runtime.
89
fromText(u('text'), 'foo'),
910
u('text', 'foo'),
1011
'should set text nodes'
1112
)
1213

14+
// @ts-ignore runtime.
1315
t.deepEqual(fromText(u('text')), u('text', ''), 'should reset text nodes (1)')
1416

1517
t.deepEqual(

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

0 commit comments

Comments
 (0)