Skip to content

Commit 0c022bd

Browse files
committed
Refactor code-style
1 parent 249e704 commit 0c022bd

File tree

5 files changed

+924
-751
lines changed

5 files changed

+924
-751
lines changed

lib/index.js

Lines changed: 79 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,66 @@
11
/**
2+
* @typedef {import('hast').Comment} Comment
3+
* @typedef {import('hast').Doctype} Doctype
4+
* @typedef {import('hast').Element} Element
5+
* @typedef {import('hast').Nodes} Nodes
6+
* @typedef {import('hast').Root} Root
7+
* @typedef {import('hast').RootContent} RootContent
8+
* @typedef {import('hast').Text} Text
9+
*
10+
* @typedef {import('mdast-util-to-hast').Raw} Raw
11+
*
212
* @typedef {import('parse5').DefaultTreeAdapterMap} DefaultTreeAdapterMap
13+
* @typedef {import('parse5').ParserOptions<DefaultTreeAdapterMap>} ParserOptions
314
* @typedef {import('parse5').Token.CharacterToken} CharacterToken
415
* @typedef {import('parse5').Token.CommentToken} CommentToken
516
* @typedef {import('parse5').Token.DoctypeToken} DoctypeToken
6-
* @typedef {import('parse5').Token.TagToken} TagToken
717
* @typedef {import('parse5').Token.Location} Location
8-
* @typedef {import('parse5').ParserOptions<DefaultTreeAdapterMap>} ParserOptions
9-
*
10-
* @typedef {import('vfile').VFile} VFile
18+
* @typedef {import('parse5').Token.TagToken} TagToken
1119
*
1220
* @typedef {import('unist').Point} Point
1321
*
14-
* @typedef {import('hast').Root} Root
15-
* @typedef {import('hast').Doctype} Doctype
16-
* @typedef {import('hast').Element} Element
17-
* @typedef {import('hast').Text} Text
18-
* @typedef {import('hast').Comment} Comment
19-
* @typedef {import('hast').RootContent} RootContent
20-
* @typedef {import('hast').Nodes} Nodes
21-
*
22-
* @typedef {import('mdast-util-to-hast').Raw} Raw
22+
* @typedef {import('vfile').VFile} VFile
2323
*/
2424

2525
/**
26-
* @typedef {{type: 'comment', value: {stitch: Nodes}}} Stitch
27-
*
2826
* @typedef Options
2927
* Configuration.
28+
* @property {VFile | null | undefined} [file]
29+
* Corresponding virtual file representing the input document (optional).
3030
* @property {Array<string> | null | undefined} [passThrough]
31-
* List of custom hast node types to pass through (keep).
31+
* List of custom hast node types to pass through (as in, keep) (optional).
3232
*
3333
* If the passed through nodes have children, those children are expected to
3434
* be hast again and will be handled.
35-
* @property {VFile | null | undefined} [file]
36-
* Corresponding virtual file representing the input document.
3735
*
3836
* @typedef State
3937
* Info passed around about the current state.
38+
* @property {(node: Nodes) => undefined} handle
39+
* Add a hast node to the parser.
40+
* @property {Options} options
41+
* User configuration.
4042
* @property {Parser<DefaultTreeAdapterMap>} parser
4143
* Current parser.
42-
* @property {(node: Nodes) => void} handle
43-
* Add a hast node to the parser.
4444
* @property {boolean} stitches
4545
* Whether there are stitches.
46-
* @property {Options} options
47-
* User configuration.
46+
*
47+
* @typedef {{type: 'comment', value: {stitch: Nodes}}} Stitch
48+
* Custom comment-like value we pass through parse5, which contains a
49+
* replacement node that we’ll swap back in afterwards.
4850
*/
4951

5052
import extend from 'extend'
5153
import {fromParse5} from 'hast-util-from-parse5'
5254
import {toParse5} from 'hast-util-to-parse5'
5355
import {htmlVoidElements} from 'html-void-elements'
5456
import {Parser, Token, TokenizerMode, html} from 'parse5'
55-
import {pointStart, pointEnd} from 'unist-util-position'
57+
import {pointEnd, pointStart} from 'unist-util-position'
5658
import {visit} from 'unist-util-visit'
57-
import {zwitch} from 'zwitch'
5859
import {webNamespaces} from 'web-namespaces'
60+
import {zwitch} from 'zwitch'
5961

