Skip to content

Commit

Permalink
Refactor to improve types
Browse files Browse the repository at this point in the history
Reviewed-by: Merlijn Vos <merlijn@soverin.net>
Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com>

Related to GH-6.
Closes GH-7.
  • Loading branch information
wooorm committed Jul 1, 2021
1 parent cf1b894 commit 1e3e7d5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 35 deletions.
47 changes: 18 additions & 29 deletions index.js
@@ -1,6 +1,7 @@
'use strict'

/**
* @typedef {import('unified').FrozenProcessor} FrozenProcessor
* @typedef {import('unified').Processor} Processor
* @typedef {import('unified').RunCallback} RunCallback
* @typedef {import('unified').Transformer} Transformer
* @typedef {import('unist').Node} Node
Expand All @@ -15,40 +16,33 @@ var hast2mdast = require('hast-util-to-mdast')
* tree (bridge-mode). Without destination, returns the mdast tree: further
* plugins run on that tree (mutate-mode).
*
* @param destination Optional unified processor.
* @param options Options passed to `hast-util-to-mdast`.
*/
var attacher =
module.exports =
/**
* @type {(
* ((destination?: FrozenProcessor, options?: Options) => Transformer) &
* ((options?: Options) => Transformer)
* )}
* @type {import('unified').Plugin<[Options?]|[Processor, Options?]>}
*/
(
/**
* @param {FrozenProcessor | Options} [destination]
* @param {Processor|Options} [destination]
* @param {Options} [options]
*/
function (destination, options) {
/** @type {Options | undefined} */
/** @type {Options|undefined} */
var settings
/** @type {FrozenProcessor | undefined} */
/** @type {Processor|undefined} */
var processor

if (
destination &&
!(/** @type {FrozenProcessor} */ (destination).process)
) {
// Overload: 'options' passed to first parameter
settings = /** @type {Options} */ (destination)
destination = null
if (typeof destination === 'function') {
processor = destination
settings = options || {}
} else {
processor = /** @type {FrozenProcessor | undefined} */ (destination)
settings = destination || {}
}

settings = settings || options || {}

if (settings.document === undefined || settings.document === null) {
settings.document = true
settings = Object.assign({}, settings, {document: true})
}

return processor ? bridge(processor, settings) : mutate(settings)
Expand All @@ -58,7 +52,7 @@ var attacher =
/**
* Bridge-mode.
* Runs the destination with the new mdast tree.
* @param {FrozenProcessor} destination
* @param {Processor} destination
* @param {Options} [options]
* @returns {Transformer}
*/
Expand All @@ -69,12 +63,9 @@ function bridge(destination, options) {
destination.run(hast2mdast(node, options), file, done)
/** @type {RunCallback} */
function done(err) {
// Cast to proper type - remove when upstream typing of Transformer is fixed.
// Note: `next` only requires one parameter: https://github.com/unifiedjs/unified#function-nexterr-tree-file
var typefixedNext =
/** @type {(error: Error | null, node?: Node, file?: import('vfile').VFile) => void} */
(next)
typefixedNext(err)
// @ts-expect-error: `unified` should accept 1 arg for next.
// See: <https://github.com/unifiedjs/unified/pull/141#issuecomment-871239574>
next(err)
}
}
}
Expand All @@ -94,8 +85,6 @@ function mutate(options) {
}
}

module.exports = attacher

// Remove the following JSDoc block when upgrading hast-util-to-mdast to version 8.
// Import these types from hast-util-to-mdast when version 8 released.
/**
Expand Down
12 changes: 10 additions & 2 deletions package.json
Expand Up @@ -25,12 +25,16 @@
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
],
"dependencies": {
"@types/hast": "^2.0.0",
"@types/mdast": "^3.0.0",
"@types/unist": "^2.0.0",
"hast-util-to-mdast": "^7.0.0"
},
"files": [
"index.js"
],
"devDependencies": {
"@types/tape": "^4.0.0",
"browserify": "^17.0.0",
"nyc": "^15.0.0",
"prettier": "^2.0.0",
Expand All @@ -39,6 +43,7 @@
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"remark-stringify": "^9.0.0",
"rimraf": "^3.0.0",
"tape": "^5.0.0",
"tinyify": "^3.0.0",
"type-coverage": "^2.0.0",
Expand All @@ -48,7 +53,7 @@
},
"scripts": {
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"build-types": "tsc -b && type-coverage",
"build-types": "rimraf \"*.d.ts\" && tsc && type-coverage",
"build-bundle": "browserify . -s rehypeRemark -o rehype-remark.js",
"build-mangle": "browserify . -s rehypeRemark -o rehype-remark.min.js -p tinyify",
"build": "npm run build-types && npm run build-bundle && npm run build-mangle",
Expand Down Expand Up @@ -86,6 +91,9 @@
"atLeast": 100,
"detail": true,
"strict": true,
"ignoreCatch": true
"ignoreCatch": true,
"ignoreFiles": [
"index.d.ts"
]
}
}
20 changes: 17 additions & 3 deletions test.js
@@ -1,5 +1,12 @@
'use strict'

/**
* @typedef {import('./index.js').Options} Options
* @typedef {import('./index.js').Handle} Handle
* @typedef {import('hast').Element} Element
* @typedef {import('hast').Text} Text
*/

var test = require('tape')
var unified = require('unified')
var parse = require('rehype-parse')
Expand Down Expand Up @@ -60,12 +67,19 @@ test('rehype2remark()', function (t) {
})

test('handlers option', function (t) {
/** @type {Options} */
var options = {
handlers: {
/**
* @type {Handle}
* @param {Element & {tagName: 'div'}} node
*/
div: function (h, node) {
node.children[0].value = 'changed'
node.type = 'paragraph'
return h(node, 'paragraph', node.children)
/** @type {Text} */
// @ts-expect-error: there’s one text child.
const child = node.children[0]
child.value = 'changed'
return h(node, 'paragraph', child)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Expand Up @@ -3,7 +3,6 @@
"*.js"
],
"exclude": [
"test.js",
"rehype-remark.js",
"*.min.js"
],
Expand All @@ -18,5 +17,7 @@
"checkJs": true,
"declaration": true,
"emitDeclarationOnly": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true
}
}

0 comments on commit 1e3e7d5

Please sign in to comment.