Skip to content

Commit

Permalink
Add JSDoc based types
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed May 10, 2021
1 parent dd52940 commit 868517c
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
*.d.ts
*.log
coverage/
node_modules/
Expand Down
37 changes: 36 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/**
* @typedef {import('hast').Node} Node
* @typedef {import('hast').Parent} Parent
* @typedef {import('hast').Literal} Literal
* @typedef {import('hast').Root} Root
* @typedef {import('hast').Element} Element
* @typedef {import('hast').DocType} Doctype
*/

import nodeAssert from 'assert'
import {zwitch} from 'zwitch'
import {mapz} from 'mapz'
Expand Down Expand Up @@ -60,17 +69,30 @@ var hast = zwitch('type', {
}
})

var all = mapz(hast, {key: 'children', indices: false})
var all = mapz(hast, {key: 'children'})

/**
* @param {unknown} node
* @param {Parent} [ancestor]
* @returns {asserts node is Node}
*/
function unknown(node, ancestor) {
unistAssert(node, ancestor)
}

/**
* @param {unknown} node
* @returns {asserts node is Parent}
*/
function assertParent(node) {
unistParent(node)
all(node)
}

/**
* @param {unknown} node
* @returns {asserts node is Literal}
*/
function assertLiteral(node) {
unistLiteral(node)
nodeAssert.strictEqual(
Expand All @@ -80,11 +102,20 @@ function assertLiteral(node) {
)
}

/**
* @param {unknown} node
* @param {Parent} [ancestor]
* @returns {asserts node is Root}
*/
function assertRoot(node, ancestor) {
assertParent(node)
nodeAssert.strictEqual(ancestor, undefined, '`root` should not have a parent')
}

/**
* @param {unknown} node
* @returns {asserts node is Element}
*/
function assertElement(node) {
assertParent(node)

Expand All @@ -100,6 +131,10 @@ function assertElement(node) {
)
}

/**
* @param {unknown} node
* @returns {asserts node is Doctype}
*/
function assertDoctype(node) {
_void(node)

Expand Down
18 changes: 18 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {expectType, expectNotType} from 'tsd'
import {Node, Parent} from 'hast'
import {assert, parent} from './index.js'

var emptyNode = {type: 'doctype'}
var literalNode = {type: 'text', value: 'a'}
var parentNode = {type: 'element', children: [emptyNode, literalNode]}

expectNotType<Node>(emptyNode)
expectNotType<Node>(literalNode)
expectNotType<Node>(parentNode)

assert(emptyNode)
expectType<Node>(emptyNode)

expectNotType<Parent>(parentNode)
parent(parentNode)
expectType<Parent>(parentNode)
17 changes: 16 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,37 @@
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js"
],
"dependencies": {
"@types/hast": "^2.0.0",
"mapz": "^2.0.0",
"unist-util-assert": "^3.0.0",
"zwitch": "^2.0.0"
},
"devDependencies": {
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"rimraf": "^3.0.0",
"tape": "^5.0.0",
"tsd": "^0.14.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"xo": "^0.39.0"
},
"scripts": {
"prepack": "npm run build && npm run format",
"build": "rimraf \"{test/**,}*.d.ts\" && tsc && tsd && type-coverage",
"format": "remark . -qfo && prettier . --write --loglevel warn && xo --fix",
"test-api": "node test/index.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test/index.js",
"test": "npm run format && npm run test-coverage"
"test": "npm run build && npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -67,5 +77,10 @@
"plugins": [
"preset-wooorm"
]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true
}
}
15 changes: 15 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"include": ["*.js", "test/**/*.js"],
"compilerOptions": {
"target": "ES2020",
"lib": ["ES2020"],
"module": "ES2020",
"moduleResolution": "node",
"allowJs": true,
"checkJs": true,
"declaration": true,
"emitDeclarationOnly": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true
}
}

0 comments on commit 868517c

Please sign in to comment.