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.0.0 to 2.0.1

86

index.d.ts
/**
* @param {Node} tree
* @param {Node|MDXJsxAttributeValueExpression|MDXJsxAttribute|MDXJsxExpressionAttribute|MDXJsxFlowElement|MDXJsxTextElement|MDXFlowExpression|MDXTextExpression} tree
* @param {Options} options
* @returns {EstreeProgram}
*/
export function toEstree(tree: Node, options?: Options): EstreeProgram
export type Node = import('unist').Node
export function toEstree(
tree:
| Node
| MDXJsxAttributeValueExpression
| MDXJsxAttribute
| MDXJsxExpressionAttribute
| MDXJsxFlowElement
| MDXJsxTextElement
| MDXFlowExpression
| MDXTextExpression,
options?: Options
): EstreeProgram
export type UnistNode = import('unist').Node
export type Parent = import('hast').Parent

@@ -14,5 +25,7 @@ export type Root = import('hast').Root

export type Properties = import('hast').Properties
export type Node = Root['children'][number] | Root
export type EstreeNode = import('estree-jsx').Node
export type EstreeProgram = import('estree-jsx').Program
export type EstreeJsxExpressionContainer = import('estree-jsx').JSXExpressionContainer
export type EstreeJsxExpressionContainer =
import('estree-jsx').JSXExpressionContainer
export type EstreeJsxElement = import('estree-jsx').JSXElement

@@ -29,52 +42,29 @@ export type EstreeJsxOpeningElement = import('estree-jsx').JSXOpeningElement

export type EstreeProperty = import('estree-jsx').Property
export type JSXIdentifier = import('estree-jsx').JSXIdentifier
export type JSXMemberExpression = import('estree-jsx').JSXMemberExpression
export type EstreeJsxElementName = EstreeJsxOpeningElement['name']
export type EstreeJsxAttributeName = EstreeJsxAttribute['name']
export type EstreeJsxChild = EstreeJsxElement['children'][number]
export type ElementChild = Element['children'][number]
export type MDXJsxAttributeValueExpression = Node & {
type: 'mdxJsxAttributeValueExpression'
value: string
}
export type MDXJsxAttribute = Node & {
type: 'mdxJsxAttribute'
name: string
value: (MDXJsxAttributeValueExpression | string) | null
}
export type MDXJsxExpressionAttribute = Node & {
type: 'mdxJsxExpressionAttribute'
value: string
}
export type MDXJsxElement = Parent & {
name: string | null
attributes: Array<MDXJsxExpressionAttribute | MDXJsxAttribute>
}
export type MDXJsxFlowElement = MDXJsxElement & {
type: 'mdxJsxFlowElement'
children: Array<MDXJsxFlowElement | ElementChild>
}
export type MDXJsxTextElement = MDXJsxElement & {
type: 'mdxJsxTextElement'
children: Array<MDXJsxTextElement | ElementChild>
}
export type MDXExpression = Node & {
value: string
}
export type MDXFlowExpression = MDXExpression & {
type: 'mdxFlowExpression'
}
export type MDXTextExpression = MDXExpression & {
type: 'mdxTextExpression'
}
export type MDXEsm = Node & {
type: 'mdxjsEsm'
value: string
}
export type MDXJsxAttributeValueExpression =
import('mdast-util-mdx-jsx').MDXJsxAttributeValueExpression
export type MDXJsxAttribute = import('mdast-util-mdx-jsx').MDXJsxAttribute
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 MDXJSEsm = import('mdast-util-mdxjs-esm').MDXJSEsm
export type Info = ReturnType<typeof find>
export type Space = 'html' | 'svg'
export type Handle = (node: Node, context: Context) => EstreeJsxChild | null
export type Handle = (node: any, context: Context) => EstreeJsxChild | null
export type Options = {
space?: Space
handlers?: {
[x: string]: Handle
}
space?: Space | undefined
handlers?:
| {
[x: string]: Handle
}
| undefined
}