6062
// Node types associated with MDX.
61-
// <https://github.com/mdx-js/mdx/blob/641eb91/packages/mdx/lib/node-types.js>
63+
// <https://github.com/mdx-js/mdx/blob/8a56312/packages/mdx/lib/node-types.js>
6264
const knownMdxNames = new Set([
6365
'mdxFlowExpression',
6466
'mdxJsxFlowElement',
@@ -77,13 +79,13 @@ const parseOptions = {sourceCodeLocationInfo: true, scriptingEnabled: false}
7779
* @param {Nodes} tree
7880
* Original hast tree to transform.
7981
* @param {Options | null | undefined} [options]
80-
* Configuration.
82+
* Configuration (optional).
8183
* @returns {Nodes}
8284
* Parsed again tree.
8385
*/
8486
export function raw(tree, options) {
8587
const document = documentMode(tree)
86-
/** @type {(node: Nodes, state: State) => void} */
88+
/** @type {(node: Nodes, state: State) => undefined} */
8789
const one = zwitch('type', {
8890
handlers: {root, element, text, comment, doctype, raw: handleRaw},
8991
unknown
@@ -93,7 +95,7 @@ export function raw(tree, options) {
9395
const state = {
9496
parser: document
9597
? new Parser(parseOptions)
96-
: Parser.getFragmentParser(null, parseOptions),
98+
: Parser.getFragmentParser(undefined, parseOptions),
9799
handle(node) {
98100
one(node, state)
99101
},
@@ -111,11 +113,13 @@ export function raw(tree, options) {
111113
})
112114

113115
if (state.stitches) {
114-
visit(result, 'comment', (node, index, parent) => {
116+
visit(result, 'comment', function (node, index, parent) {
115117
const stitch = /** @type {Stitch} */ (/** @type {unknown} */ (node))
116-
if (stitch.value.stitch && parent !== null && index !== null) {
118+
if (stitch.value.stitch && parent && index !== undefined) {
119+
/** @type {Array<RootContent>} */
120+
const siblings = parent.children
117121
// @ts-expect-error: assume the stitch is allowed.
118-
parent.children[index] = stitch.value.stitch
122+
siblings[index] = stitch.value.stitch
119123
return index
120124
}
121125
})
@@ -140,7 +144,7 @@ export function raw(tree, options) {
140144
* hast content.
141145
* @param {State} state
142146
* Info passed around about the current state.
143-
* @returns {void}
147+
* @returns {undefined}
144148
* Nothing.
145149
*/
146150
function all(nodes, state) {
@@ -161,7 +165,7 @@ function all(nodes, state) {
161165
* hast root node.
162166
* @param {State} state
163167
* Info passed around about the current state.
164-
* @returns {void}
168+
* @returns {undefined}
165169
* Nothing.
166170
*/
167171
function root(node, state) {
@@ -175,7 +179,7 @@ function root(node, state) {
175179
* hast element node.
176180
* @param {State} state
177181
* Info passed around about the current state.
178-
* @returns {void}
182+
* @returns {undefined}
179183
* Nothing.
180184
*/
181185
function element(node, state) {
@@ -193,7 +197,7 @@ function element(node, state) {
193197
* hast text node.
194198
* @param {State} state
195199
* Info passed around about the current state.
196-
* @returns {void}
200+
* @returns {undefined}
197201
* Nothing.
198202
*/
199203
function text(node, state) {
@@ -220,7 +224,7 @@ function text(node, state) {
220224
* hast doctype node.
221225
* @param {State} state
222226
* Info passed around about the current state.
223-
* @returns {void}
227+
* @returns {undefined}
224228
* Nothing.
225229
*/
226230
function doctype(node, state) {
@@ -250,7 +254,7 @@ function doctype(node, state) {
250254
* unknown node.
251255
* @param {State} state
252256
* Info passed around about the current state.
253-
* @returns {void}
257+
* @returns {undefined}
254258
* Nothing.
255259
*/
256260
function stitch(node, state) {
@@ -263,8 +267,10 @@ function stitch(node, state) {
263267
// Recurse, because to somewhat handle `[<x>]</x>` (where `[]` denotes the
264268
// passed through node).
265269
if ('children' in node && 'children' in clone) {
266-
const fakeRoot = raw({type: 'root', children: node.children}, state.options)
267-
// @ts-expect-error Assume a given parent yields a parent.
270+
// Root in root out.
271+
const fakeRoot = /** @type {Root} */ (
272+
raw({type: 'root', children: node.children}, state.options)
273+
)
268274
clone.children = fakeRoot.children
269275
}
270276

@@ -281,12 +287,12 @@ function stitch(node, state) {
281287
* hast comment node.
282288
* @param {State} state
283289
* Info passed around about the current state.
284-
* @returns {void}
290+
* @returns {undefined}
285291
* Nothing.
286292
*/
287293
function comment(node, state) {
288294
/** @type {string} */
289-
// @ts-expect-error: yeah, we’re passing stiches through.
295+
// @ts-expect-error: we pass stitches through.
290296
const data = node.value
291297

292298
/** @type {CommentToken} */
@@ -311,12 +317,12 @@ function comment(node, state) {
311317
* hast raw node.
312318
* @param {State} state
313319
* Info passed around about the current state.
314-
* @returns {void}
320+
* @returns {undefined}
315321
* Nothing.
316322
*/
317323
function handleRaw(node, state) {
318324
// Reset preprocessor:
319-
// See: <https://github.com/inikulin/parse5/blob/8e22fe4/packages/parse5/lib/tokenizer/preprocessor.ts#L18-L31>.
325+
// See: <https://github.com/inikulin/parse5/blob/6f7ca60/packages/parse5/lib/tokenizer/preprocessor.ts#L18-L31>.
320326
state.parser.tokenizer.preprocessor.html = ''
321327
// @ts-expect-error: private.
322328
// type-coverage:ignore-next-line
@@ -353,6 +359,8 @@ function handleRaw(node, state) {
353359

354360
// Note: `State` is not exposed by `parse5`, so these numbers are fragile.
355361
// See: <https://github.com/inikulin/parse5/blob/46cba43/packages/parse5/lib/tokenizer/index.ts#L58>
362+
// Note: a change to `parse5`, which breaks this, was merged but not released.
363+
// Investigate when it is.
356364
if (
357365
state.parser.tokenizer.state === 72 /* NAMED_CHARACTER_REFERENCE */ ||
358366
state.parser.tokenizer.state === 78 /* NUMERIC_CHARACTER_REFERENCE_END */
@@ -377,7 +385,7 @@ function handleRaw(node, state) {
377385
* unknown node.
378386
* @param {State} state
379387
* Info passed around about the current state.
380-
* @returns {void}
388+
* @returns {undefined}
381389
* Never.
382390
*/
383391
function unknown(node_, state) {
@@ -407,7 +415,7 @@ function unknown(node_, state) {
407415
* Info passed around about the current state.
408416
* @param {Point | undefined} point
409417
* Point.
410-
* @returns {void}
418+
* @returns {undefined}
411419
* Nothing.
412420
*/
413421
function resetTokenizer(state, point) {
@@ -480,7 +488,7 @@ function resetTokenizer(state, point) {
480488
* Info passed around about the current state.
481489
* @param {Point | undefined} point
482490
* Point.
483-
* @returns {void}
491+
* @returns {undefined}
484492
* Nothing.
485493
*/
486494
function setPoint(state, point) {
@@ -513,7 +521,7 @@ function setPoint(state, point) {
513521
* Element.
514522
* @param {State} state
515523
* Info passed around about the current state.
516-
* @returns {void}
524+
* @returns {undefined}
517525
* Nothing.
518526
*/
519527
function startTag(node, state) {
@@ -578,7 +586,7 @@ function startTag(node, state) {
578586
* Element.
579587
* @param {State} state
580588
* Info passed around about the current state.
581-
* @returns {void}
589+
* @returns {undefined}
582590
* Nothing.
583591
*/
584592
function endTag(node, state) {
@@ -663,23 +671,30 @@ function documentMode(node) {
663671
* `parse5` location.
664672
*/
665673
function createParse5Location(node) {
666-
const start = pointStart(node)
667-
const end = pointEnd(node)
668-
669-
return {
670-
// @ts-expect-error: could be `undefined` in hast, which `parse5` types don’t want.
671-
startLine: start?.line,
672-
// @ts-expect-error: could be `undefined` in hast, which `parse5` types don’t want.
673-
startCol: start?.column,
674-
// @ts-expect-error: could be `undefined` in hast, which `parse5` types don’t want.
675-
startOffset: start?.offset,
676-
// @ts-expect-error: could be `undefined` in hast, which `parse5` types don’t want.
677-
endLine: end?.line,
678-
// @ts-expect-error: could be `undefined` in hast, which `parse5` types don’t want.
679-
endCol: end?.column,
680-
// @ts-expect-error: could be `undefined` in hast, which `parse5` types don’t want.
681-
endOffset: end?.offset
674+
const start = pointStart(node) || {
675+
line: undefined,
676+
column: undefined,
677+
offset: undefined
678+
}
679+
const end = pointEnd(node) || {
680+
line: undefined,
681+
column: undefined,
682+
offset: undefined
682683
}
684+
685+
/** @type {Record<keyof Location, number | undefined>} */
686+
const location = {
687+
startLine: start.line,
688+
startCol: start.column,
689+
startOffset: start.offset,
690+
endLine: end.line,
691+
endCol: end.column,
692+
endOffset: end.offset
693+
}
694+
695+
// @ts-expect-error: unist point values can be `undefined` in hast, which
696+
// `parse5` types don’t want.
697+
return location
683698
}
684699

685700
/**

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@
8989
"strict": true
9090
},
9191
"xo": {
92+
"overrides": [
93+
{
94+
"files": [
95+
"**/*.ts"
96+
],
97+
"rules": {
98+
"@typescript-eslint/consistent-type-definitions": "off"
99+
}
100+
}
101+
],
92102
"prettier": true
93103
}
94104
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ import {visit} from 'unist-util-visit'
181181
/** @type {import('hast').Root} */
182182
const tree = getHastNodeSomeHow()
183183

184-
visit(tree, (node) => {
184+
visit(tree, function (node) {
185185
// `node` can now be a `raw` node.
186186
})
187187
```

test-types.d.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import type {Parent, Literal} from 'hast'
22

3-
/* eslint-disable @typescript-eslint/consistent-type-definitions */
4-
53
export interface CustomParent extends Parent {
64
type: 'customParent'
75
}
@@ -26,5 +24,3 @@ declare module 'hast' {
2624
customParent: CustomParent
2725
}
2826
}
29-
30-
/* eslint-enable @typescript-eslint/consistent-type-definitions */

0 commit comments

Comments
 (0)