{"id":13156,"library":"esutils","title":"ESUtils","description":"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.","status":"maintenance","version":"2.0.3","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/estools/esutils","tags":["javascript"],"install":[{"cmd":"npm install esutils","lang":"bash","label":"npm"},{"cmd":"yarn add esutils","lang":"bash","label":"yarn"},{"cmd":"pnpm add esutils","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library primarily exports a default object containing `ast`, `code`, and `keyword` sub-modules. Named imports for these sub-modules directly are not supported.","wrong":"import { ast, code, keyword } from 'esutils';","symbol":"esutils","correct":"import esutils from 'esutils';"},{"note":"CommonJS users should `require('esutils')` to get the full object, then access properties like `ast`.","wrong":"const ast = require('esutils').ast;","symbol":"esutils.ast","correct":"import esutils from 'esutils';\nconst isExpression = esutils.ast.isExpression;"},{"note":"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.","wrong":"import { isKeywordES6 } from 'esutils';","symbol":"esutils.keyword","correct":"import esutils from 'esutils';\nconst isKeywordES6 = esutils.keyword.isKeywordES6;"}],"quickstart":{"code":"import esutils from 'esutils';\n\n// Example AST node (simplified for demonstration)\nconst ifStatementNode = {\n  type: 'IfStatement',\n  test: { type: 'Identifier', name: 'condition' },\n  consequent: { type: 'BlockStatement', body: [] },\n  alternate: null\n};\n\n// 1. Using AST utilities\nconsole.log(`Is 'ifStatementNode' an Expression? ${esutils.ast.isExpression(ifStatementNode)}`);\nconsole.log(`Is 'ifStatementNode' a Statement? ${esutils.ast.isStatement(ifStatementNode)}`);\n\n// 2. Using code utilities (character codes)\nconst charCodeA = 'A'.charCodeAt(0);\nconst charCode5 = '5'.charCodeAt(0);\nconsole.log(`Is charCode 'A' a decimal digit? ${esutils.code.isDecimalDigit(charCodeA)}`);\nconsole.log(`Is charCode '5' a decimal digit? ${esutils.code.isDecimalDigit(charCode5)}`);\nconsole.log(`Is charCode 'A' an identifier start? ${esutils.code.isIdentifierStart(charCodeA)}`);\n\n// 3. Using keyword utilities\nconst identifier = 'const';\nconst strictIdentifier = 'yield';\nconsole.log(`Is '${identifier}' a keyword in ES6? ${esutils.keyword.isKeywordES6(identifier, false)}`);\nconsole.log(`Is '${strictIdentifier}' a keyword in ES6 strict mode? ${esutils.keyword.isKeywordES6(strictIdentifier, true)}`);\n","lang":"javascript","description":"Demonstrates basic usage of `esutils` to check AST node types, character codes, and ECMAScript keywords."},"warnings":[{"fix":"Always explicitly choose `esutils.keyword.isKeywordES5`, `isKeywordES6`, `isReservedWordES5`, or `isReservedWordES6` according to the ECMAScript version you intend to target.","message":"When 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.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Consult the official `esutils` documentation for `isProblematicIfStatement` to understand its precise definition and implications before relying on it for AST transformations.","message":"The `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.","severity":"gotcha","affected_versions":">=2.0.0"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure `esutils` is properly imported: `import esutils from 'esutils';` for ESM or `const esutils = require('esutils');` for CommonJS, then access `esutils.ast.isExpression`.","cause":"Attempting to access `esutils.ast.isExpression` when `esutils` was not correctly imported or resolved.","error":"TypeError: Cannot read properties of undefined (reading 'isExpression')"},{"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.","cause":"Using `require()` syntax in an ECMAScript Module (ESM) context without a bundler or transpiler set up to handle CommonJS imports.","error":"ReferenceError: require is not defined"},{"fix":"`esutils.ast` is an object containing utility functions, not a function itself. Access its methods like `esutils.ast.isExpression(node)`.","cause":"Attempting to call `esutils.ast` as a function, or incorrectly assuming `ast` is directly exported.","error":"TypeError: esutils.ast is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}