diff --git a/lib/index.js b/lib/index.js index ef77f12..7eb78fe 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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' */ /** @@ -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). @@ -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. @@ -113,7 +91,9 @@ 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 @@ -121,7 +101,10 @@ function one(state, node) { 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' @@ -148,14 +131,16 @@ 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 @@ -163,7 +148,7 @@ function one(state, node) { // Element. default: { - const reference = /** @type {P5Element} */ (node) + const reference = /** @type {DefaultTreeAdapterMap['element']} */ (node) result = element(state, reference) return result } @@ -175,7 +160,7 @@ function one(state, node) { * * @param {State} state * Info passed around about the current state. - * @param {Array} nodes + * @param {Array} nodes * Nodes. * @returns {Array} * hast nodes. @@ -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. @@ -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) @@ -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. @@ -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. @@ -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. diff --git a/test/index.js b/test/index.js index 81d164d..8d124be 100644 --- a/test/index.js +++ b/test/index.js @@ -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' */ /** @@ -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', @@ -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',