Skip to content

Commit 1c3d9f9

Browse files
committed
Add JSDoc based types
1 parent 3f4e104 commit 1c3d9f9

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
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

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
1+
/**
2+
* @typedef {import('hast').Properties} Properties
3+
* @typedef {import('hast').Element} Element
4+
*/
5+
16
var search = /[#.]/g
27

3-
// Create a hast element from a simple CSS selector.
8+
/**
9+
* Create a hast element from a simple CSS selector.
10+
*
11+
* @param {string} [selector]
12+
* @param {string} [name='div']
13+
* @returns {Element}
14+
*/
415
export function parseSelector(selector, name = 'div') {
516
var value = selector || ''
17+
/** @type {Properties} */
618
var props = {}
719
var start = 0
20+
/** @type {string} */
821
var subvalue
22+
/** @type {string} */
923
var previous
24+
/** @type {RegExpMatchArray} */
1025
var match
1126

1227
while (start < value.length) {
@@ -19,7 +34,7 @@ export function parseSelector(selector, name = 'div') {
1934
name = subvalue
2035
} else if (previous === '#') {
2136
props.id = subvalue
22-
} else if (props.className) {
37+
} else if (Array.isArray(props.className)) {
2338
props.className.push(subvalue)
2439
} else {
2540
props.className = [subvalue]

Diff for: package.json

+17-1
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,33 @@
2727
"sideEffects": false,
2828
"type": "module",
2929
"main": "index.js",
30+
"types": "index.d.ts",
3031
"files": [
32+
"index.d.ts",
3133
"index.js"
3234
],
35+
"dependencies": {
36+
"@types/hast": "^2.0.0"
37+
},
3338
"devDependencies": {
39+
"@types/tape": "^4.0.0",
3440
"c8": "^7.0.0",
3541
"prettier": "^2.0.0",
3642
"remark-cli": "^9.0.0",
3743
"remark-preset-wooorm": "^8.0.0",
44+
"rimraf": "^3.0.0",
3845
"tape": "^5.0.0",
46+
"type-coverage": "^2.0.0",
47+
"typescript": "^4.0.0",
3948
"xo": "^0.39.0"
4049
},
4150
"scripts": {
51+
"prepack": "npm run build && npm run format",
52+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
4253
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
4354
"test-api": "node test.js",
4455
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
45-
"test": "npm run format && npm run test-coverage"
56+
"test": "npm run build && npm run format && npm run test-coverage"
4657
},
4758
"prettier": {
4859
"tabWidth": 2,
@@ -63,5 +74,10 @@
6374
"plugins": [
6475
"preset-wooorm"
6576
]
77+
},
78+
"typeCoverage": {
79+
"atLeast": 100,
80+
"detail": true,
81+
"strict": true
6682
}
6783
}

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

0 commit comments

Comments
 (0)