Skip to content

Commit 8fb3f2a

Browse files
committed
Add JSDoc based types
1 parent 271e703 commit 8fb3f2a

File tree

6 files changed

+68
-13
lines changed

6 files changed

+68
-13
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/

build.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import concat from 'concat-stream'
44
import {bail} from 'bail'
55
import unified from 'unified'
66
import html from 'rehype-parse'
7+
// @ts-ignore
78
import q from 'hast-util-select'
9+
// @ts-ignore
810
import toString from 'hast-util-to-string'
911
import {htmlElementAttributes} from './index.js'
1012

@@ -21,19 +23,34 @@ if (!globals) {
2123
// Crawl WHATWG HTML.
2224
https.get('https://html.spec.whatwg.org/multipage/indices.html', onhtml)
2325

26+
/**
27+
* @param {import('http').IncomingMessage} response
28+
*/
2429
function onhtml(response) {
2530
response.pipe(concat(onconcat)).on('error', bail)
2631

32+
/**
33+
* @param {Buffer} buf
34+
*/
2735
function onconcat(buf) {
2836
var nodes = q.selectAll('#attributes-1 tbody tr', processor.parse(buf))
2937
var index = -1
3038
var result = {}
39+
/** @type {string[]} */
3140
var keys
41+
/** @type {string} */
3242
var key
43+
/** @type {string} */
3344
var name
45+
/** @type {string} */
46+
var value
47+
/** @type {string[]} */
3448
var elements
49+
/** @type {string} */
3550
var tagName
51+
/** @type {string[]} */
3652
var attributes
53+
/** @type {number} */
3754
var offset
3855

3956
// Throw if we didn’t match, e.g., when the spec updates.
@@ -43,16 +60,16 @@ function onhtml(response) {
4360

4461
while (++index < nodes.length) {
4562
name = toString(nodes[index].children[0]).trim()
46-
elements = toString(nodes[index].children[1]).trim()
63+
value = toString(nodes[index].children[1]).trim()
4764

48-
if (/custom elements/i.test(elements)) {
65+
if (/custom elements/i.test(value)) {
4966
continue
5067
}
5168

5269
offset = -1
53-
elements = /HTML elements/.test(elements)
70+
elements = /HTML elements/.test(value)
5471
? ['*']
55-
: elements.split(/;/g).map((d) => d.replace(/\([^)]+\)/g, '').trim())
72+
: value.split(/;/g).map((d) => d.replace(/\([^)]+\)/g, '').trim())
5673

5774
while (++offset < elements.length) {
5875
tagName = elements[offset].toLowerCase().trim()
@@ -75,9 +92,7 @@ function onhtml(response) {
7592

7693
if (key !== '*') {
7794
htmlElementAttributes[key] = htmlElementAttributes[key].filter(
78-
function (attribute) {
79-
return !globals.includes(attribute)
80-
}
95+
(/** @type {string} */ d) => !globals.includes(d)
8196
)
8297
}
8398

package.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,15 @@
2626
"sideEffects": false,
2727
"type": "module",
2828
"main": "index.js",
29+
"types": "index.d.ts",
2930
"files": [
31+
"index.d.ts",
3032
"index.js"
3133
],
3234
"devDependencies": {
35+
"@types/concat-stream": "^1.6.0",
36+
"@types/node": "^14.14.36",
37+
"@types/tape": "^4.0.0",
3338
"bail": "^2.0.0",
3439
"c8": "^7.0.0",
3540
"concat-stream": "^2.0.0",
@@ -39,16 +44,21 @@
3944
"rehype-parse": "^7.0.0",
4045
"remark-cli": "^9.0.0",
4146
"remark-preset-wooorm": "^8.0.0",
47+
"rimraf": "^3.0.0",
4248
"tape": "^5.0.0",
49+
"type-coverage": "^2.0.0",
50+
"typescript": "^4.0.0",
4351
"unified": "^9.0.0",
4452
"xo": "^0.38.0"
4553
},
4654
"scripts": {
55+
"prepack": "npm run build && npm run format",
56+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
4757
"generate": "node build",
4858
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
4959
"test-api": "node test.js",
5060
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
51-
"test": "npm run generate && npm run format && npm run test-coverage"
61+
"test": "npm run generate && npm run build && npm run format && npm run test-coverage"
5262
},
5363
"prettier": {
5464
"tabWidth": 2,
@@ -70,5 +80,10 @@
7080
"plugins": [
7181
"preset-wooorm"
7282
]
83+
},
84+
"typeCoverage": {
85+
"atLeast": 100,
86+
"detail": true,
87+
"strict": true
7388
}
7489
}

readme.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,18 @@ console.log(htmlElementAttributes.ol)
3737
Yields:
3838

3939
```js
40-
[ 'accesskey',
40+
[
41+
'accesskey',
4142
'autocapitalize',
4243
'autofocus',
4344
'class',
44-
// ...
45+
//
4546
'style',
4647
'tabindex',
4748
'title',
48-
'translate' ]
49-
[ 'compact', 'reversed', 'start', 'type' ]
49+
'translate'
50+
]
51+
['compact', 'reversed', 'start', 'type']
5052
```
5153

5254
## API

test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ test('htmlElementAttributes', function (t) {
88
t.equal(typeof htmlElementAttributes, 'object', 'should be an `object`')
99

1010
t.doesNotThrow(function () {
11+
/** @type {string} */
1112
var key
1213

1314
for (key in htmlElementAttributes) {
@@ -18,10 +19,15 @@ test('htmlElementAttributes', function (t) {
1819
}, 'values should be array')
1920

2021
t.doesNotThrow(function () {
22+
/** @type {string} */
2123
var key
24+
/** @type {string[]} */
2225
var props
26+
/** @type {number} */
2327
var index
28+
/** @type {string} */
2429
var prop
30+
/** @type {string} */
2531
var label
2632

2733
for (key in htmlElementAttributes) {
@@ -33,7 +39,7 @@ test('htmlElementAttributes', function (t) {
3339
prop = props[index]
3440
label = prop + ' in ' + key
3541

36-
assert.ok(typeof prop, 'string', label + ' should be string')
42+
assert.strictEqual(typeof prop, 'string', label + ' should be string')
3743
assert.strictEqual(
3844
prop,
3945
prop.toLowerCase(),

tsconfig.json

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

0 commit comments

Comments
 (0)