jsx-ast-utils is a dedicated utility module designed for the static analysis of JSX Abstract Syntax Tree (AST) nodes. It provides functions to query, validate, and extract information from JSX elements, particularly focusing on prop existence and values. Currently stable at version 3.3.5, it offers a consistent API for working with JSX ASTs, making it an invaluable tool for authors of linting rules, code transformers, or other static analysis tools. Originally extracted from eslint-plugin-jsx-a11y, its primary differentiator is its focused scope on JSX syntax, providing robust and tested utilities that account for various JSX complexities including spread attributes and case insensitivity.
npm install jsx-ast-utilsVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to use `hasProp` within a simplified ESLint-like visitor function to check for prop existence on JSX elements, including handling spread attributes and case insensitivity.
Review any code that interacts with `getProp` or related utilities to ensure it correctly handles the guaranteed return of a value, even if the prop is not found or malformed. Adjust checks from `if (!prop)` to `if (prop === undefined)` if strict non-existence handling is needed.
To correctly check for props that might be included through spread attributes, explicitly set `spreadStrict: false` in the options object: `hasProp(attributes, 'myProp', { spreadStrict: false })`.If strict case-sensitive matching is required for a prop, you must implement a manual check. This can involve iterating through the `node.attributes` array and performing a case-sensitive comparison on `attribute.name.name`.
Upgrade to `jsx-ast-utils@^2.1.0` or newer if working with TypeScript or TSX files to ensure proper AST node handling and prevent unexpected behavior.
Before accessing `node.attributes` or passing a node to `jsx-ast-utils` functions expecting JSX attributes, ensure that `node.type === 'JSXOpeningElement'` (or a similar check for relevant JSX node types).
Switch to ESM import syntax: `import { hasProp } from 'jsx-ast-utils';` for named exports.For CommonJS, ensure you are correctly destructuring the named export: `const { hasProp } = require('jsx-ast-utils');`.No dependency data recorded yet.