@@ -81,0 +71,0 @@ export type Context = {

/**
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Node} UnistNode
* @typedef {import('hast').Parent} Parent

@@ -9,2 +9,3 @@ * @typedef {import('hast').Root} Root

* @typedef {import('hast').Properties} Properties
* @typedef {Root['children'][number]|Root} Node
* @typedef {import('estree-jsx').Node} EstreeNode

@@ -24,2 +25,4 @@ * @typedef {import('estree-jsx').Program} EstreeProgram

* @typedef {import('estree-jsx').Property} EstreeProperty
* @typedef {import('estree-jsx').JSXIdentifier} JSXIdentifier
* @typedef {import('estree-jsx').JSXMemberExpression} JSXMemberExpression
*

@@ -29,16 +32,13 @@ * @typedef {EstreeJsxOpeningElement['name']} EstreeJsxElementName

* @typedef {EstreeJsxElement['children'][number]} EstreeJsxChild
* @typedef {Element['children'][number]} ElementChild
*
* @typedef {Node & {type: 'mdxJsxAttributeValueExpression', value: string}} MDXJsxAttributeValueExpression
* @typedef {Node & {type: 'mdxJsxAttribute', name: string, value: (MDXJsxAttributeValueExpression|string)?}} MDXJsxAttribute
* @typedef {Node & {type: 'mdxJsxExpressionAttribute', value: string}} MDXJsxExpressionAttribute
* @typedef {Parent & {name: string|null, attributes: Array.<MDXJsxExpressionAttribute|MDXJsxAttribute>}} MDXJsxElement
* @typedef {MDXJsxElement & {type: 'mdxJsxFlowElement', children: Array.<MDXJsxFlowElement|ElementChild>}} MDXJsxFlowElement
* @typedef {MDXJsxElement & {type: 'mdxJsxTextElement', children: Array.<MDXJsxTextElement|ElementChild>}} MDXJsxTextElement
* @typedef {import('mdast-util-mdx-jsx').MDXJsxAttributeValueExpression} MDXJsxAttributeValueExpression
* @typedef {import('mdast-util-mdx-jsx').MDXJsxAttribute} MDXJsxAttribute
* @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 {Node & {value: string}} MDXExpression
* @typedef {MDXExpression & {type: 'mdxFlowExpression'}} MDXFlowExpression
* @typedef {MDXExpression & {type: 'mdxTextExpression'}} MDXTextExpression
* @typedef {import('mdast-util-mdx-expression').MDXFlowExpression} MDXFlowExpression
* @typedef {import('mdast-util-mdx-expression').MDXTextExpression} MDXTextExpression
*
* @typedef {Node & {type: 'mdxjsEsm', value: string}} MDXEsm
* @typedef {import('mdast-util-mdxjs-esm').MDXJSEsm} MDXJSEsm
*

@@ -48,3 +48,3 @@ * @typedef {ReturnType<find>} Info

*
* @typedef {(node: Node, context: Context) => EstreeJsxChild?} Handle
* @typedef {(node: any, context: Context) => EstreeJsxChild?} Handle
*

@@ -75,7 +75,8 @@ * @typedef Options

var own = {}.hasOwnProperty
var push = [].push
const toReact = /** @type {Record<string, string>} */ (hastToReact)
const own = {}.hasOwnProperty
/**
* @param {Node} tree
* @param {Node|MDXJsxAttributeValueExpression|MDXJsxAttribute|MDXJsxExpressionAttribute|MDXJsxFlowElement|MDXJsxTextElement|MDXFlowExpression|MDXTextExpression} tree
* @param {Options} options

@@ -86,9 +87,12 @@ * @returns {EstreeProgram}

/** @type {Context} */
var context = {
const context = {
schema: options.space === 'svg' ? svg : html,
comments: [],
esm: [],
// @ts-expect-error: hush.
handle: zwitch('type', {
invalid,
// @ts-expect-error: hush.
unknown,
// @ts-expect-error: hush.
handlers: Object.assign(

@@ -112,4 +116,4 @@ {},

}
var result = context.handle(tree, context)
var body = context.esm
let result = context.handle(tree, context)
const body = context.esm

@@ -126,3 +130,3 @@ if (result) {

// @ts-ignore Types are wrong (`expression` *can* be JSX).
// @ts-expect-error Types are wrong (`expression` *can* be JSX).
body.push(create(tree, {type: 'ExpressionStatement', expression: result}))

@@ -161,3 +165,3 @@ }

function comment(node, context) {
var esnode = inherit(node, {type: 'Block', value: node.value})
const esnode = inherit(node, {type: 'Block', value: node.value})

@@ -182,23 +186,5 @@ context.comments.push(esnode)

function element(node, context) {
var parentSchema = context.schema
var schema = parentSchema
var props = node.properties || {}
/** @type {Array<EstreeJsxAttribute|EstreeJsxSpreadAttribute>} */
var attributes = []
/** @type {Array.<EstreeJsxChild>} */
var children
/** @type {Info} */
var info
/** @type {string} */
var prop
/** @type {string} */
var cssProp
/** @type {Array.<EstreeProperty>} */
var cssProperties
/** @type {Properties[string]} */
var value
/** @type {EstreeJsxAttribute['value']} */
var attributeValue
/** @type {Object.<string, string>} */
var styleValue
const parentSchema = context.schema
let schema = parentSchema
const props = node.properties || {}

@@ -210,8 +196,14 @@ if (parentSchema.space === 'html' && node.tagName.toLowerCase() === 'svg') {

children = all(node, context)
const children = all(node, context)
/** @type {Array<EstreeJsxAttribute|EstreeJsxSpreadAttribute>} */
const attributes = []
/** @type {string} */
let prop
for (prop in props) {
if (own.call(props, prop)) {
value = props[prop]
info = find(schema, prop)
let value = props[prop]
const info = find(schema, prop)
/** @type {EstreeJsxAttribute['value']} */
let attributeValue

@@ -231,3 +223,3 @@ // Ignore nullish and `NaN` values.

prop = info.space
? hastToReact[info.property] || info.property
? toReact[info.property] || info.property
: info.attribute

@@ -242,7 +234,11 @@

if (prop === 'style') {
// @ts-ignore Assume `value` is then an object.
styleValue =
/** @type {Object.<string, string>} */
// @ts-expect-error Assume `value` is then an object.
const styleValue =
typeof value === 'string' ? parseStyle(value, node.tagName) : value
cssProperties = []
/** @type {Array.<EstreeProperty>} */
const cssProperties = []
/** @type {string} */
let cssProp

@@ -292,4 +288,4 @@ for (cssProp in styleValue) {

key: {type: 'Literal', value: String(prop)},
// @ts-ignore No need to worry about `style` (which has a `JSXExpressionContainer`
// value) because that’s a valid identifier.
// @ts-expect-error No need to worry about `style` (which has a
// `JSXExpressionContainer` value) because that’s a valid identifier.
value: attributeValue || {type: 'Literal', value: true},

@@ -325,3 +321,3 @@ kind: 'init'

/**
* @param {MDXEsm} node
* @param {MDXJSEsm} node
* @param {Context} context

@@ -332,10 +328,10 @@ * @returns {void}

/** @type {EstreeProgram} */
// @ts-ignore Assume program.
var estree = node.data && node.data.estree
var comments = (estree && estree.comments) || []
// @ts-expect-error Assume program.
const estree = node.data && node.data.estree
const comments = (estree && estree.comments) || []
if (estree) {
push.apply(context.comments, comments)
context.comments.push(...comments)
attachComments(estree, comments)
push.apply(context.esm, estree.body)
context.esm.push(...estree.body)
}

@@ -351,14 +347,16 @@ }

/** @type {EstreeProgram} */
// @ts-ignore Assume program.
var estree = node.data && node.data.estree
/** @type {EstreeExpression} */
var expression
// @ts-expect-error Assume program.
const estree = node.data && node.data.estree
const comments = (estree && estree.comments) || []
/** @type {EstreeExpression|undefined} */
let expression
if (estree) {
push.apply(context.comments, estree.comments)
context.comments.push(...comments)
attachComments(estree, estree.comments)
expression =
estree.body[0] &&
estree.body[0].type === 'ExpressionStatement' &&
estree.body[0].expression
(estree.body[0] &&
estree.body[0].type === 'ExpressionStatement' &&
estree.body[0].expression) ||
undefined
}

@@ -379,22 +377,6 @@

function mdxJsxElement(node, context) {
var parentSchema = context.schema
var schema = parentSchema
var attrs = node.attributes || []
var index = -1
/** @type {Array<EstreeJsxAttribute|EstreeJsxSpreadAttribute>} */
var attributes = []
/** @type {Array.<EstreeJsxChild>} */
var children
/** @type {MDXJsxExpressionAttribute|MDXJsxAttribute} */
var attr
/** @type {MDXJsxAttributeValueExpression|string} */
var value
/** @type {EstreeExpression} */
var expression
/** @type {EstreeProgram} */
var estree
/** @type {EstreeJsxAttribute['value']} */
var attributeValue
/** @type {EstreeJsxSpreadAttribute['argument']} */
var argumentValue
const parentSchema = context.schema
let schema = parentSchema
const attrs = node.attributes || []
let index = -1

@@ -410,7 +392,11 @@ if (

children = all(node, context)
const children = all(node, context)
/** @type {Array<EstreeJsxAttribute|EstreeJsxSpreadAttribute>} */
const attributes = []
while (++index < attrs.length) {
attr = attrs[index]
value = attr.value
const attr = attrs[index]
const value = attr.value
/** @type {EstreeJsxAttribute['value']} */
let attributeValue

@@ -424,13 +410,19 @@ if (attr.type === 'mdxJsxAttribute') {

else if (typeof value === 'object') {
// @ts-ignore Assume program.
estree = value.data && value.data.estree
expression = null
/** @type {EstreeProgram} */
// @ts-expect-error Assume program.
const estree = value.data && value.data.estree
const comments = (estree && estree.comments) || []
/** @type {EstreeExpression|undefined} */
let expression
if (estree) {
push.apply(context.comments, estree.comments)
context.comments.push(...comments)
attachComments(estree, estree.comments)
// Should exist.
/* c8 ignore next 5 */
expression =
estree.body[0] &&
estree.body[0].type === 'ExpressionStatement' &&
estree.body[0].expression
(estree.body[0] &&
estree.body[0].type === 'ExpressionStatement' &&
estree.body[0].expression) ||
undefined
}

@@ -458,18 +450,24 @@

else {
// @ts-ignore Assume program.
estree = attr.data && attr.data.estree
argumentValue = null
/** @type {EstreeProgram} */
// @ts-expect-error Assume program.
const estree = attr.data && attr.data.estree
const comments = (estree && estree.comments) || []
/** @type {EstreeJsxSpreadAttribute['argument']|undefined} */
let argumentValue
if (estree) {
push.apply(context.comments, estree.comments)
context.comments.push(...comments)
attachComments(estree, estree.comments)
// Should exist.
/* c8 ignore next 10 */
argumentValue =
estree.body[0] &&
estree.body[0].type === 'ExpressionStatement' &&
estree.body[0].expression &&
estree.body[0].expression.type === 'ObjectExpression' &&
estree.body[0].expression.properties &&
estree.body[0].expression.properties[0] &&
estree.body[0].expression.properties[0].type === 'SpreadElement' &&
estree.body[0].expression.properties[0].argument
(estree.body[0] &&
estree.body[0].type === 'ExpressionStatement' &&
estree.body[0].expression &&
estree.body[0].expression.type === 'ObjectExpression' &&
estree.body[0].expression.properties &&
estree.body[0].expression.properties[0] &&
estree.body[0].expression.properties[0].type === 'SpreadElement' &&
estree.body[0].expression.properties[0].argument) ||
undefined
}

@@ -521,14 +519,12 @@

function root(node, context) {
var children = all(node, context)
const children = all(node, context)
/** @type {Array.<EstreeJsxChild>} */
var cleanChildren = []
var index = -1
/** @type {Array.<EstreeJsxChild>} */
var queue
/** @type {EstreeJsxChild} */
var child
const cleanChildren = []
let index = -1
/** @type {Array.<EstreeJsxChild>|undefined} */
let queue
// Remove surrounding whitespace nodes from the fragment.
while (++index < children.length) {
child = children[index]
const child = children[index]

@@ -540,7 +536,5 @@ if (

) {
if (queue) {
queue.push(child)
}
if (queue) queue.push(child)
} else {
push.apply(cleanChildren, queue)
if (queue) cleanChildren.push(...queue)
cleanChildren.push(child)

@@ -561,6 +555,6 @@ queue = []

* @param {Text} node
* @returns {EstreeJsxExpressionContainer}
* @returns {EstreeJsxExpressionContainer|void}
*/
function text(node) {
var value = String(node.value || '')
const value = String(node.value || '')

@@ -576,3 +570,3 @@ if (!value) return

/**
* @param {Parent} parent
* @param {Parent|MDXJsxFlowElement|MDXJsxTextElement} parent
* @param {Context} context

@@ -582,11 +576,9 @@ * @returns {Array.<EstreeJsxChild>}

function all(parent, context) {
var children = parent.children || []
var index = -1
const children = parent.children || []
let index = -1
/** @type {Array.<EstreeJsxChild>} */
var results = []
/** @type {EstreeJsxChild|Array.<EstreeJsxChild>} */
var result
const results = []
while (++index < children.length) {
result = context.handle(children[index], context)
const result = context.handle(children[index], context)

@@ -607,3 +599,3 @@ if (Array.isArray(result)) {

* @template {EstreeNode|EstreeComment} T
* @param {Node} hast
* @param {Node|MDXJsxAttributeValueExpression|MDXJsxAttribute|MDXJsxExpressionAttribute|MDXJsxFlowElement|MDXJsxTextElement|MDXFlowExpression|MDXTextExpression} hast
* @param {T} esnode

@@ -613,7 +605,7 @@ * @returns {T}

function inherit(hast, esnode) {
var left = hast.data
/** @type {Object.<string, unknown>} */
var right
const left = hast.data
/** @type {Object.<string, unknown>|undefined} */
let right
/** @type {string} */
var key
let key

@@ -631,3 +623,3 @@ create(hast, esnode)

if (right) {
// @ts-ignore `esast` extension.
// @ts-expect-error `esast` extension.
esnode.data = right

@@ -644,3 +636,3 @@ }

* @template {EstreeNode|EstreeComment} T
* @param {Node} hast
* @param {Node|MDXJsxAttributeValueExpression|MDXJsxAttribute|MDXJsxExpressionAttribute|MDXJsxFlowElement|MDXJsxTextElement|MDXFlowExpression|MDXTextExpression} hast
* @param {T} esnode

@@ -650,8 +642,12 @@ * @returns {T}

function create(hast, esnode) {
var p = position(hast)
const p = position(hast)
if (p.start.line) {
// @ts-ignore acorn-style.
if (
p.start.line &&
p.start.offset !== undefined &&
p.end.offset !== undefined
) {
// @ts-expect-error acorn-style.
esnode.start = p.start.offset
// @ts-ignore acorn-style.
// @ts-expect-error acorn-style.
esnode.end = p.end.offset

@@ -682,20 +678,23 @@ esnode.loc = {

function (name, attribute) {
/** @type {Array.<string>} */
var parts
/** @type {EstreeJsxElementName} */
var node
if (!attribute && name.includes('.')) {
const parts = name.split('.')
let part = parts.shift()
/** @type {JSXIdentifier|JSXMemberExpression} */
// @ts-expect-error: hush, the first is always defined.
let node = {type: 'JSXIdentifier', name: part}
if (!attribute && name.includes('.')) {
parts = name.split('.')
node = {type: 'JSXIdentifier', name: parts.shift()}
while (parts.length > 0) {
while ((part = parts.shift())) {
node = {
type: 'JSXMemberExpression',
object: node,
property: {type: 'JSXIdentifier', name: parts.shift()}
property: {type: 'JSXIdentifier', name: part}
}
}
} else if (name.includes(':')) {
parts = name.split(':')
node = {
return node
}
if (name.includes(':')) {
const parts = name.split(':')
return {
type: 'JSXNamespacedName',

@@ -705,7 +704,5 @@ namespace: {type: 'JSXIdentifier', name: parts[0]},

}
} else {
node = {type: 'JSXIdentifier', name}
}
return node
return {type: 'JSXIdentifier', name}
}

@@ -721,3 +718,3 @@ )

/** @type {Object.<string, string>} */
var result = {}
const result = {}

@@ -761,3 +758,3 @@ try {

function jsxIdentifierName(name) {
var index = -1
let index = -1

@@ -764,0 +761,0 @@ while (++index < name.length) {

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

@@ -49,2 +49,4 @@ "license": "MIT",

"hast-util-whitespace": "^2.0.0",
"mdast-util-mdx-expression": "^1.0.0",
"mdast-util-mdxjs-esm": "^1.0.0",
"property-information": "^6.0.0",

@@ -68,13 +70,13 @@ "space-separated-tokens": "^2.0.0",

"estree-util-build-jsx": "^2.0.0",
"hast-util-from-parse5": "^6.0.0",
"hast-util-from-parse5": "^7.0.0",
"hastscript": "^7.0.0",
"mdast-util-from-markdown": "^0.8.0",
"mdast-util-mdx": "^0.1.0",
"mdast-util-from-markdown": "^1.0.0",
"mdast-util-mdx": "^1.0.0",
"mdast-util-to-hast": "^11.0.0",
"micromark-extension-mdxjs": "^0.3.0",
"micromark-extension-mdxjs": "^1.0.0",
"parse5": "^6.0.0",
"prettier": "^2.0.0",
"recast": "^0.20.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"remark-cli": "^10.0.0",
"remark-preset-wooorm": "^9.0.0",
"rimraf": "^3.0.0",

@@ -84,5 +86,5 @@ "tape": "^5.0.0",

"typescript": "^4.0.0",
"unist-util-visit": "^3.0.0",
"vfile": "^4.0.0",
"xo": "^0.39.0"
"unist-util-visit": "^4.0.0",
"vfile": "^5.0.0",
"xo": "^0.44.0"
},

@@ -106,7 +108,3 @@ "scripts": {

"xo": {
"prettier": true,
"rules": {
"no-var": "off",
"prefer-arrow-callback": "off"
}
"prettier": true
},

@@ -122,4 +120,8 @@ "remarkConfig": {

"strict": true,
"ignoreCatch": true
"ignoreCatch": true,
"#": "needed any",
"ignoreFiles": [
"index.d.ts"
]
}
}

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

```js
import fs from 'fs'
import fs from 'node:fs'
import parse5 from 'parse5'

@@ -58,5 +58,5 @@ import {fromParse5} from 'hast-util-from-parse5'

var hast = fromParse5(parse5.parse(String(fs.readFileSync('example.html'))))
const hast = fromParse5(parse5.parse(String(fs.readFileSync('example.html'))))
var estree = toEstree(hast)
const estree = toEstree(hast)

@@ -63,0 +63,0 @@ estree.comments = null // `recast` doesn’t like comments on the root.

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