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 Jul 1, 2024
1 parent fa3d482 commit 95687c8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 32 deletions.
46 changes: 15 additions & 31 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -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<DefaultTreeAdapterMap>} 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'
*/

/**
Expand Down Expand Up @@ -65,7 +49,7 @@ const knownMdxNames = new Set([
'mdxjsEsm'
])

/** @type {ParserOptions} */
/** @type {ParserOptions<DefaultTreeAdapterMap>} */
const parseOptions = {sourceCodeLocationInfo: true, scriptingEnabled: false}

/**
Expand Down Expand Up @@ -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,
Expand All @@ -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',
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -672,7 +656,7 @@ function documentMode(node) {
*
* @param {Nodes | Stitch} node
* hast node.
* @returns {Location}
* @returns {Token.Location}
* `parse5` location.
*/
function createParse5Location(node) {
Expand All @@ -687,7 +671,7 @@ function createParse5Location(node) {
offset: undefined
}

/** @type {Record<keyof Location, number | undefined>} */
/** @type {Record<keyof Token.Location, number | undefined>} */
const location = {
startLine: start.line,
startCol: start.column,
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @typedef {import('./test-types.js')} DoNotTouchAsThisImportIncludesCustomNodesInTree
* @import {} from './test-types.js'
*/

import assert from 'node:assert/strict'
Expand Down

0 comments on commit 95687c8

Please sign in to comment.