Skip to content

Commit c1d8ef2

Browse files
committed
Refactor to use @imports
1 parent d4bfe9d commit c1d8ef2

File tree

10 files changed

+92
-123
lines changed

10 files changed

+92
-123
lines changed

lib/handlers/comment.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
/**
2-
* @typedef {import('estree').Comment} Comment
3-
*
4-
* @typedef {import('estree-jsx').JSXEmptyExpression} JsxEmptyExpression
5-
* @typedef {import('estree-jsx').JSXExpressionContainer} JsxExpressionContainer
6-
*
7-
* @typedef {import('hast').Comment} HastComment
8-
*
9-
* @typedef {import('../state.js').State} State
2+
* @import {
3+
* JSXEmptyExpression as JsxEmptyExpression,
4+
* JSXExpressionContainer as JsxExpressionContainer,
5+
* } from 'estree-jsx'
6+
* @import {Comment} from 'estree'
7+
* @import {State} from 'hast-util-to-estree'
8+
* @import {Comment as HastComment} from 'hast'
109
*/
1110

1211
// Make VS Code show references to the above types.

lib/handlers/element.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
/**
2-
* @typedef {import('estree').Property} Property
3-
*
4-
* @typedef {import('estree-jsx').JSXAttribute} JsxAttribute
5-
* @typedef {import('estree-jsx').JSXElement} JsxElement
6-
* @typedef {import('estree-jsx').JSXSpreadAttribute} JsxSpreadAttribute
7-
*
8-
* @typedef {import('hast').Element} HastElement
9-
*
10-
* @typedef {import('../state.js').State} State
11-
*/
12-
13-
/**
14-
* @typedef {Record<string, string>} Style
2+
* @import {
3+
* JSXAttribute as JsxAttribute,
4+
* JSXElement as JsxElement,
5+
* JSXSpreadAttribute as JsxSpreadAttribute,
6+
* } from 'estree-jsx'
7+
* @import {Property} from 'estree'
8+
* @import {State} from 'hast-util-to-estree'
9+
* @import {Element as HastElement} from 'hast'
1510
*/
1611

