esast-util-from-js is a utility within the unified ecosystem designed to parse JavaScript code strings into an esast syntax tree, which is an extension of the ESTree format. It integrates seamlessly with `unist` and `vfile` for processing abstract syntax trees in a structured manner. The current stable version is 2.0.1, with major releases occurring periodically to update Node.js compatibility and internal dependencies. It differentiates itself from raw `acorn` usage by providing a higher-level abstraction suitable for unified processing pipelines, making it ideal for linters, formatters, and other tools that operate on ASTs alongside other types of content. The library primarily focuses on converting serialised JavaScript into its programmatic representation, offering options for specifying JS version, module type, and other parsing rules.
npm install esast-util-from-jsVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to parse a JavaScript string into an esast syntax tree, specifying it as a module and a specific ES version, then logs key properties of the resulting AST.
Upgrade your Node.js environment to version 16 or newer. For projects using nvm: `nvm install 16 && nvm use 16`.
Ensure your project is configured for ESM and use `import` statements. Avoid directly importing from deep paths that are not explicitly exported. Update your `package.json` to include `"type": "module"` if necessary, or rename your files to `.mjs`.
When providing binary input, ensure you use `Uint8Array`, which `Buffer` is a superclass of. Most users passing `string` inputs will not be directly affected, but custom integrations might need updates.
To lock down the JavaScript version being parsed, explicitly pass a year (e.g., `{ version: 2023 }`) instead of `'latest'` to ensure consistent parsing behavior.Change your import statement from `const { fromJs } = require('esast-util-from-js')` to `import { fromJs } from 'esast-util-from-js'`. Ensure your project is set up for ES Modules (e.g., `"type": "module"` in `package.json` or using `.mjs` file extensions).If using modern JavaScript features, try explicitly setting the `version` option to a newer year (e.g., `{ version: 2023 }`). If it's genuinely malformed JavaScript, correct the syntax errors in your input code.Inspect the detailed error message within the `VFileMessage` for specifics. This often indicates malformed syntax, missing plugins for non-standard syntax (like JSX), or an incorrect `module` option for the source code type (e.g., parsing ESM as a script).