Registry / serialization / babel-jsx-utils

babel-jsx-utils

JSON →
library1.1.0jsnpmunverified

babel-jsx-utils (v1.1.0) is a utility library for resolving JSX attribute values at compile time using Babel. It statically evaluates attribute expressions in the local scope, handling variables, literals, and simple expressions but not function calls or props passed from parent components. This is primarily used in custom Babel plugins for static analysis or code transformations. Released infrequently, it is a lightweight alternative to more complex AST utilities. Key differentiator: focuses specifically on JSX attribute resolution with a simple API (getAttributeValues, getAttributeValue) and built-in error handling for unresolvable attributes.

npm install babel-jsx-utils
INSTALL
IMPORT
SIG · BABEL-JSX-UTILS
B
babel-jsx-utils
serializationjavascriptv1.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

getAttributeValues
import { getAttributeValues } from 'babel-jsx-utils'
const { getAttributeValues } = require('babel-jsx-utils')
ESM native; CJS require works but may cause issues in some tooling
getAttributeValue
import { getAttributeValue } from 'babel-jsx-utils'
import getAttributeValue from 'babel-jsx-utils'
This is a named export, not a default export
CoreNodePath
import type { CoreNodePath } from 'babel-jsx-utils'
TypeScript type export; only needed if using TypeScript for type annotations

Shows how to parse JSX, traverse AST, and resolve attribute values using getAttributeValues.

import { parse, traverse } from '@babel/core'; import { getAttributeValues } from 'babel-jsx-utils'; const code = `function Logo() { const src = 'trex.png'; return <img src={src} alt="T-Rex" />; }`; const ast = parse(code, { filename: 'test.jsx', presets: ['@babel/preset-react'] }); traverse(ast, { JSXOpeningElement(path) { const values = getAttributeValues(path); console.log(values); // { src: 'trex.png', alt: 'T-Rex' } } });
Debug
Known issues
gotchagetAttributeValues cannot resolve function calls or props passed from a parent component; it only evaluates local variables and simple expressions.
fix
Ensure attribute values are local literals, variables, or simple expressions (e.g., a + b). For dynamic props, consider runtime evaluation or a different approach.
affects: >=1.0.0
gotchaSpread attributes are not resolved individually; the entire spread is skipped.
fix
Do not rely on spread attributes being included in the result; handle them separately.
affects: >=1.0.0
gotchaThe return type of getAttributeValue includes `true` for boolean attributes without a value (e.g., <Image eager />), which may conflict with other value types.
fix
Check for `true` explicitly or use TypeScript to handle the union type `T | true`.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot resolve attribute 'src': no scope information
The attribute value references a variable or expression that is not statically analyzable in the current Babel scope.
fix
Ensure the variable is defined in the same function scope and is a literal or simple expression. Alternatively, pass a custom onError callback to handle unresolved attributes.
TypeError: (0 , _babelJsxUtils.getAttributeValues) is not a function
CommonJS require style used instead of named import, potentially due to mixing CJS and ESM.
fix
Use correct named import: import { getAttributeValues } from 'babel-jsx-utils'
Module '"babel-jsx-utils"' has no exported member 'getAttributeValues'
Attempting to import a non-existent symbol or using default import when only named exports exist.
fix
Check import statement: use import { getAttributeValues } from 'babel-jsx-utils'
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
13 hits · last 30 days
node
12
OpenAI (training)
1
Resources
babel-jsx-utils — npm install babel-jsx-utils · libregistry