1712
import {stringify as commas} from 'comma-separated-tokens'
@@ -231,11 +226,11 @@ export function element(node, state) {
231226
* CSS text.
232227
* @param {string} tagName
233228
* Element name.
234-
* @returns {Style}
229+
* @returns {Record<string, string>}
235230
* Properties.
236231
*/
237232
function parseStyle(value, tagName) {
238-
/** @type {Style} */
233+
/** @type {Record<string, string>} */
239234
const result = {}
240235

241236
try {
@@ -277,11 +272,11 @@ function parseStyle(value, tagName) {
277272
/**
278273
* Transform a DOM casing style object to a CSS casing style object.
279274
*
280-
* @param {Style} domCasing
281-
* @returns {Style}
275+
* @param {Record<string, string>} domCasing
276+
* @returns {Record<string, string>}
282277
*/
283278
function transformStylesToCssCasing(domCasing) {
284-
/** @type {Style} */
279+
/** @type {Record<string, string>} */
285280
const cssCasing = {}
286281
/** @type {string} */
287282
let from

lib/handlers/mdx-expression.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
/**
2-
* @typedef {import('estree').Expression} Expression
3-
*
4-
* @typedef {import('estree-jsx').JSXEmptyExpression} JsxEmptyExpression
5-
* @typedef {import('estree-jsx').JSXExpressionContainer} JsxExpressionContainer
6-
*
7-
* @typedef {import('mdast-util-mdx-expression').MdxFlowExpressionHast} MdxFlowExpression
8-
* @typedef {import('mdast-util-mdx-expression').MdxTextExpressionHast} MdxTextExpression
9-
*
10-
* @typedef {import('../state.js').State} State
2+
* @import {
3+
* JSXEmptyExpression as JsxEmptyExpression,
4+
* JSXExpressionContainer as JsxExpressionContainer
5+
* } from 'estree-jsx'
6+
* @import {Expression} from 'estree'
7+
* @import {
8+
* MdxFlowExpressionHast as MdxFlowExpression,
9+
* MdxTextExpressionHast as MdxTextExpression
10+
* } from 'mdast-util-mdx-expression'
11+
* @import {State} from 'hast-util-to-estree'
1112
*/
1213

1314
import {attachComments} from 'estree-util-attach-comments'

lib/handlers/mdx-jsx-element.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
/**
2-
* @typedef {import('estree').Expression} Expression
3-
*
4-
* @typedef {import('estree-jsx').JSXAttribute} JsxAttribute
5-
* @typedef {import('estree-jsx').JSXElement} JsxElement
6-
* @typedef {import('estree-jsx').JSXFragment} JsxFragment
7-
* @typedef {import('estree-jsx').JSXSpreadAttribute} JsxSpreadAttribute
8-
*
9-
* @typedef {import('mdast-util-mdx-jsx').MdxJsxFlowElementHast} MdxJsxFlowElement
10-
* @typedef {import('mdast-util-mdx-jsx').MdxJsxTextElementHast} MdxJsxTextElement
11-
*
12-
* @typedef {import('../state.js').State} State
2+
* @import {
3+
* JSXAttribute as JsxAttribute,
4+
* JSXElement as JsxElement,
5+
* JSXFragment as JsxFragment,
6+
* JSXSpreadAttribute as JsxSpreadAttribute
7+
* } from 'estree-jsx'
8+
* @import {Expression} from 'estree'
9+
* @import {State} from 'hast-util-to-estree'
10+
* @import {
11+
* MdxJsxFlowElementHast as MdxJsxFlowElement,
12+
* MdxJsxTextElementHast as MdxJsxTextElement
13+
* } from 'mdast-util-mdx-jsx'
1314
*/
1415

1516
import {attachComments} from 'estree-util-attach-comments'

lib/handlers/mdxjs-esm.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/**
2-
* @typedef {import('mdast-util-mdxjs-esm').MdxjsEsmHast} MdxjsEsm
3-
*
4-
* @typedef {import('../state.js').State} State
2+
* @import {MdxjsEsmHast as MdxjsEsm} from 'mdast-util-mdxjs-esm'
3+
* @import {State} from 'hast-util-to-estree'
54
*/
65

76
import {attachComments} from 'estree-util-attach-comments'

lib/handlers/root.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
2-
* @typedef {import('estree-jsx').JSXFragment} JsxFragment
3-
*
4-
* @typedef {import('hast').Root} HastRoot
5-
*
6-
* @typedef {import('../state.js').State} State
7-
*/
8-
9-
/**
10-
* @typedef {JsxFragment['children'][number]} JsxChild
2+
* @import {
3+
* JSXElement as JsxElement,
4+
* JSXExpressionContainer as JsxExpressionContainer,
5+
* JSXFragment as JsxFragment,
6+
* JSXSpreadChild as JsxSpreadChild,
7+
* JSXText as JsxText,
8+
* } from 'estree-jsx'
9+
* @import {State} from 'hast-util-to-estree'
10+
* @import {Root as HastRoot} from 'hast'
1111
*/
1212

1313
import {whitespace} from 'hast-util-whitespace'
@@ -24,10 +24,10 @@ import {whitespace} from 'hast-util-whitespace'
2424
*/
2525
export function root(node, state) {
2626
const children = state.all(node)
27-
/** @type {Array<JsxChild>} */
27+
/** @type {Array<JsxElement | JsxExpressionContainer | JsxFragment | JsxSpreadChild | JsxText>} */
2828
const cleanChildren = []
2929
let index = -1
30-
/** @type {Array<JsxChild> | undefined} */
30+
/** @type {Array<JsxElement | JsxExpressionContainer | JsxFragment | JsxSpreadChild | JsxText> | undefined} */
3131
let queue
3232

3333
// Remove surrounding whitespace nodes from the fragment.

lib/handlers/text.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
/**
2-
* @typedef {import('estree').Literal} Literal
3-
*
4-
* @typedef {import('estree-jsx').JSXExpressionContainer} JsxExpressionContainer
5-
*
6-
* @typedef {import('hast').Text} HastText
7-
*
8-
* @typedef {import('../state.js').State} State
2+
* @import {JSXExpressionContainer as JsxExpressionContainer} from 'estree-jsx'
3+
* @import {Literal} from 'estree'
4+
* @import {State} from 'hast-util-to-estree'
5+
* @import {Text as HastText} from 'hast'
96
*/
107

118
// Make VS Code show references to the above types.

lib/index.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
/**
2-
* Register MDX nodes in tree:
3-
*
4-
* @typedef {import('mdast-util-mdx-expression')}
5-
* @typedef {import('mdast-util-mdx-jsx')}
6-
* @typedef {import('mdast-util-mdxjs-esm')}
7-
*/
8-
9-
/**
10-
* @typedef {import('estree').ExpressionStatement} ExpressionStatement
11-
* @typedef {import('estree').Program} Program
12-
*
13-
* @typedef {import('hast').Nodes} HastNodes
14-
*
15-
* @typedef {import('./state.js').Options} Options
2+
* @import {} from 'mdast-util-mdx-expression'
3+
* @import {} from 'mdast-util-mdx-jsx'
4+
* @import {} from 'mdast-util-mdxjs-esm'
5+
* @import {ExpressionStatement, Program} from 'estree'
6+
* @import {Options} from 'hast-util-to-estree'
7+
* @import {Nodes as HastNodes} from 'hast'
168
*/
179

1810
import {createState} from './state.js'

lib/state.js

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,18 @@
11
/**
2-
* @typedef {import('estree').Comment} Comment
3-
* @typedef {import('estree').Directive} Directive
4-
* @typedef {import('estree').ModuleDeclaration} ModuleDeclaration
5-
* @typedef {import('estree').Node} EstreeNode
6-
* @typedef {import('estree').Statement} Statement
7-
*
8-
* @typedef {import('estree-jsx').JSXAttribute} JsxAttribute
9-
* @typedef {import('estree-jsx').JSXElement} JsxElement
10-
* @typedef {import('estree-jsx').JSXIdentifier} JsxIdentifier
11-
* @typedef {import('estree-jsx').JSXMemberExpression} JsxMemberExpression
12-
* @typedef {import('estree-jsx').JSXNamespacedName} JsxNamespacedName
13-
*
14-
* @typedef {import('mdast-util-mdx-jsx').MdxJsxAttribute} MdxJsxAttribute
15-
* @typedef {import('mdast-util-mdx-jsx').MdxJsxAttributeValueExpression} MdxJsxAttributeValueExpression
16-
* @typedef {import('mdast-util-mdx-jsx').MdxJsxExpressionAttribute} MdxJsxExpressionAttribute
17-
*
18-
* @typedef {import('hast').Nodes} HastNodes
19-
* @typedef {import('hast').Parents} HastParents
20-
*
21-
* @typedef {import('property-information').Schema} Schema
22-
*/
23-
24-
/**
25-
* @typedef {JsxElement['openingElement']['name']} JsxElementName
26-
* @typedef {JsxAttribute['name']} JsxAttributeName
27-
* @typedef {JsxElement['children'][number]} JsxChild
2+
* @import {
3+
* JSXElement as JsxElement,
4+
* JSXExpressionContainer as JsxExpressionContainer,
5+
* JSXFragment as JsxFragment,
6+
* JSXIdentifier as JsxIdentifier,
7+
* JSXMemberExpression as JsxMemberExpression,
8+
* JSXNamespacedName as JsxNamespacedName,
9+
* JSXSpreadChild as JsxSpreadChild,
10+
* JSXText as JsxText,
11+
* } from 'estree-jsx'
12+
* @import {Comment, Directive, ModuleDeclaration, Node as EstreeNode, Statement} from 'estree'
13+
* @import {MdxJsxAttribute, MdxJsxAttributeValueExpression, MdxJsxExpressionAttribute} from 'mdast-util-mdx-jsx'
14+
* @import {Nodes as HastNodes, Parents as HastParents} from 'hast'
15+
* @import {Schema} from 'property-information'
2816
*/
2917

3018
/**
@@ -40,7 +28,7 @@
4028
* Expected hast node.
4129
* @param {State} state
4230
* Info passed around about the current state.
43-
* @returns {JsxChild | null | undefined}
31+
* @returns {JsxElement | JsxExpressionContainer | JsxFragment | JsxSpreadChild | JsxText | null | undefined}
4432
* estree node.
4533
*
4634
* @typedef Options
@@ -78,19 +66,19 @@
7866
*
7967
* @typedef State
8068
* Info passed around about the current state.
81-
* @property {(parent: HastParents) => Array<JsxChild>} all
69+
* @property {(parent: HastParents) => Array<JsxElement | JsxExpressionContainer | JsxFragment | JsxSpreadChild | JsxText>} all
8270
* Transform children of a hast parent to estree.
8371
* @property {Array<Comment>} comments
8472
* List of estree comments.
85-
* @property {(name: string) => JsxAttributeName} createJsxAttributeName
73+
* @property {(name: string) => JsxIdentifier | JsxNamespacedName} createJsxAttributeName
8674
* Create a JSX attribute name.
87-
* @property {(name: string) => JsxElementName} createJsxElementName
75+
* @property {(name: string) => JsxIdentifier | JsxMemberExpression | JsxNamespacedName} createJsxElementName
8876
* Create a JSX element name.
8977
* @property {ElementAttributeNameCase} elementAttributeNameCase
9078
* Casing to use for attribute names.
9179
* @property {Array<Directive | ModuleDeclaration | Statement>} esm
9280
* List of top-level estree nodes.
93-
* @property {(node: any) => JsxChild | null | undefined} handle
81+
* @property {(node: any) => JsxElement | JsxExpressionContainer | JsxFragment | JsxSpreadChild | JsxText | null | undefined} handle
9482
* Transform a hast node to estree.
9583
* @property {(from: HastNodes | MdxJsxAttribute | MdxJsxAttributeValueExpression | MdxJsxExpressionAttribute, to: Comment | EstreeNode) => undefined} inherit
9684
* Take positional info and data from `from` (use `patch` if you don’t want data).
@@ -164,7 +152,7 @@ export function createState(options) {
164152
/**
165153
* @this {State}
166154
* @param {any} node
167-
* @returns {JsxChild | null | undefined}
155+
* @returns {JsxElement | JsxExpressionContainer | JsxFragment | JsxSpreadChild | JsxText | null | undefined}
168156
*/
169157
function handle(node) {
170158
return one(node, this)
@@ -202,13 +190,13 @@ function unknown(node) {
202190
* Info passed around about the current state.
203191
* @param {HastParents} parent
204192
* hast node whose children to transform.
205-
* @returns {Array<JsxChild>}
193+
* @returns {Array<JsxElement | JsxExpressionContainer | JsxFragment | JsxSpreadChild | JsxText>}
206194
* estree nodes.
207195
*/
208196
function all(parent) {
209197
const children = parent.children || []
210198
let index = -1
211-
/** @type {Array<JsxChild>} */
199+
/** @type {Array<JsxElement | JsxExpressionContainer | JsxFragment | JsxSpreadChild | JsxText>} */
212200
const results = []
213201
const ignoreLineBreak =
214202
this.schema.space === 'html' &&
@@ -302,7 +290,7 @@ function patch(from, to) {
302290
* Create a JSX attribute name.
303291
*
304292
* @param {string} name
305-
* @returns {JsxAttributeName}
293+
* @returns {JsxIdentifier | JsxNamespacedName}
306294
*/
307295
function createJsxAttributeName(name) {
308296
const node = createJsxNameFromString(name)
@@ -320,7 +308,7 @@ function createJsxAttributeName(name) {
320308
* Create a JSX element name.
321309
*
322310
* @param {string} name
323-
* @returns {JsxElementName}
311+
* @returns {JsxIdentifier | JsxMemberExpression | JsxNamespacedName}
324312
*/
325313
function createJsxElementName(name) {
326314
return createJsxNameFromString(name)

test.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
/**
2-
* @typedef {import('estree').Program} Program
3-
* @typedef {import('estree').Node} Node
4-
* @typedef {import('hast').Nodes} HastNodes
5-
* @typedef {import('mdast-util-mdx-jsx').MdxJsxAttribute} MdxJsxAttribute
6-
* @typedef {import('mdast-util-mdx-jsx').MdxJsxAttributeValueExpression} MdxJsxAttributeValueExpression
7-
* @typedef {import('mdast-util-mdx-jsx').MdxJsxExpressionAttribute} MdxJsxExpressionAttribute
2+
* @import {Node, Program} from 'estree'
3+
* @import {Nodes as HastNodes} from 'hast'
4+
* @import {MdxJsxAttributeValueExpression, MdxJsxAttribute, MdxJsxExpressionAttribute} from 'mdast-util-mdx-jsx'
85
*/
96

107
import assert from 'node:assert/strict'

0 commit comments

Comments
 (0)