From 95687c80cad05442c38cfa97256eaa76276a08e8 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Mon, 1 Jul 2024 11:55:39 +0200 Subject: [PATCH] Refactor to use `@import`s --- lib/index.js | 46 +++++++++++++++------------------------------- test.js | 2 +- 2 files changed, 16 insertions(+), 32 deletions(-) diff --git a/lib/index.js b/lib/index.js index 2d8bc4c..6fd7057 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,25 +1,9 @@ /** - * @typedef {import('hast').Comment} Comment - * @typedef {import('hast').Doctype} Doctype - * @typedef {import('hast').Element} Element - * @typedef {import('hast').Nodes} Nodes - * @typedef {import('hast').Root} Root - * @typedef {import('hast').RootContent} RootContent - * @typedef {import('hast').Text} Text - * - * @typedef {import('hast-util-raw').Options} Options - * - * @typedef {import('mdast-util-to-hast').Raw} Raw - * - * @typedef {import('parse5').DefaultTreeAdapterMap} DefaultTreeAdapterMap - * @typedef {import('parse5').ParserOptions} ParserOptions - * @typedef {import('parse5').Token.CharacterToken} CharacterToken - * @typedef {import('parse5').Token.CommentToken} CommentToken - * @typedef {import('parse5').Token.DoctypeToken} DoctypeToken - * @typedef {import('parse5').Token.Location} Location - * @typedef {import('parse5').Token.TagToken} TagToken - * - * @typedef {import('unist').Point} Point + * @import {Comment, Doctype, Element, Nodes, RootContent, Root, Text} from 'hast' + * @import {Options} from 'hast-util-raw' + * @import {Raw} from 'mdast-util-to-hast' + * @import {DefaultTreeAdapterMap, ParserOptions} from 'parse5' + * @import {Point} from 'unist' */ /** @@ -65,7 +49,7 @@ const knownMdxNames = new Set([ 'mdxjsEsm' ]) -/** @type {ParserOptions} */ +/** @type {ParserOptions} */ const parseOptions = {sourceCodeLocationInfo: true, scriptingEnabled: false} /** @@ -206,7 +190,7 @@ function text(node, state) { state.parser.tokenizer.state = 0 } - /** @type {CharacterToken} */ + /** @type {Token.CharacterToken} */ const token = { type: Token.TokenType.CHARACTER, chars: node.value, @@ -233,7 +217,7 @@ function text(node, state) { * Nothing. */ function doctype(node, state) { - /** @type {DoctypeToken} */ + /** @type {Token.DoctypeToken} */ const token = { type: Token.TokenType.DOCTYPE, name: 'html', @@ -300,7 +284,7 @@ function comment(node, state) { // @ts-expect-error: we pass stitches through. const data = node.value - /** @type {CommentToken} */ + /** @type {Token.CommentToken} */ const token = { type: Token.TokenType.COMMENT, data, @@ -427,7 +411,7 @@ function resetTokenizer(state, point) { setPoint(state, point) // Process final characters if they’re still there after hibernating. - /** @type {CharacterToken} */ + /** @type {Token.CharacterToken} */ // @ts-expect-error: private. // type-coverage:ignore-next-line const token = state.parser.tokenizer.currentCharacterToken @@ -496,7 +480,7 @@ function resetTokenizer(state, point) { */ function setPoint(state, point) { if (point && point.offset !== undefined) { - /** @type {Location} */ + /** @type {Token.Location} */ const location = { startLine: point.line, startCol: point.column, @@ -548,7 +532,7 @@ function startTag(node, state) { {space: ns === webNamespaces.svg ? 'svg' : 'html'} ) - /** @type {TagToken} */ + /** @type {Token.TagToken} */ const tag = { type: Token.TokenType.START_TAG, tagName, @@ -608,7 +592,7 @@ function endTag(node, state) { resetTokenizer(state, pointEnd(node)) - /** @type {TagToken} */ + /** @type {Token.TagToken} */ const tag = { type: Token.TokenType.END_TAG, tagName, @@ -672,7 +656,7 @@ function documentMode(node) { * * @param {Nodes | Stitch} node * hast node. - * @returns {Location} + * @returns {Token.Location} * `parse5` location. */ function createParse5Location(node) { @@ -687,7 +671,7 @@ function createParse5Location(node) { offset: undefined } - /** @type {Record} */ + /** @type {Record} */ const location = { startLine: start.line, startCol: start.column, diff --git a/test.js b/test.js index 48f6798..cfce772 100644 --- a/test.js +++ b/test.js @@ -1,5 +1,5 @@ /** - * @typedef {import('./test-types.js')} DoNotTouchAsThisImportIncludesCustomNodesInTree + * @import {} from './test-types.js' */ import assert from 'node:assert/strict'