Skip to content

Commit

Permalink
Refactor to use @imports
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Nov 19, 2024
1 parent 029fcd7 commit 19e59f5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 47 deletions.
65 changes: 25 additions & 40 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,9 @@
/**
* @typedef {import('hast').Element} Element
* @typedef {import('hast').ElementData} ElementData
* @typedef {import('hast').Nodes} Nodes
* @typedef {import('hast').Root} Root
* @typedef {import('hast').RootContent} RootContent
*
* @typedef {import('parse5').DefaultTreeAdapterMap} DefaultTreeAdapterMap
* @typedef {import('parse5').Token.ElementLocation} P5ElementLocation
* @typedef {import('parse5').Token.Location} P5Location
*
* @typedef {import('property-information').Schema} Schema
*
* @typedef {import('unist').Point} Point
* @typedef {import('unist').Position} Position
*
* @typedef {import('vfile').VFile} VFile
*/

/**
* @typedef {DefaultTreeAdapterMap['document']} P5Document
* @typedef {DefaultTreeAdapterMap['documentFragment']} P5DocumentFragment
* @typedef {DefaultTreeAdapterMap['documentType']} P5DocumentType
* @typedef {DefaultTreeAdapterMap['commentNode']} P5Comment
* @typedef {DefaultTreeAdapterMap['textNode']} P5Text
* @typedef {DefaultTreeAdapterMap['element']} P5Element
* @typedef {DefaultTreeAdapterMap['node']} P5Node
* @typedef {DefaultTreeAdapterMap['template']} P5Template
* @import {ElementData, Element, Nodes, RootContent, Root} from 'hast'
* @import {DefaultTreeAdapterMap, Token} from 'parse5'
* @import {Schema} from 'property-information'
* @import {Point, Position} from 'unist'
* @import {VFile} from 'vfile'
*/

/**
Expand Down Expand Up @@ -76,7 +54,7 @@ const proto = Object.prototype
/**
* Transform a `parse5` AST to hast.
*
* @param {P5Node} tree
* @param {DefaultTreeAdapterMap['node']} tree
* `parse5` tree to transform.
* @param {Options | null | undefined} [options]
* Configuration (optional).
Expand All @@ -102,7 +80,7 @@ export function fromParse5(tree, options) {
*
* @param {State} state
* Info passed around about the current state.
* @param {P5Node} node
* @param {DefaultTreeAdapterMap['node']} node
* p5 node.
* @returns {Nodes}
* hast node.
Expand All @@ -113,15 +91,20 @@ function one(state, node) {

switch (node.nodeName) {
case '#comment': {
const reference = /** @type {P5Comment} */ (node)
const reference = /** @type {DefaultTreeAdapterMap['commentNode']} */ (
node
)
result = {type: 'comment', value: reference.data}
patch(state, reference, result)
return result
}

case '#document':
case '#document-fragment': {
const reference = /** @type {P5Document | P5DocumentFragment} */ (node)
const reference =
/** @type {DefaultTreeAdapterMap['document'] | DefaultTreeAdapterMap['documentFragment']} */ (
node
)
const quirksMode =
'mode' in reference
? reference.mode === 'quirks' || reference.mode === 'limited-quirks'
Expand All @@ -148,22 +131,24 @@ function one(state, node) {
}

case '#documentType': {
const reference = /** @type {P5DocumentType} */ (node)
const reference = /** @type {DefaultTreeAdapterMap['documentType']} */ (
node
)
result = {type: 'doctype'}
patch(state, reference, result)
return result
}

case '#text': {
const reference = /** @type {P5Text} */ (node)
const reference = /** @type {DefaultTreeAdapterMap['textNode']} */ (node)
result = {type: 'text', value: reference.value}
patch(state, reference, result)
return result
}

// Element.
default: {
const reference = /** @type {P5Element} */ (node)
const reference = /** @type {DefaultTreeAdapterMap['element']} */ (node)
result = element(state, reference)
return result
}
Expand All @@ -175,7 +160,7 @@ function one(state, node) {
*
* @param {State} state
* Info passed around about the current state.
* @param {Array<P5Node>} nodes
* @param {Array<DefaultTreeAdapterMap['node']>} nodes
* Nodes.
* @returns {Array<RootContent>}
* hast nodes.
Expand All @@ -199,7 +184,7 @@ function all(state, nodes) {
*
* @param {State} state
* Info passed around about the current state.
* @param {P5Element} node
* @param {DefaultTreeAdapterMap['element']} node
* `parse5` node to transform.
* @returns {Element}
* hast node.
Expand Down Expand Up @@ -230,7 +215,7 @@ function element(state, node) {

// Switch content.
if (result.tagName === 'template') {
const reference = /** @type {P5Template} */ (node)
const reference = /** @type {DefaultTreeAdapterMap['template']} */ (node)
const pos = reference.sourceCodeLocation
const startTag = pos && pos.startTag && position(pos.startTag)
const endTag = pos && pos.endTag && position(pos.endTag)
Expand All @@ -255,7 +240,7 @@ function element(state, node) {
*
* @param {State} state
* Info passed around about the current state.
* @param {P5Node} from
* @param {DefaultTreeAdapterMap['node']} from
* p5 node.
* @param {Nodes} to
* hast node.
Expand All @@ -280,7 +265,7 @@ function patch(state, from, to) {
* Info passed around about the current state.
* @param {Nodes} node
* hast node.
* @param {P5ElementLocation} location
* @param {Token.ElementLocation} location
* p5 location info.
* @returns {Position | undefined}
* Position, or nothing.
Expand Down Expand Up @@ -337,7 +322,7 @@ function createLocation(state, node, location) {
/**
* Turn a p5 location into a position.
*
* @param {P5Location} loc
* @param {Token.Location} loc
* Location.
* @returns {Position | undefined}
* Position or nothing.
Expand Down
14 changes: 7 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/**
* @typedef {import('hast').Nodes} Nodes
*
* @typedef {import('parse5').html.NS} NS
*
* @typedef {import('vfile').VFile} VFile
* @import {Nodes} from 'hast'
* @import {html as Html} from 'parse5'
* @import {VFile} from 'vfile'
*/

/**
Expand Down Expand Up @@ -101,7 +99,7 @@ test('fromParse5', async function (t) {
nodeName: 'title',
tagName: 'title',
attrs: [],
namespaceURI: /** @type {NS} */ ('http://www.w3.org/1999/xhtml'),
namespaceURI: /** @type {Html.NS} */ ('http://www.w3.org/1999/xhtml'),
childNodes: [
{
nodeName: '#text',
Expand Down Expand Up @@ -143,7 +141,9 @@ test('fromParse5', async function (t) {
nodeName: 'p',
tagName: 'p',
attrs: [],
namespaceURI: /** @type {NS} */ ('http://www.w3.org/1999/xhtml'),
namespaceURI: /** @type {Html.NS} */ (
'http://www.w3.org/1999/xhtml'
),
childNodes: [
{
nodeName: '#text',
Expand Down

0 comments on commit 19e59f5

Please sign in to comment.