Registry / testing / esutils

esutils

JSON →
library1.0.1jsnpmunverified

ESUtils is a foundational utility library for ECMAScript language tools, providing a 'utility box' for analyzing JavaScript Abstract Syntax Trees (ASTs), character codes, and keywords/reserved words based on specific ECMA262 editions. It offers functions to classify AST nodes (e.g., `isExpression`, `isStatement`), identify character types (e.g., `isDecimalDigit`, `isIdentifierStart`), and check for keywords (`isKeywordES5`, `isKeywordES6`). The current stable version is 2.0.3, released in 2018. Given its core utility nature, its API is highly stable, and its release cadence is low, typically for maintenance or specification updates. It serves as a building block for various JavaScript parsing, linting, and transformation projects, providing low-level, spec-compliant checks.

npm install esutils
INSTALL
IMPORT
SIG · ESUTILS
E
esutils
testingjavascriptv1.0.1
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.

esutils
import esutils from 'esutils';
import { ast, code, keyword } from 'esutils';
The library primarily exports a default object containing `ast`, `code`, and `keyword` sub-modules. Named imports for these sub-modules directly are not supported.
esutils.ast
import esutils from 'esutils'; const isExpression = esutils.ast.isExpression;
const ast = require('esutils').ast;
CommonJS users should `require('esutils')` to get the full object, then access properties like `ast`.
esutils.keyword
import esutils from 'esutils'; const isKeywordES6 = esutils.keyword.isKeywordES6;
import { isKeywordES6 } from 'esutils';
Individual functions like `isKeywordES6` are properties of the `keyword` sub-module, which is itself a property of the default `esutils` export. Direct named imports are incorrect.

Demonstrates basic usage of `esutils` to check AST node types, character codes, and ECMAScript keywords.

import esutils from 'esutils'; // Example AST node (simplified for demonstration) const ifStatementNode = { type: 'IfStatement', test: { type: 'Identifier', name: 'condition' }, consequent: { type: 'BlockStatement', body: [] }, alternate: null }; // 1. Using AST utilities console.log(`Is 'ifStatementNode' an Expression? ${esutils.ast.isExpression(ifStatementNode)}`); console.log(`Is 'ifStatementNode' a Statement? ${esutils.ast.isStatement(ifStatementNode)}`); // 2. Using code utilities (character codes) const charCodeA = 'A'.charCodeAt(0); const charCode5 = '5'.charCodeAt(0); console.log(`Is charCode 'A' a decimal digit? ${esutils.code.isDecimalDigit(charCodeA)}`); console.log(`Is charCode '5' a decimal digit? ${esutils.code.isDecimalDigit(charCode5)}`); console.log(`Is charCode 'A' an identifier start? ${esutils.code.isIdentifierStart(charCodeA)}`); // 3. Using keyword utilities const identifier = 'const'; const strictIdentifier = 'yield'; console.log(`Is '${identifier}' a keyword in ES6? ${esutils.keyword.isKeywordES6(identifier, false)}`); console.log(`Is '${strictIdentifier}' a keyword in ES6 strict mode? ${esutils.keyword.isKeywordES6(strictIdentifier, true)}`);
Debug
Known issues
gotchaWhen checking for keywords or reserved words, ensure you use the correct ECMA262 edition function (e.g., `isKeywordES5` vs. `isKeywordES6`) based on your target JavaScript environment or desired spec compliance. Using the wrong version can lead to incorrect parsing or validation results.
fix
Always explicitly choose `esutils.keyword.isKeywordES5`, `isKeywordES6`, `isReservedWordES5`, or `isReservedWordES6` according to the ECMAScript version you intend to target.
affects: >=2.0.0
gotchaThe `ast.isProblematicIfStatement` function identifies specific, unusual `IfStatement` structures that cannot be faithfully represented in standard JavaScript code due to ambiguity in `else` clause association (e.g., a dangling `else`). This is an edge case for advanced AST manipulation and should be understood carefully.
fix
Consult the official `esutils` documentation for `isProblematicIfStatement` to understand its precise definition and implications before relying on it for AST transformations.
affects: >=2.0.0
gotchaThe character code utilities (e.g., `isIdentifierStart`, `isIdentifierPart`) operate on raw Unicode code points. While they follow ECMA262 definitions, handling complex Unicode identifiers (e.g., those involving supplementary planes or combining characters) requires careful pre-processing or a more sophisticated Unicode-aware parser if full compliance for all possible characters is needed beyond basic ASCII/Latin-1 ranges.
fix
For basic character checks, the functions are sufficient. For full Unicode identifier validation, consider pairing `esutils` with a dedicated Unicode normalization library or a parser that handles identifier processing for all Unicode ranges.
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'isExpression')
Attempting to access `esutils.ast.isExpression` when `esutils` was not correctly imported or resolved.
fix
Ensure `esutils` is properly imported: `import esutils from 'esutils';` for ESM or `const esutils = require('esutils');` for CommonJS, then access `esutils.ast.isExpression`.
ReferenceError: require is not defined
Using `require()` syntax in an ECMAScript Module (ESM) context without a bundler or transpiler set up to handle CommonJS imports.
fix
Switch to ESM `import esutils from 'esutils';` or configure your build system (e.g., Webpack, Rollup, Babel) to transpile CommonJS `require` calls in ESM environments.
TypeError: esutils.ast is not a function
Attempting to call `esutils.ast` as a function, or incorrectly assuming `ast` is directly exported.
fix
`esutils.ast` is an object containing utility functions, not a function itself. Access its methods like `esutils.ast.isExpression(node)`.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
esutils — npm install esutils · libregistry