Socket
Socket
Sign inDemoInstall

hast-util-to-estree

Package Overview
Dependencies
Maintainers
2
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hast-util-to-estree - npm Package Compare versions

Comparing version 2.3.3 to 3.0.0

18

index.d.ts

@@ -1,10 +0,8 @@

export {handlers as defaultHandlers} from './lib/handlers/index.js'
export {toEstree} from './lib/index.js'
export type ElementAttributeNameCase =
import('./lib/state.js').ElementAttributeNameCase
export type Handle = import('./lib/state.js').Handle
export type Options = import('./lib/state.js').Options
export type Space = import('./lib/state.js').Space
export type State = import('./lib/state.js').State
export type StylePropertyNameCase =
import('./lib/state.js').StylePropertyNameCase
export { handlers as defaultHandlers } from "./lib/handlers/index.js";
export { toEstree } from "./lib/index.js";
export type ElementAttributeNameCase = import('./lib/state.js').ElementAttributeNameCase;
export type Handle = import('./lib/state.js').Handle;
export type Options = import('./lib/state.js').Options;
export type Space = import('./lib/state.js').Space;
export type State = import('./lib/state.js').State;
export type StylePropertyNameCase = import('./lib/state.js').StylePropertyNameCase;
/**
* @typedef {import('estree').Comment} Comment
* @typedef {import('estree-jsx').JSXExpressionContainer} JsxExpressionContainer
* @typedef {import('estree-jsx').JSXEmptyExpression} JsxEmptyExpression
* @typedef {import('../state.js').State} State
*/
/**
* Turn a hast comment into an estree node.
*
* @param {import('hast').Comment} node
* @param {HastComment} node
* hast node to transform.

@@ -17,9 +11,7 @@ * @param {State} state

*/
export function comment(
node: import('hast').Comment,
state: State
): JsxExpressionContainer
export type Comment = import('estree').Comment
export type JsxExpressionContainer = import('estree-jsx').JSXExpressionContainer
export type JsxEmptyExpression = import('estree-jsx').JSXEmptyExpression
export type State = import('../state.js').State
export function comment(node: HastComment, state: State): JsxExpressionContainer;
export type Comment = import('estree').Comment;
export type JsxEmptyExpression = import('estree-jsx').JSXEmptyExpression;
export type JsxExpressionContainer = import('estree-jsx').JSXExpressionContainer;
export type HastComment = import('hast').Comment;
export type State = import('../state.js').State;
/**
* @typedef {import('estree').Comment} Comment
*
* @typedef {import('estree-jsx').JSXEmptyExpression} JsxEmptyExpression
* @typedef {import('estree-jsx').JSXExpressionContainer} JsxExpressionContainer
* @typedef {import('estree-jsx').JSXEmptyExpression} JsxEmptyExpression
*
* @typedef {import('hast').Comment} HastComment
*
* @typedef {import('../state.js').State} State
*/
// Make VS Code show references to the above types.
''
/**
* Turn a hast comment into an estree node.
*
* @param {import('hast').Comment} node
* @param {HastComment} node
* hast node to transform.

@@ -13,0 +20,0 @@ * @param {State} state

/**
* Turn a hast element into an estree node.
*
* @param {Element} node
* @param {HastElement} node
* hast node to transform.

@@ -11,9 +11,9 @@ * @param {State} state

*/
export function element(node: Element, state: State): JsxElement
export type Element = import('hast').Element
export type Property = import('estree').Property
export type JsxElement = import('estree-jsx').JSXElement
export type JsxSpreadAttribute = import('estree-jsx').JSXSpreadAttribute
export type JsxAttribute = import('estree-jsx').JSXAttribute
export type State = import('../state.js').State
export type Style = Record<string, string>
export function element(node: HastElement, state: State): JsxElement;
export type Property = import('estree').Property;
export type JsxAttribute = import('estree-jsx').JSXAttribute;
export type JsxElement = import('estree-jsx').JSXElement;
export type JsxSpreadAttribute = import('estree-jsx').JSXSpreadAttribute;
export type HastElement = import('hast').Element;
export type State = import('../state.js').State;
export type Style = Record<string, string>;
/**
* @typedef {import('hast').Element} Element
* @typedef {import('estree').Property} Property
*
* @typedef {import('estree-jsx').JSXAttribute} JsxAttribute
* @typedef {import('estree-jsx').JSXElement} JsxElement
* @typedef {import('estree-jsx').JSXSpreadAttribute} JsxSpreadAttribute
* @typedef {import('estree-jsx').JSXAttribute} JsxAttribute
*
* @typedef {import('hast').Element} HastElement
*
* @typedef {import('../state.js').State} State

@@ -15,9 +18,5 @@ */

import {stringify as commas} from 'comma-separated-tokens'
import {svg, find, hastToReact} from 'property-information'
import {name as identifierName} from 'estree-util-is-identifier-name'
import {find, hastToReact, svg} from 'property-information'
import {stringify as spaces} from 'space-separated-tokens'
import {
start as identifierStart,
cont as identifierCont,
name as identifierName
} from 'estree-util-is-identifier-name'
import styleToObject from 'style-to-object'

@@ -32,3 +31,3 @@

*
* @param {Element} node
* @param {HastElement} node
* hast node to transform.

@@ -68,6 +67,6 @@ * @param {State} state

