acorn-jsx is a plugin for the Acorn JavaScript parser that provides capabilities to parse React.js JSX syntax. It transforms JSX source code into an Abstract Syntax Tree (AST), which is then consumed by various language tools and transpilers like Babel and Buble. The current stable version is 5.3.2. This package maintains a moderate release cadence, often aligning with updates to the Acorn parser itself or changes in the JSX specification. Its key differentiators include its small footprint, speed, and JavaScript-only implementation, making it a widely adopted core component for JSX parsing in the JavaScript ecosystem. It strictly focuses on parsing to AST, rather than performing any transpilation to ES5-compliant JavaScript.
npm install acorn-jsxVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to load the `acorn` parser and extend it with the `acorn-jsx` plugin to parse a JSX code snippet into an AST.
Ensure that `}` and `>` characters within JSX text are properly escaped (e.g., `{'}'}` or `>`) if they are not intended to be part of JSX expressions or tags. Review existing JSX text for compliance.Avoid using mixed XML namespaces and object-style access in tag names. If you absolutely require this non-standard behavior, you can explicitly enable it by passing `{ allowNamespacedObjects: true }` to the `jsx()` plugin function.If your application does not use JSX namespaces and you want to prohibit them (e.g., for stricter parsing or to prevent unexpected syntax), explicitly pass `{ allowNamespaces: false }` to the `jsx()` plugin function when extending the parser.Install `acorn` explicitly via `npm install acorn` or `yarn add acorn`, ensuring the installed version matches the peer dependency range specified by `acorn-jsx`.
Escape special characters like `}` or `>` in JSX text (e.g., by wrapping them in `{}` as `{'}'}` or using HTML entities like `>`) or ensure they are part of valid JSX expressions/tags.Verify that `const acorn = require('acorn');` is correctly executed and that you are calling `jsx()` as `acorn.Parser.extend(jsx()).parse(...)`.If this syntax is absolutely necessary (though not spec-compliant), pass `{ allowNamespacedObjects: true }` to the `jsx()` plugin function: `acorn.Parser.extend(jsx({ allowNamespacedObjects: true }))`.Install a compatible version of `acorn` using `npm install acorn@^8` (or `^6`, `^7` depending on preference) to satisfy the peer dependency warning.