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 Apr 24, 2021
1 parent 20528b4 commit 780659d
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 12 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
62 changes: 56 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,56 @@
/**
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Parent} Parent
* @typedef {import('mdast').Heading} Heading
*
* @typedef {(value: string, node: Heading) => boolean} TestFunction
* @typedef {string|RegExp|TestFunction} Test
*
* @typedef Options
* @property {Test} test
* @property {boolean} [ignoreFinalDefinitions=false]
*
* @typedef ZoneInfo
* @property {number} start
* @property {number} end
* @property {Parent|null} parent
*
* @callback Handler
* @param {Heading|undefined} start
* @param {Array.<Node>} between
* @param {Node|undefined} end
* @param {ZoneInfo} info
*/

import {toString} from 'mdast-util-to-string'

// Search `node` with `options` and invoke `callback`.
/**
* Search `node` with `options` and invoke `callback`.
*
* @param {Node} node
* @param {Test|Options} options
* @param {Handler} handler
*/
// eslint-disable-next-line complexity
export function headingRange(node, options, callback) {
export function headingRange(node, options, handler) {
var test = options
var children = node.children
/** @type {Array.<Node>} */
// @ts-ignore looks like children.
var children = node.children || []
var index = -1
/** @type {boolean} */
var ignoreFinalDefinitions
/** @type {number} */
var depth
/** @type {number} */
var start
/** @type {number} */
var end
/** @type {Array.<Node>} */
var nodes
/** @type {Array.<Node>} */
var result
/** @type {Node} */
var child

// Object, not regex.
Expand Down Expand Up @@ -48,7 +87,9 @@ export function headingRange(node, options, callback) {
break
}

// @ts-ignore looks like a heading.
if (!depth && test(toString(child), child)) {
// @ts-ignore looks like a heading.
depth = child.depth
start = index
// Assume no end heading is found.
Expand All @@ -68,7 +109,8 @@ export function headingRange(node, options, callback) {
}
}

nodes = callback(
nodes = handler(
// @ts-ignore `start` points to a heading.
children[start],
children.slice(start + 1, end),
children[end],
Expand All @@ -94,11 +136,19 @@ export function headingRange(node, options, callback) {
}
}

// Wrap an expression into an assertion function.
/**
* Wrap an expression into an assertion function.
* @param {RegExp} expression
* @returns {(value: string) => boolean}
*/
function wrapExpression(expression) {
return assertion

// Assert `value` matches the bound `expression`.
/**
* Assert `value` matches the bound `expression`.
* @param {string} value
* @returns {boolean}
*/
function assertion(value) {
return expression.test(value)
}
Expand Down
17 changes: 16 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,36 @@
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js"
],
"dependencies": {
"@types/mdast": "^3.0.0",
"@types/unist": "^2.0.0",
"mdast-util-to-string": "^3.0.0"
},
"devDependencies": {
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"prettier": "^2.0.0",
"remark": "^13.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"rimraf": "^3.0.0",
"tape": "^5.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"xo": "^0.39.0"
},
"scripts": {
"prepack": "npm run build && npm run format",
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.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
}
}
21 changes: 16 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* @typedef {import('tape').Test} Test
* @typedef {import('unist').Node} Node
* @typedef {import('./index.js').Test | import('./index.js').Options} Options
*/

import test from 'tape'
import remark from 'remark'
import {headingRange} from './index.js'
Expand Down Expand Up @@ -47,6 +53,7 @@ test('mdast-util-heading-range()', function (t) {
process(
t,
['# Fo', '', '## Fooooo', '', 'Bar', '', '# Fo', ''].join('\n'),
/** @type {Options} */
function (value) {
return value.toLowerCase().indexOf('foo') === 0
}
Expand Down Expand Up @@ -161,7 +168,7 @@ test('mdast-util-heading-range()', function (t) {
remark()
.use(function () {
return function (node) {
headingRange(node, 'foo', function (start, nodes, end) {
headingRange(node, 'foo', function (start, _, end) {
return [start, {type: 'thematicBreak'}, end]
})
}
Expand All @@ -184,8 +191,7 @@ test('mdast-util-heading-range()', function (t) {
return function (node) {
headingRange(node, 'foo', function (start, nodes, end) {
t.equal(nodes.length, 3)

return [start].concat(nodes, end)
return [start, ...nodes, end]
})
}
})
Expand Down Expand Up @@ -303,15 +309,20 @@ test('mdast-util-heading-range()', function (t) {
)
})

/**
* @param {Test} t
* @param {string} value
* @param {Options} options
*/
function process(t, value, options) {
return remark()
.use(function () {
return function (node) {
headingRange(node, options, function (start, nodes, end, scope) {
headingRange(node, options, function (start, _, end, scope) {
t.equal(typeof scope.start, 'number')
t.assert(typeof scope.end === 'number' || scope.end === null)
t.equal(scope.parent.type, 'root')
return [start].concat([end])
return [start, end]
})
}
})
Expand Down
15 changes: 15 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"include": ["*.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 780659d

Please sign in to comment.