if (
value === null ||
value === undefined ||
value === null ||
value === false ||
(typeof value === 'number' && Number.isNaN(value)) ||
value === false ||
(!value && info.boolean)

@@ -131,3 +130,3 @@ ) {

if (jsxIdentifierName(prop)) {
if (identifierName(prop, {jsx: true})) {
attributes.push({

@@ -204,6 +203,8 @@ type: 'JSXAttribute',

} catch (error) {
const exception = /** @type {Error} */ (error)
exception.message =
tagName + '[style]' + exception.message.slice('undefined'.length)
throw error
const cause = /** @type {Error} */ (error)
const exception = new Error(
'Could not parse `style` attribute on `' + tagName + '`',
{cause}
)
throw exception
}

@@ -220,3 +221,3 @@

* Value.
* @returns {void}
* @returns {undefined}
* Nothing.

@@ -286,30 +287,1 @@ */

}
/**
* Checks if the given string is a valid identifier name.
*
* Allows dashes, so it’s actually JSX identifier names.
*
* @param {string} name
* Whatever.
* @returns {boolean}
* Whether `name` is a valid JSX identifier.
*/
function jsxIdentifierName(name) {
let index = -1
while (++index < name.length) {
if (!(index ? cont : identifierStart)(name.charCodeAt(index))) return false
}
// `false` if `name` is empty.
return index > 0
/**
* @param {number} code
* @returns {boolean}
*/
function cont(code) {
return identifierCont(code) || code === 45 /* `-` */
}
}
export namespace handlers {
export {comment}
export {ignore as doctype}
export {element}
export {mdxExpression as mdxFlowExpression}
export {mdxExpression as mdxTextExpression}
export {mdxJsxElement as mdxJsxFlowElement}
export {mdxJsxElement as mdxJsxTextElement}
export {mdxjsEsm}
export {text}
export {root}
export { comment };
export { ignore as doctype };
export { element };
export { mdxExpression as mdxFlowExpression };
export { mdxJsxElement as mdxJsxFlowElement };
export { mdxJsxElement as mdxJsxTextElement };
export { mdxExpression as mdxTextExpression };
export { mdxjsEsm };
export { root };
export { text };
}
import {comment} from './comment.js'
import { comment } from './comment.js';
/**
* Handle a node that is ignored.
*
* @returns {void}
* @returns {undefined}
* Nothing.
*/
declare function ignore(): void
import {element} from './element.js'
import {mdxExpression} from './mdx-expression.js'
import {mdxJsxElement} from './mdx-jsx-element.js'
import {mdxjsEsm} from './mdxjs-esm.js'
import {text} from './text.js'
import {root} from './root.js'
export {}
declare function ignore(): undefined;
import { element } from './element.js';
import { mdxExpression } from './mdx-expression.js';
import { mdxJsxElement } from './mdx-jsx-element.js';
import { mdxjsEsm } from './mdxjs-esm.js';
import { root } from './root.js';
import { text } from './text.js';
export {};

@@ -6,4 +6,4 @@ import {comment} from './comment.js'

import {mdxjsEsm} from './mdxjs-esm.js'
import {root} from './root.js'
import {text} from './text.js'
import {root} from './root.js'

@@ -15,8 +15,8 @@ export const handlers = {

mdxFlowExpression: mdxExpression,
mdxTextExpression: mdxExpression,
mdxJsxFlowElement: mdxJsxElement,
mdxJsxTextElement: mdxJsxElement,
mdxTextExpression: mdxExpression,
mdxjsEsm,
text,
root
root,
text
}

@@ -27,5 +27,5 @@

*
* @returns {void}
* @returns {undefined}
* Nothing.
*/
function ignore() {}

@@ -11,13 +11,8 @@ /**

*/
export function mdxExpression(
node: MdxFlowExpression | MdxTextExpression,
state: State
): JsxExpressionContainer
export type MdxFlowExpression =
import('mdast-util-mdx-expression').MdxFlowExpression
export type MdxTextExpression =
import('mdast-util-mdx-expression').MdxTextExpression
export type Expression = import('estree').Expression
export type JsxEmptyExpression = import('estree-jsx').JSXEmptyExpression
export type JsxExpressionContainer = import('estree-jsx').JSXExpressionContainer
export type State = import('../state.js').State
export function mdxExpression(node: MdxFlowExpression | MdxTextExpression, state: State): JsxExpressionContainer;
export type Expression = import('estree').Expression;
export type JsxEmptyExpression = import('estree-jsx').JSXEmptyExpression;
export type JsxExpressionContainer = import('estree-jsx').JSXExpressionContainer;
export type MdxFlowExpression = import('mdast-util-mdx-expression').MdxFlowExpressionHast;
export type MdxTextExpression = import('mdast-util-mdx-expression').MdxTextExpressionHast;
export type State = import('../state.js').State;
/**
* @typedef {import('mdast-util-mdx-expression').MdxFlowExpression} MdxFlowExpression
* @typedef {import('mdast-util-mdx-expression').MdxTextExpression} MdxTextExpression
* @typedef {import('estree').Expression} Expression
*
* @typedef {import('estree-jsx').JSXEmptyExpression} JsxEmptyExpression
* @typedef {import('estree-jsx').JSXExpressionContainer} JsxExpressionContainer
*
* @typedef {import('mdast-util-mdx-expression').MdxFlowExpressionHast} MdxFlowExpression
* @typedef {import('mdast-util-mdx-expression').MdxTextExpressionHast} MdxTextExpression
*
* @typedef {import('../state.js').State} State

@@ -8,0 +11,0 @@ */

@@ -11,13 +11,10 @@ /**

*/
export function mdxJsxElement(
node: MdxJsxFlowElement | MdxJsxTextElement,
state: State
): JsxElement | JsxFragment
export type MdxJsxFlowElement = import('mdast-util-mdx-jsx').MdxJsxFlowElement
export type MdxJsxTextElement = import('mdast-util-mdx-jsx').MdxJsxTextElement
export type Expression = import('estree').Expression
export type JsxElement = import('estree-jsx').JSXElement
export type JsxFragment = import('estree-jsx').JSXFragment
export type JsxAttribute = import('estree-jsx').JSXAttribute
export type JsxSpreadAttribute = import('estree-jsx').JSXSpreadAttribute
export type State = import('../state.js').State
export function mdxJsxElement(node: MdxJsxFlowElement | MdxJsxTextElement, state: State): JsxElement | JsxFragment;
export type Expression = import('estree').Expression;
export type JsxAttribute = import('estree-jsx').JSXAttribute;
export type JsxElement = import('estree-jsx').JSXElement;
export type JsxFragment = import('estree-jsx').JSXFragment;
export type JsxSpreadAttribute = import('estree-jsx').JSXSpreadAttribute;
export type MdxJsxFlowElement = import('mdast-util-mdx-jsx').MdxJsxFlowElementHast;
export type MdxJsxTextElement = import('mdast-util-mdx-jsx').MdxJsxTextElementHast;
export type State = import('../state.js').State;
/**
* @typedef {import('mdast-util-mdx-jsx').MdxJsxFlowElement} MdxJsxFlowElement
* @typedef {import('mdast-util-mdx-jsx').MdxJsxTextElement} MdxJsxTextElement
* @typedef {import('estree').Expression} Expression
*
* @typedef {import('estree-jsx').JSXAttribute} JsxAttribute
* @typedef {import('estree-jsx').JSXElement} JsxElement
* @typedef {import('estree-jsx').JSXFragment} JsxFragment
* @typedef {import('estree-jsx').JSXAttribute} JsxAttribute
* @typedef {import('estree-jsx').JSXSpreadAttribute} JsxSpreadAttribute
*
* @typedef {import('mdast-util-mdx-jsx').MdxJsxFlowElementHast} MdxJsxFlowElement
* @typedef {import('mdast-util-mdx-jsx').MdxJsxTextElementHast} MdxJsxTextElement
*
* @typedef {import('../state.js').State} State

@@ -52,3 +55,3 @@ */

if (attr.type === 'mdxJsxAttribute') {
if (value === undefined || value === null) {
if (value === null || value === undefined) {
attributeValue = null

@@ -55,0 +58,0 @@ // Empty.

@@ -8,7 +8,7 @@ /**

* Info passed around about the current state.
* @returns {void}
* @returns {undefined}
* Nothing.
*/
export function mdxjsEsm(node: MdxjsEsm, state: State): void
export type MdxjsEsm = import('mdast-util-mdxjs-esm').MdxjsEsm
export type State = import('../state.js').State
export function mdxjsEsm(node: MdxjsEsm, state: State): undefined;
export type MdxjsEsm = import('mdast-util-mdxjs-esm').MdxjsEsmHast;
export type State = import('../state.js').State;
/**
* @typedef {import('mdast-util-mdxjs-esm').MdxjsEsm} MdxjsEsm
* @typedef {import('mdast-util-mdxjs-esm').MdxjsEsmHast} MdxjsEsm
*
* @typedef {import('../state.js').State} State

@@ -15,3 +16,3 @@ */

* Info passed around about the current state.
* @returns {void}
* @returns {undefined}
* Nothing.

@@ -18,0 +19,0 @@ */

/**
* Turn a hast root node into an estree node.
*
* @param {Root} node
* @param {HastRoot} node
* hast node to transform.

@@ -11,6 +11,6 @@ * @param {State} state

*/
export function root(node: Root, state: State): JsxFragment
export type Root = import('hast').Root
export type JsxFragment = import('estree-jsx').JSXFragment
export type JsxChild = JsxFragment['children'][number]
export type State = import('../state.js').State
export function root(node: HastRoot, state: State): JsxFragment;
export type JsxFragment = import('estree-jsx').JSXFragment;
export type HastRoot = import('hast').Root;
export type State = import('../state.js').State;
export type JsxChild = JsxFragment['children'][number];
/**
* @typedef {import('hast').Root} Root
* @typedef {import('estree-jsx').JSXFragment} JsxFragment
* @typedef {JsxFragment['children'][number]} JsxChild
*
* @typedef {import('hast').Root} HastRoot
*
* @typedef {import('../state.js').State} State
*/
/**
* @typedef {JsxFragment['children'][number]} JsxChild
*/
import {whitespace} from 'hast-util-whitespace'

@@ -13,3 +18,3 @@

*
* @param {Root} node
* @param {HastRoot} node
* hast node to transform.

@@ -36,3 +41,3 @@ * @param {State} state

child.expression.type === 'Literal' &&
whitespace(child.expression.value)
whitespace(String(child.expression.value))
) {

@@ -39,0 +44,0 @@ if (queue) queue.push(child)

/**
* @typedef {import('hast').Text} Text
* @typedef {import('estree').Literal} Literal
* @typedef {import('estree-jsx').JSXExpressionContainer} JsxExpressionContainer
* @typedef {import('../state.js').State} State
*/
/**
* Turn a hast text node into an estree node.
*
* @param {Text} node
* @param {HastText} node
* hast node to transform.
* @param {State} state
* Info passed around about the current state.
* @returns {JsxExpressionContainer | void}
* @returns {JsxExpressionContainer | undefined}
* JSX expression.
*/
export function text(node: Text, state: State): JsxExpressionContainer | void
export type Text = import('hast').Text
export type Literal = import('estree').Literal
export type JsxExpressionContainer = import('estree-jsx').JSXExpressionContainer
export type State = import('../state.js').State
export function text(node: HastText, state: State): JsxExpressionContainer | undefined;
export type Literal = import('estree').Literal;
export type JsxExpressionContainer = import('estree-jsx').JSXExpressionContainer;
export type HastText = import('hast').Text;
export type State = import('../state.js').State;
/**
* @typedef {import('hast').Text} Text
* @typedef {import('estree').Literal} Literal
*
* @typedef {import('estree-jsx').JSXExpressionContainer} JsxExpressionContainer
*
* @typedef {import('hast').Text} HastText
*
* @typedef {import('../state.js').State} State
*/
// Make VS Code show references to the above types.
''
/**
* Turn a hast text node into an estree node.
*
* @param {Text} node
* @param {HastText} node
* hast node to transform.
* @param {State} state
* Info passed around about the current state.
* @returns {JsxExpressionContainer | void}
* @returns {JsxExpressionContainer | undefined}
* JSX expression.

@@ -17,0 +23,0 @@ */

@@ -30,6 +30,6 @@ /**

*
* @param {Node} tree
* @param {HastNodes} tree
* hast tree.
* @param {Options | null | undefined} [options]
* Configuration.
* Configuration (optional).
* @returns {Program}

@@ -52,31 +52,6 @@ * estree program node.

*/
export function toEstree(
tree: Node,
options?: Options | null | undefined
): Program
export type Content = import('hast').Content
export type Root = import('hast').Root
export type ExpressionStatement = import('estree').ExpressionStatement
export type Program = import('estree').Program
export type MdxJsxAttribute = import('mdast-util-mdx-jsx').MdxJsxAttribute
export type MdxJsxAttributeValueExpression =
import('mdast-util-mdx-jsx').MdxJsxAttributeValueExpression
export type MdxJsxExpressionAttribute =
import('mdast-util-mdx-jsx').MdxJsxExpressionAttribute
export type MdxJsxFlowElement = import('mdast-util-mdx-jsx').MdxJsxFlowElement
export type MdxJsxTextElement = import('mdast-util-mdx-jsx').MdxJsxTextElement
export type MdxFlowExpression =
import('mdast-util-mdx-expression').MdxFlowExpression
export type MdxTextExpression =
import('mdast-util-mdx-expression').MdxTextExpression
export type Options = import('./state.js').Options
export type Node =
| Root
| Content
| MdxJsxAttributeValueExpression
| MdxJsxAttribute
| MdxJsxExpressionAttribute
| MdxJsxFlowElement
| MdxJsxTextElement
| MdxFlowExpression
| MdxTextExpression
export function toEstree(tree: HastNodes, options?: Options | null | undefined): Program;
export type ExpressionStatement = import('estree').ExpressionStatement;
export type Program = import('estree').Program;
export type HastNodes = import('hast').Nodes;
export type Options = import('./state.js').Options;
/**
* @typedef {import('hast').Content} Content
* @typedef {import('hast').Root} Root
* Register MDX nodes in tree:
*
* @typedef {import('mdast-util-mdx-expression')}
* @typedef {import('mdast-util-mdx-jsx')}
* @typedef {import('mdast-util-mdxjs-esm')}
*/
/**
* @typedef {import('estree').ExpressionStatement} ExpressionStatement
* @typedef {import('estree').Program} Program
*
* @typedef {import('mdast-util-mdx-jsx').MdxJsxAttribute} MdxJsxAttribute
* @typedef {import('mdast-util-mdx-jsx').MdxJsxAttributeValueExpression} MdxJsxAttributeValueExpression
* @typedef {import('mdast-util-mdx-jsx').MdxJsxExpressionAttribute} MdxJsxExpressionAttribute
* @typedef {import('mdast-util-mdx-jsx').MdxJsxFlowElement} MdxJsxFlowElement
* @typedef {import('mdast-util-mdx-jsx').MdxJsxTextElement} MdxJsxTextElement
* @typedef {import('hast').Nodes} HastNodes
*
* @typedef {import('mdast-util-mdx-expression').MdxFlowExpression} MdxFlowExpression
* @typedef {import('mdast-util-mdx-expression').MdxTextExpression} MdxTextExpression
*
* @typedef {import('./state.js').Options} Options
*/
/**
* @typedef {Root | Content | MdxJsxAttributeValueExpression | MdxJsxAttribute | MdxJsxExpressionAttribute | MdxJsxFlowElement | MdxJsxTextElement | MdxFlowExpression | MdxTextExpression} Node
*/
import {createState} from './state.js'

@@ -55,6 +49,6 @@

*
* @param {Node} tree
* @param {HastNodes} tree
* hast tree.
* @param {Options | null | undefined} [options]
* Configuration.
* Configuration (optional).
* @returns {Program}

@@ -94,3 +88,3 @@ * estree program node.

/** @type {ExpressionStatement} */
// @ts-expect-error Types are wrong (`expression` *can* be JSX).
// @ts-expect-error: `estree` types don’t allow JSX.
const statement = {type: 'ExpressionStatement', expression: result}

@@ -97,0 +91,0 @@ state.patch(tree, statement)

@@ -9,61 +9,69 @@ /**

*/
export function createState(options: Options): State
export type Comment = import('estree').Comment
export type Directive = import('estree').Directive
export type ModuleDeclaration = import('estree').ModuleDeclaration
export type EstreeNode = import('estree').Node
export type Statement = import('estree').Statement
export type JsxAttribute = import('estree-jsx').JSXAttribute
export type JsxElement = import('estree-jsx').JSXElement
export type JsxIdentifier = import('estree-jsx').JSXIdentifier
export type JsxMemberExpression = import('estree-jsx').JSXMemberExpression
export type JsxNamespacedName = import('estree-jsx').JSXNamespacedName
export type Content = import('hast').Content
export type Root = import('hast').Root
export type MdxFlowExpression =
import('mdast-util-mdx-expression').MdxFlowExpression
export type MdxTextExpression =
import('mdast-util-mdx-expression').MdxTextExpression
export type MdxJsxAttribute = import('mdast-util-mdx-jsx').MdxJsxAttribute
export type MdxJsxAttributeValueExpression =
import('mdast-util-mdx-jsx').MdxJsxAttributeValueExpression
export type MdxJsxExpressionAttribute =
import('mdast-util-mdx-jsx').MdxJsxExpressionAttribute
export type MdxJsxFlowElement = import('mdast-util-mdx-jsx').MdxJsxFlowElement
export type MdxJsxTextElement = import('mdast-util-mdx-jsx').MdxJsxTextElement
export type Schema = import('property-information').Schema
export type UnistParent = import('unist').Parent
export type Node =
| Content
| MdxJsxAttributeValueExpression
| MdxJsxAttribute
| MdxJsxExpressionAttribute
| MdxJsxFlowElement
| MdxJsxTextElement
| MdxFlowExpression
| MdxTextExpression
| Root
export type Parent = Extract<Node, UnistParent>
export type JsxElementName = JsxElement['openingElement']['name']
export type JsxAttributeName = JsxAttribute['name']
export type JsxChild = JsxElement['children'][number]
export function createState(options: Options): State;
export type Comment = import('estree').Comment;
export type Directive = import('estree').Directive;
export type ModuleDeclaration = import('estree').ModuleDeclaration;
export type EstreeNode = import('estree').Node;
export type Statement = import('estree').Statement;
export type JsxAttribute = import('estree-jsx').JSXAttribute;
export type JsxElement = import('estree-jsx').JSXElement;
export type JsxIdentifier = import('estree-jsx').JSXIdentifier;
export type JsxMemberExpression = import('estree-jsx').JSXMemberExpression;
export type JsxNamespacedName = import('estree-jsx').JSXNamespacedName;
export type MdxJsxAttribute = import('mdast-util-mdx-jsx').MdxJsxAttribute;
export type MdxJsxAttributeValueExpression = import('mdast-util-mdx-jsx').MdxJsxAttributeValueExpression;
export type MdxJsxExpressionAttribute = import('mdast-util-mdx-jsx').MdxJsxExpressionAttribute;
export type HastNodes = import('hast').Nodes;
export type HastParents = import('hast').Parents;
export type Schema = import('property-information').Schema;
export type JsxElementName = JsxElement['openingElement']['name'];
export type JsxAttributeName = JsxAttribute['name'];
export type JsxChild = JsxElement['children'][number];
/**
* Namespace.
* Specify casing to use for attribute names.
*
* HTML casing is for example `class`, `stroke-linecap`, `xml:lang`.
* React casing is for example `className`, `strokeLinecap`, `xmlLang`.
*/
export type Space = 'html' | 'svg'
export type ElementAttributeNameCase = 'html' | 'react';
/**
* Turn a hast node into an estree node.
*/
export type Handle = (
node: any,
state: State
) => JsxChild | null | undefined | void
export type Handle = (node: any, state: State) => JsxChild | null | undefined;
/**
* Specify casing to use for attribute names.
*
* HTML casing is for example `class`, `stroke-linecap`, `xml:lang`.
* React casing is for example `className`, `strokeLinecap`, `xmlLang`.
* Configuration.
*/
export type ElementAttributeNameCase = 'html' | 'react'
export type Options = {
/**
* Specify casing to use for attribute names (default: `'react'`).
*
* This casing is used for hast elements, not for embedded MDX JSX nodes
* (components that someone authored manually).
*/
elementAttributeNameCase?: ElementAttributeNameCase | null | undefined;
/**
* Custom handlers (optional).
*/
handlers?: Record<string, Handle | null | undefined> | null | undefined;
/**
* Which space the document is in (default: `'html'`).
*
* When an `<svg>` element is found in the HTML space, this package already
* automatically switches to and from the SVG space when entering and exiting
* it.
*/
space?: Space | null | undefined;
/**
* Specify casing to use for property names in `style` objects (default: `'dom'`).
*
* This casing is used for hast elements, not for embedded MDX JSX nodes
* (components that someone authored manually).
*/
stylePropertyNameCase?: StylePropertyNameCase | null | undefined;
};
/**
* Namespace.
*/
export type Space = 'html' | 'svg';
/**
* Casing to use for property names in `style` objects.

@@ -74,82 +82,51 @@ *

*/
export type StylePropertyNameCase = 'css' | 'dom'
export type StylePropertyNameCase = 'css' | 'dom';
/**
* Configuration.
*/
export type Options = {
/**
* Specify casing to use for attribute names.
*
* This casing is used for hast elements, not for embedded MDX JSX nodes
* (components that someone authored manually).
*/
elementAttributeNameCase?: ElementAttributeNameCase | null | undefined
/**
* Custom handlers.
*/
handlers?: Record<string, Handle | null | undefined> | null | undefined
/**
* Which space the document is in.
*
* When an `<svg>` element is found in the HTML space, this package already
* automatically switches to and from the SVG space when entering and exiting
* it.
*/
space?: Space | null | undefined
/**
* Specify casing to use for property names in `style` objects.
*
* This casing is used for hast elements, not for embedded MDX JSX nodes
* (components that someone authored manually).
*/
stylePropertyNameCase?: StylePropertyNameCase | null | undefined
}
/**
* Info passed around about the current state.
*/
export type State = {
/**
* Current schema.
*/
schema: Schema
/**
* Casing to use for attribute names.
*/
elementAttributeNameCase: ElementAttributeNameCase
/**
* Casing to use for property names in `style` objects.
*/
stylePropertyNameCase: StylePropertyNameCase
/**
* List of estree comments.
*/
comments: Array<Comment>
/**
* List of top-level estree nodes.
*/
esm: Array<Directive | Statement | ModuleDeclaration>
/**
* Transform a hast node to estree.
*/
handle: (node: any) => JsxChild | null | undefined | void
/**
* Transform children of a hast parent to estree.
*/
all: (parent: Parent) => Array<JsxChild>
/**
* Take positional info from `from` (use `inherit` if you also want data).
*/
patch: (from: Node, to: EstreeNode | Comment) => void
/**
* Take positional info and data from `from` (use `patch` if you don’t want data).
*/
inherit: (from: Node, to: EstreeNode | Comment) => void
/**
* Create a JSX attribute name.
*/
createJsxAttributeName: (name: string) => JsxAttributeName
/**
* Create a JSX element name.
*/
createJsxElementName: (name: string) => JsxElementName
}
/**
* Transform children of a hast parent to estree.
*/
all: (parent: HastParents) => Array<JsxChild>;
/**
* List of estree comments.
*/
comments: Array<Comment>;
/**
* Create a JSX attribute name.
*/
createJsxAttributeName: (name: string) => JsxAttributeName;
/**
* Create a JSX element name.
*/
createJsxElementName: (name: string) => JsxElementName;
/**
* Casing to use for attribute names.
*/
elementAttributeNameCase: ElementAttributeNameCase;
/**
* List of top-level estree nodes.
*/
esm: Array<Directive | ModuleDeclaration | Statement>;
/**
* Transform a hast node to estree.
*/
handle: (node: any) => JsxChild | null | undefined;
/**
* Take positional info and data from `from` (use `patch` if you don’t want data).
*/
inherit: (from: HastNodes | MdxJsxAttribute | MdxJsxAttributeValueExpression | MdxJsxExpressionAttribute, to: Comment | EstreeNode) => undefined;
/**
* Take positional info from `from` (use `inherit` if you also want data).
*/
patch: (from: HastNodes, to: Comment | EstreeNode) => undefined;
/**
* Current schema.
*/
schema: Schema;
/**
* Casing to use for property names in `style` objects.
*/
stylePropertyNameCase: StylePropertyNameCase;
};

@@ -14,28 +14,24 @@ /**

*
* @typedef {import('hast').Content} Content
* @typedef {import('hast').Root} Root
*
* @typedef {import('mdast-util-mdx-expression').MdxFlowExpression} MdxFlowExpression
* @typedef {import('mdast-util-mdx-expression').MdxTextExpression} MdxTextExpression
*
* @typedef {import('mdast-util-mdx-jsx').MdxJsxAttribute} MdxJsxAttribute
* @typedef {import('mdast-util-mdx-jsx').MdxJsxAttributeValueExpression} MdxJsxAttributeValueExpression
* @typedef {import('mdast-util-mdx-jsx').MdxJsxExpressionAttribute} MdxJsxExpressionAttribute
* @typedef {import('mdast-util-mdx-jsx').MdxJsxFlowElement} MdxJsxFlowElement
* @typedef {import('mdast-util-mdx-jsx').MdxJsxTextElement} MdxJsxTextElement
*
* @typedef {import('hast').Nodes} HastNodes
* @typedef {import('hast').Parents} HastParents
*
* @typedef {import('property-information').Schema} Schema
*
* @typedef {import('unist').Parent} UnistParent
*/
/**
* @typedef {Content | MdxJsxAttributeValueExpression | MdxJsxAttribute | MdxJsxExpressionAttribute | MdxJsxFlowElement | MdxJsxTextElement | MdxFlowExpression | MdxTextExpression | Root} Node
* @typedef {Extract<Node, UnistParent>} Parent
* @typedef {JsxElement['openingElement']['name']} JsxElementName
* @typedef {JsxAttribute['name']} JsxAttributeName
* @typedef {JsxElement['children'][number]} JsxChild
*/
/**
* @typedef {'html' | 'react'} ElementAttributeNameCase
* Specify casing to use for attribute names.
*
* @typedef {'html' | 'svg'} Space
* Namespace.
* HTML casing is for example `class`, `stroke-linecap`, `xml:lang`.
* React casing is for example `className`, `strokeLinecap`, `xmlLang`.
*

@@ -48,21 +44,9 @@ * @callback Handle

* Info passed around about the current state.
* @returns {JsxChild | null | undefined | void}
* @returns {JsxChild | null | undefined}
* estree node.
*
* @typedef {'html' | 'react'} ElementAttributeNameCase
* Specify casing to use for attribute names.
*
* HTML casing is for example `class`, `stroke-linecap`, `xml:lang`.
* React casing is for example `className`, `strokeLinecap`, `xmlLang`.
*
* @typedef {'css' | 'dom'} StylePropertyNameCase
* Casing to use for property names in `style` objects.
*
* CSS casing is for example `background-color` and `-webkit-line-clamp`.
* DOM casing is for example `backgroundColor` and `WebkitLineClamp`.
*
* @typedef Options
* Configuration.
* @property {ElementAttributeNameCase | null | undefined} [elementAttributeNameCase='react']
* Specify casing to use for attribute names.
* Specify casing to use for attribute names (default: `'react'`).
*

@@ -72,5 +56,5 @@ * This casing is used for hast elements, not for embedded MDX JSX nodes

* @property {Record<string, Handle | null | undefined> | null | undefined} [handlers={}]
* Custom handlers.
* Custom handlers (optional).
* @property {Space | null | undefined} [space='html']
* Which space the document is in.
* Which space the document is in (default: `'html'`).
*

@@ -81,3 +65,3 @@ * When an `<svg>` element is found in the HTML space, this package already

* @property {StylePropertyNameCase | null | undefined} [stylePropertyNameCase='dom']
* Specify casing to use for property names in `style` objects.
* Specify casing to use for property names in `style` objects (default: `'dom'`).
*

@@ -87,22 +71,17 @@ * This casing is used for hast elements, not for embedded MDX JSX nodes

*
* @typedef {'html' | 'svg'} Space
* Namespace.
*
* @typedef {'css' | 'dom'} StylePropertyNameCase
* Casing to use for property names in `style` objects.
*
* CSS casing is for example `background-color` and `-webkit-line-clamp`.
* DOM casing is for example `backgroundColor` and `WebkitLineClamp`.
*
* @typedef State
* Info passed around about the current state.
* @property {Schema} schema
* Current schema.
* @property {ElementAttributeNameCase} elementAttributeNameCase
* Casing to use for attribute names.
* @property {StylePropertyNameCase} stylePropertyNameCase
* Casing to use for property names in `style` objects.
* @property {(parent: HastParents) => Array<JsxChild>} all
* Transform children of a hast parent to estree.
* @property {Array<Comment>} comments
* List of estree comments.
* @property {Array<Directive | Statement | ModuleDeclaration>} esm
* List of top-level estree nodes.
* @property {(node: any) => JsxChild | null | undefined | void} handle
* Transform a hast node to estree.
* @property {(parent: Parent) => Array<JsxChild>} all
* Transform children of a hast parent to estree.
* @property {(from: Node, to: EstreeNode | Comment) => void} patch
* Take positional info from `from` (use `inherit` if you also want data).
* @property {(from: Node, to: EstreeNode | Comment) => void} inherit
* Take positional info and data from `from` (use `patch` if you don’t want data).
* @property {(name: string) => JsxAttributeName} createJsxAttributeName

@@ -112,4 +91,19 @@ * Create a JSX attribute name.

* Create a JSX element name.
* @property {ElementAttributeNameCase} elementAttributeNameCase
* Casing to use for attribute names.
* @property {Array<Directive | ModuleDeclaration | Statement>} esm
* List of top-level estree nodes.
* @property {(node: any) => JsxChild | null | undefined} handle
* Transform a hast node to estree.
* @property {(from: HastNodes | MdxJsxAttribute | MdxJsxAttributeValueExpression | MdxJsxExpressionAttribute, to: Comment | EstreeNode) => undefined} inherit
* Take positional info and data from `from` (use `patch` if you don’t want data).
* @property {(from: HastNodes, to: Comment | EstreeNode) => undefined} patch
* Take positional info from `from` (use `inherit` if you also want data).
* @property {Schema} schema
* Current schema.
* @property {StylePropertyNameCase} stylePropertyNameCase
* Casing to use for property names in `style` objects.
*/
import {ok as assert} from 'devlop'
import {html, svg} from 'property-information'

@@ -134,3 +128,3 @@ import {position} from 'unist-util-position'

// See: <https://github.com/mdx-js/mdx/issues/2000>
const tableElements = new Set(['table', 'thead', 'tbody', 'tfoot', 'tr'])
const tableElements = new Set(['table', 'tbody', 'thead', 'tfoot', 'tr'])

@@ -156,4 +150,4 @@ /**

// Current space.
elementAttributeNameCase: options.elementAttributeNameCase || 'react',
schema: options.space === 'svg' ? svg : html,
elementAttributeNameCase: options.elementAttributeNameCase || 'react',
stylePropertyNameCase: options.stylePropertyNameCase || 'dom',

@@ -164,8 +158,8 @@ // Results.

// Useful functions.
all,
createJsxAttributeName,
createJsxElementName,
handle,
all,
patch,
inherit,
createJsxAttributeName,
createJsxElementName
patch
}

@@ -176,3 +170,3 @@

* @param {any} node
* @returns {JsxChild | null | undefined | void}
* @returns {JsxChild | null | undefined}
*/

@@ -205,3 +199,4 @@ function handle(node) {

function unknown(node) {
// @ts-expect-error: JS guarantees there’s a `type`.
assert(node && typeof node === 'object')
assert('type' in node)
throw new Error('Cannot handle unknown node `' + node.type + '`')

@@ -213,3 +208,3 @@ }

* Info passed around about the current state.
* @param {Parent | MdxJsxFlowElement | MdxJsxTextElement} parent
* @param {HastParents} parent
* hast node whose children to transform.

@@ -253,13 +248,12 @@ * @returns {Array<JsxChild>}

*
* @param {Node | MdxJsxAttributeValueExpression | MdxJsxAttribute | MdxJsxExpressionAttribute | MdxJsxFlowElement | MdxJsxTextElement | MdxFlowExpression | MdxTextExpression} from
* @param {HastNodes | MdxJsxAttribute | MdxJsxAttributeValueExpression | MdxJsxExpressionAttribute} from
* hast node to take positional info and data from.
* @param {EstreeNode | Comment} to
* @param {Comment | EstreeNode} to
* estree node to add positional info and data to.
* @returns {void}
* @returns {undefined}
* Nothing.
*/
function inherit(from, to) {
const left = /** @type {Record<string, unknown> | undefined} */ (from.data)
/** @type {Record<string, unknown> | undefined} */
const left = from.data
/** @type {Record<string, unknown> | undefined} */
let right

@@ -291,7 +285,7 @@ /** @type {string} */

*
* @param {Node | MdxJsxAttributeValueExpression | MdxJsxAttribute | MdxJsxExpressionAttribute | MdxJsxFlowElement | MdxJsxTextElement | MdxFlowExpression | MdxTextExpression} from
* @param {HastNodes | MdxJsxAttribute | MdxJsxAttributeValueExpression | MdxJsxExpressionAttribute} from
* hast node to take positional info from.
* @param {EstreeNode | Comment} to
* @param {Comment | EstreeNode} to
* estree node to add positional info to.
* @returns {void}
* @returns {undefined}
* Nothing.

@@ -302,7 +296,3 @@ */

if (
p.start.line &&
p.start.offset !== undefined &&
p.end.offset !== undefined
) {
if (p && p.start.offset !== undefined && p.end.offset !== undefined) {
// @ts-expect-error acorn-style.

@@ -353,3 +343,3 @@ to.start = p.start.offset

* Name.
* @returns {JsxMemberExpression | JsxNamespacedName | JsxIdentifier}
* @returns {JsxIdentifier | JsxMemberExpression | JsxNamespacedName}
* Node.

@@ -361,4 +351,4 @@ */

let part = names.shift()
/** @type {JsxMemberExpression} */
// @ts-expect-error: hush, the first is always defined.
assert(part, 'Expected `part` to be defined')
/** @type {JsxIdentifier | JsxMemberExpression} */
let node = {type: 'JSXIdentifier', name: part}

@@ -365,0 +355,0 @@

{
"name": "hast-util-to-estree",
"version": "2.3.3",
"version": "3.0.0",
"description": "hast utility to transform to estree (JavaScript AST) JSX",

@@ -17,3 +17,2 @@ "license": "MIT",

"estree",
"babel",
"jsx",

@@ -36,4 +35,3 @@ "javascript",

"type": "module",
"main": "index.js",
"types": "index.d.ts",
"exports": "./index.js",
"files": [

@@ -47,36 +45,30 @@ "lib/",

"@types/estree-jsx": "^1.0.0",
"@types/hast": "^2.0.0",
"@types/unist": "^2.0.0",
"@types/hast": "^3.0.0",
"comma-separated-tokens": "^2.0.0",
"estree-util-attach-comments": "^2.0.0",
"estree-util-is-identifier-name": "^2.0.0",
"hast-util-whitespace": "^2.0.0",
"mdast-util-mdx-expression": "^1.0.0",
"mdast-util-mdxjs-esm": "^1.0.0",
"devlop": "^1.0.0",
"estree-util-attach-comments": "^3.0.0",
"estree-util-is-identifier-name": "^3.0.0",
"hast-util-whitespace": "^3.0.0",
"mdast-util-mdx-expression": "^2.0.0",
"mdast-util-mdx-jsx": "^3.0.0",
"mdast-util-mdxjs-esm": "^2.0.0",
"property-information": "^6.0.0",
"space-separated-tokens": "^2.0.0",
"style-to-object": "^0.4.1",
"unist-util-position": "^4.0.0",
"style-to-object": "^0.4.0",
"unist-util-position": "^5.0.0",
"zwitch": "^2.0.0"
},
"devDependencies": {
"@babel/core": "^7.0.0",
"@babel/generator": "^7.0.0",
"@babel/plugin-transform-react-jsx": "^7.0.0",
"@types/babel__core": "^7.0.0",
"@types/babel__generator": "^7.0.0",
"@types/node": "^20.0.0",
"@vue/babel-plugin-jsx": "^1.0.0",
"acorn-jsx": "^5.0.0",
"c8": "^7.0.0",
"esast-util-from-js": "^1.0.0",
"estree-to-babel": "^5.0.0",
"estree-util-to-js": "^1.0.0",
"c8": "^8.0.0",
"esast-util-from-js": "^2.0.0",
"estree-util-to-js": "^2.0.0",
"estree-walker": "^3.0.0",
"hastscript": "^7.0.0",
"mdast-util-from-markdown": "^1.0.0",
"mdast-util-mdx": "^2.0.0",
"mdast-util-to-hast": "^12.0.0",
"micromark-extension-mdxjs": "^1.0.0",
"prettier": "^2.0.0",
"hastscript": "^8.0.0",
"mdast-util-from-markdown": "^2.0.0",
"mdast-util-mdx": "^3.0.0",
"mdast-util-to-hast": "^13.0.0",
"micromark-extension-mdxjs": "^2.0.0",
"prettier": "^3.0.0",
"remark-cli": "^11.0.0",

@@ -86,4 +78,4 @@ "remark-preset-wooorm": "^9.0.0",

"typescript": "^5.0.0",
"unist-util-visit": "^4.0.0",
"xo": "^0.54.0"
"unist-util-visit": "^5.0.0",
"xo": "^0.55.0"
},

@@ -93,24 +85,18 @@ "scripts": {

"build": "tsc --build --clean && tsc --build && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"format": "remark . -qfo && prettier . -w --log-level warn && xo --fix",
"test-api": "node --conditions development test.js",
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api",
"test-coverage": "c8 --100 --reporter lcov npm run test-api",
"test": "npm run build && npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"bracketSpacing": false,
"semi": false,
"trailingComma": "none"
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "none",
"useTabs": false
},
"xo": {
"prettier": true,
"rules": {
"unicorn/prefer-code-point": "off"
}
},
"remarkConfig": {
"plugins": [
"preset-wooorm"
"remark-preset-wooorm"
]

@@ -121,3 +107,2 @@ },

"detail": true,
"strict": true,
"ignoreCatch": true,

@@ -128,4 +113,12 @@ "#": "needed any",

"lib/state.js"
]
],
"strict": true
},
"xo": {
"prettier": true,
"rules": {
"unicorn/prefer-code-point": "off",
"unicorn/prefer-string-replace-all": "off"
}
}
}

@@ -50,3 +50,3 @@ # hast-util-to-estree

This package is [ESM only][esm].
In Node.js (version 14.14+ and 16.0+), install with [npm][]:
In Node.js (version 16+), install with [npm][]:

@@ -60,3 +60,3 @@ ```sh

```js
import {toEstree} from 'https://esm.sh/hast-util-to-estree@2'
import {toEstree} from 'https://esm.sh/hast-util-to-estree@3'
```

@@ -68,3 +68,3 @@

<script type="module">
import {toEstree} from 'https://esm.sh/hast-util-to-estree@2?bundle'
import {toEstree} from 'https://esm.sh/hast-util-to-estree@3?bundle'
</script>

@@ -101,5 +101,5 @@ ```

import fs from 'node:fs/promises'
import {jsx, toJs} from 'estree-util-to-js'
import {fromHtml} from 'hast-util-from-html'
import {toEstree} from 'hast-util-to-estree'
import {toJs, jsx} from 'estree-util-to-js'

@@ -292,24 +292,24 @@ const hast = fromHtml(await fs.readFile('example.html'))

* `schema` ([`Schema`][schema])
— current schema
* `all` (`(node: HastParent) => EstreeJsxChild | undefined`)
— transform children of a hast parent to estree
* `comments` (`Array<EstreeComment>`)
— list of estree comments
* `createJsxAttributeName` (`(name: string) => EstreeJsxAttributeName`)
— create a JSX attribute name
* `createJsxElementName` (`(name: string) => EstreeJsxElementName`)
— create a JSX attribute name
* `elementAttributeNameCase`
([`ElementAttributeNameCase`][api-element-attribute-name-case])
— casing to use for attribute names
* `comments` (`Array<EstreeComment>`)
— list of estree comments
* `esm` (`Array<EstreeNode>`)
— list of top-level estree nodes
* `handle` (`(node: HastNode) => EstreeJsxChild | void`)
* `handle` (`(node: HastNode) => EstreeJsxChild | undefined`)
— transform a hast node to estree
* `handle` (`(node: HastParent) => EstreeJsxChild | void`)
— transform children of a hast parent to estree
* `patch` (`(from: HastNode, to: EstreeNode) => void`)
— take positional info from `from` (use `inherit` if you also want data)
* `inherit` (`(from: HastNode, to: EstreeNode) => void`)
* `inherit` (`(from: HastNode, to: EstreeNode) => undefined`)
— take positional info and data from `from` (use `patch` if you don’t want
data)
* `createJsxAttributeName` (`(name: string) => EstreeJsxAttributeName`)
— create a JSX attribute name
* `createJsxElementName` (`(name: string) => EstreeJsxElementName`)
— create a JSX attribute name
* `patch` (`(from: HastNode, to: EstreeNode) => undefined`)
— take positional info from `from` (use `inherit` if you also want data)
* `schema` ([`Schema`][schema])
— current schema

@@ -326,3 +326,3 @@ ### `StylePropertyNameCase`

```ts
type StylePropertyNameCase = 'dom' | 'css'
type StylePropertyNameCase = 'css' | 'dom'
```

@@ -341,7 +341,10 @@

Projects maintained by the unified collective are compatible with all maintained
Projects maintained by the unified collective are compatible with maintained
versions of Node.js.
As of now, that is Node.js 14.14+ and 16.0+.
Our projects sometimes work with older versions, but this is not guaranteed.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line, `hast-util-to-estree@^3`,
compatible with Node.js 16.
## Security

@@ -354,2 +357,4 @@

* [`estree-util-build-jsx`][build-jsx]
— transform JSX to function calls
* [`hastscript`][hastscript]

@@ -359,4 +364,2 @@ — hyperscript compatible interface for creating nodes

— transform a DOM tree to hast
* [`estree-util-build-jsx`][build-jsx]
— transform JSX to function calls

@@ -391,5 +394,5 @@ ## Contribute

[size-badge]: https://img.shields.io/bundlephobia/minzip/hast-util-to-estree.svg
[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size&query=$.size.compressedSize&url=https://deno.bundlejs.com/?q=hast-util-to-estree
[size]: https://bundlephobia.com/result?p=hast-util-to-estree
[size]: https://bundlejs.com/?q=hast-util-to-estree

@@ -396,0 +399,0 @@ [sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc