Skip to content

Commit 19e59f5

Browse files
committed
Refactor to use @imports
1 parent 029fcd7 commit 19e59f5

File tree

2 files changed

+32
-47
lines changed

2 files changed

+32
-47
lines changed

lib/index.js

+25-40
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,9 @@
11
/**
2-
* @typedef {import('hast').Element} Element
3-
* @typedef {import('hast').ElementData} ElementData
4-
* @typedef {import('hast').Nodes} Nodes
5-
* @typedef {import('hast').Root} Root
6-
* @typedef {import('hast').RootContent} RootContent
7-
*
8-
* @typedef {import('parse5').DefaultTreeAdapterMap} DefaultTreeAdapterMap
9-
* @typedef {import('parse5').Token.ElementLocation} P5ElementLocation
10-
* @typedef {import('parse5').Token.Location} P5Location
11-
*
12-
* @typedef {import('property-information').Schema} Schema
13-
*
14-
* @typedef {import('unist').Point} Point
15-
* @typedef {import('unist').Position} Position
16-
*
17-
* @typedef {import('vfile').VFile} VFile
18-
*/
19-
20-
/**
21-
* @typedef {DefaultTreeAdapterMap['document']} P5Document
22-
* @typedef {DefaultTreeAdapterMap['documentFragment']} P5DocumentFragment
23-
* @typedef {DefaultTreeAdapterMap['documentType']} P5DocumentType
24-
* @typedef {DefaultTreeAdapterMap['commentNode']} P5Comment
25-
* @typedef {DefaultTreeAdapterMap['textNode']} P5Text
26-
* @typedef {DefaultTreeAdapterMap['element']} P5Element
27-
* @typedef {DefaultTreeAdapterMap['node']} P5Node
28-
* @typedef {DefaultTreeAdapterMap['template']} P5Template
2+
* @import {ElementData, Element, Nodes, RootContent, Root} from 'hast'
3+
* @import {DefaultTreeAdapterMap, Token} from 'parse5'
4+
* @import {Schema} from 'property-information'
5+
* @import {Point, Position} from 'unist'
6+
* @import {VFile} from 'vfile'
297
*/
308

319
/**
@@ -76,7 +54,7 @@ const proto = Object.prototype
7654
/**
7755
* Transform a `parse5` AST to hast.
7856
*
79-
* @param {P5Node} tree
57+
* @param {DefaultTreeAdapterMap['node']} tree
8058
* `parse5` tree to transform.
8159
* @param {Options | null | undefined} [options]
8260
* Configuration (optional).
@@ -102,7 +80,7 @@ export function fromParse5(tree, options) {
10280
*
10381
* @param {State} state
10482
* Info passed around about the current state.
105-
* @param {P5Node} node
83+
* @param {DefaultTreeAdapterMap['node']} node
10684
* p5 node.
10785
* @returns {Nodes}
10886
* hast node.
@@ -113,15 +91,20 @@ function one(state, node) {
11391

11492
switch (node.nodeName) {
11593
case '#comment': {
116-
const reference = /** @type {P5Comment} */ (node)
94+
const reference = /** @type {DefaultTreeAdapterMap['commentNode']} */ (
95+
node
96+
)
11797
result = {type: 'comment', value: reference.data}
11898
patch(state, reference, result)
11999
return result
120100
}
121101

122102
case '#document':
123103
case '#document-fragment': {
124-
const reference = /** @type {P5Document | P5DocumentFragment} */ (node)
104+
const reference =
105+
/** @type {DefaultTreeAdapterMap['document'] | DefaultTreeAdapterMap['documentFragment']} */ (
106+
node
107+
)
125108
const quirksMode =
126109
'mode' in reference
127110
? reference.mode === 'quirks' || reference.mode === 'limited-quirks'
@@ -148,22 +131,24 @@ function one(state, node) {
148131
}
149132

150133
case '#documentType': {
151-
const reference = /** @type {P5DocumentType} */ (node)
134+
const reference = /** @type {DefaultTreeAdapterMap['documentType']} */ (
135+
node
136+
)
152137
result = {type: 'doctype'}
153138
patch(state, reference, result)
154139
return result
155140
}
156141

157142
case '#text': {
158-
const reference = /** @type {P5Text} */ (node)
143+
const reference = /** @type {DefaultTreeAdapterMap['textNode']} */ (node)
159144
result = {type: 'text', value: reference.value}
160145
patch(state, reference, result)
161146
return result
162147
}
163148

164149
// Element.
165150
default: {
166-
const reference = /** @type {P5Element} */ (node)
151+
const reference = /** @type {DefaultTreeAdapterMap['element']} */ (node)
167152
result = element(state, reference)
168153
return result
169154
}
@@ -175,7 +160,7 @@ function one(state, node) {
175160
*
176161
* @param {State} state
177162
* Info passed around about the current state.
178-
* @param {Array<P5Node>} nodes
163+
* @param {Array<DefaultTreeAdapterMap['node']>} nodes
179164
* Nodes.
180165
* @returns {Array<RootContent>}
181166
* hast nodes.
@@ -199,7 +184,7 @@ function all(state, nodes) {
199184
*
200185
* @param {State} state
201186
* Info passed around about the current state.
202-
* @param {P5Element} node
187+
* @param {DefaultTreeAdapterMap['element']} node
203188
* `parse5` node to transform.
204189
* @returns {Element}
205190
* hast node.
@@ -230,7 +215,7 @@ function element(state, node) {
230215

231216
// Switch content.
232217
if (result.tagName === 'template') {
233-
const reference = /** @type {P5Template} */ (node)
218+
const reference = /** @type {DefaultTreeAdapterMap['template']} */ (node)
234219
const pos = reference.sourceCodeLocation
235220
const startTag = pos && pos.startTag && position(pos.startTag)
236221
const endTag = pos && pos.endTag && position(pos.endTag)
@@ -255,7 +240,7 @@ function element(state, node) {
255240
*
256241
* @param {State} state
257242
* Info passed around about the current state.
258-
* @param {P5Node} from
243+
* @param {DefaultTreeAdapterMap['node']} from
259244
* p5 node.
260245
* @param {Nodes} to
261246
* hast node.
@@ -280,7 +265,7 @@ function patch(state, from, to) {
280265
* Info passed around about the current state.
281266
* @param {Nodes} node
282267
* hast node.
283-
* @param {P5ElementLocation} location
268+
* @param {Token.ElementLocation} location
284269
* p5 location info.
285270
* @returns {Position | undefined}
286271
* Position, or nothing.
@@ -337,7 +322,7 @@ function createLocation(state, node, location) {
337322
/**
338323
* Turn a p5 location into a position.
339324
*
340-
* @param {P5Location} loc
325+
* @param {Token.Location} loc
341326
* Location.
342327
* @returns {Position | undefined}
343328
* Position or nothing.

test/index.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
/**
2-
* @typedef {import('hast').Nodes} Nodes
3-
*
4-
* @typedef {import('parse5').html.NS} NS
5-
*
6-
* @typedef {import('vfile').VFile} VFile
2+
* @import {Nodes} from 'hast'
3+
* @import {html as Html} from 'parse5'
4+
* @import {VFile} from 'vfile'
75
*/
86

97
/**
@@ -101,7 +99,7 @@ test('fromParse5', async function (t) {
10199
nodeName: 'title',
102100
tagName: 'title',
103101
attrs: [],
104-
namespaceURI: /** @type {NS} */ ('http://www.w3.org/1999/xhtml'),
102+
namespaceURI: /** @type {Html.NS} */ ('http://www.w3.org/1999/xhtml'),
105103
childNodes: [
106104
{
107105
nodeName: '#text',
@@ -143,7 +141,9 @@ test('fromParse5', async function (t) {
143141
nodeName: 'p',
144142
tagName: 'p',
145143
attrs: [],
146-
namespaceURI: /** @type {NS} */ ('http://www.w3.org/1999/xhtml'),
144+
namespaceURI: /** @type {Html.NS} */ (
145+
'http://www.w3.org/1999/xhtml'
146+
),
147147
childNodes: [
148148
{
149149
nodeName: '#text',

0 commit comments

Comments
 (0)