Skip to content

Commit

Permalink
Update unified, vfile
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 22, 2023
1 parent 7864c11 commit d5ca3cb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 30 deletions.
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @typedef {import('unified').Processor} Processor
* @typedef {import('unified').ProcessCallback} ProcessCallback
* @typedef {import('vfile').BufferEncoding} Encoding
* @typedef {import('vfile').VFileValue} Value
* @typedef {((error?: Error) => void)} Callback
* @typedef {Omit<NodeJS.ReadableStream & NodeJS.WritableStream, 'read'|'setEncoding'|'pause'|'resume'|'isPaused'|'unpipe'|'unshift'|'wrap'>} MinimalDuplex
Expand Down Expand Up @@ -36,14 +35,14 @@ export function stream(processor) {
const write =
/**
* @type {(
* ((value?: Value, encoding?: Encoding, callback?: Callback) => boolean) &
* ((value?: Value, encoding?: string, callback?: Callback) => boolean) &
* ((value: Value, callback?: Callback) => boolean)
* )}
*/
(
/**
* @param {Value} [chunk]
* @param {Encoding} [encoding]
* @param {string} [encoding]
* @param {Callback} [callback]
*/
function (chunk, encoding, callback) {
Expand All @@ -56,6 +55,8 @@ export function stream(processor) {
throw new Error('Did not expect `write` after `end`')
}

// To do: improve writnig, I think there’s a Node textencoder example somewhere.
// @ts-expect-error: `encoding` is fine on buffers.
chunks.push((chunk || '').toString(encoding || 'utf8'))

if (callback) {
Expand Down Expand Up @@ -85,14 +86,14 @@ export function stream(processor) {
const end =
/**
* @type {(
* ((value?: Value, encoding?: Encoding, callback?: Callback) => boolean) &
* ((value?: Value, encoding?: string, callback?: Callback) => boolean) &
* ((value: Value, callback?: Callback) => boolean)
* )}
*/
(
/**
* @param {Value} [chunk]
* @param {Encoding} [encoding]
* @param {string} [encoding]
* @param {Callback} [callback]
*/
function (chunk, encoding, callback) {
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@
"index.js"
],
"dependencies": {
"unified": "^10.0.0",
"vfile": "^5.0.0"
"unified": "^11.0.0",
"vfile": "^6.0.0"
},
"devDependencies": {
"@types/node": "^20.0.0",
"@types/unist": "^3.0.0",
"c8": "^8.0.0",
"prettier": "^3.0.0",
"remark-cli": "^11.0.0",
"remark-preset-wooorm": "^9.0.0",
"type-coverage": "^2.0.0",
"typescript": "^5.0.0",
"vfile-message": "^4.0.2",
"xo": "^0.56.0"
},
"scripts": {
Expand Down
50 changes: 27 additions & 23 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* @typedef {import('unist').Literal} Literal
* @typedef {import('unist').Node} Node
*
* @typedef {import('unified').Processor} Processor
* @typedef {import('unified').Plugin} Plugin
* @typedef {import('unified').Transformer} Transformer
* @typedef {import('unified').ParserFunction} ParserFunction
* @typedef {import('unified').CompilerFunction} CompilerFunction
*
* @typedef {import('vfile-message').VFileMessage} VFileMessage
*/

Expand Down Expand Up @@ -74,9 +74,7 @@ test('stream', async (t) => {

stream(
proc().use(() => {
return transformer
/** @type {Transformer} */
function transformer() {
return function () {
return exception
}
})
Expand All @@ -99,9 +97,7 @@ test('stream', async (t) => {

stream(
proc().use(() => {
return transformer
/** @type {Transformer} */
function transformer(_, file) {
return function (_, file) {
file.message(exception)
}
})
Expand Down Expand Up @@ -194,29 +190,37 @@ test('stream', async (t) => {
})

/**
* @type {Plugin}
* @this {Processor}
* @type {import('unified').Plugin<[], string, Node>}
*/
function parse() {
this.Parser = parser
/** @type {Processor} */
// @ts-expect-error: TS is wrong about `this`.
// eslint-disable-next-line unicorn/no-this-assignment
const self = this

/** @type {ParserFunction} */
self.parser = parser

/** @type {import('unified').Parser<Node>} */
function parser(doc) {
// @ts-expect-error: hush.
return {type: 'text', value: doc}
/** @type {Literal} */
const literal = {type: 'text', value: doc}
return literal
}
}

/**
* @type {Plugin}
* @this {Processor}
* @type {import('unified').Plugin<[], Node, string>}
*/
function stringify() {
this.Compiler = compiler
/** @type {Processor} */
// @ts-expect-error: TS is wrong about `this`.
// eslint-disable-next-line unicorn/no-this-assignment
const self = this

self.compiler = compiler

/** @type {CompilerFunction} */
function compiler(tree) {
// @ts-expect-error: it’s a text node.
return tree.value
/** @type {import('unified').Compiler<Node, string>} */
function compiler(node) {
return 'value' in node ? String(node.value) : ''
}
}

0 comments on commit d5ca3cb

Please sign in to comment.