Get Assigned Identifiers
The `get-assigned-identifiers` package provides a focused utility for JavaScript AST analysis, specifically designed to extract identifiers that are initialized by a given AST node, including those within destructuring assignments. It helps developers programmatically understand which variables are being declared or assigned in a specific scope, a common requirement for static analysis tools, code linters, and refactoring utilities. The current stable version is 1.2.0, which was published over six years ago, indicating a mature but infrequently updated library. Its primary differentiator is its single-purpose API, which allows for direct integration without the overhead of larger AST manipulation frameworks. While stable for its intended use cases, users should be aware of its age when integrating with very recent JavaScript syntax or modern AST parsers, as compatibility might not be actively maintained.
Common errors
-
TypeError: getAssignedIdentifiers is not a function
cause Attempting to use `getAssignedIdentifiers` before it's properly imported or if the imported value is `undefined`.fixEnsure you are using the correct import statement for your module system: `const getAssignedIdentifiers = require('get-assigned-identifiers')` for CommonJS or `import getAssignedIdentifiers from 'get-assigned-identifiers'` for ESM. -
SyntaxError: Cannot use import statement outside a module
cause Attempting to use ESM `import` syntax in a CommonJS context (e.g., a `.js` file without `"type": "module"` in `package.json` or run with `node --experimental-modules`).fixEither use the CommonJS `require()` syntax (`const getAssignedIdentifiers = require('get-assigned-identifiers')`) or configure your project for ESM by adding `"type": "module"` to your `package.json` or saving the file as `.mjs`.
Warnings
- gotcha The `get-assigned-identifiers` package has not been updated in over six years (last release v1.2.0). While its core functionality remains sound for older JavaScript syntax and AST specifications, it may not correctly parse or handle identifiers from very recent ECMAScript features (e.g., private class fields, new `import`/`export` syntax within modules) or be fully compatible with AST structures produced by modern parsers (e.g., Babel 7+, Acorn 8+).
- gotcha The package expects an AST node from a parser like Acorn or Esprima. It does not provide its own parser. Ensure you are passing a valid AST node, typically an `Identifier` or `ObjectPattern`/`ArrayPattern` node.
Install
-
npm install get-assigned-identifiers -
yarn add get-assigned-identifiers -
pnpm add get-assigned-identifiers
Imports
- getAssignedIdentifiers
import { getAssignedIdentifiers } from 'get-assigned-identifiers'import getAssignedIdentifiers from 'get-assigned-identifiers'
- getAssignedIdentifiers
const getAssignedIdentifiers = require('get-assigned-identifiers')
Quickstart
import { parse } from 'acorn';
import getAssignedIdentifiers from 'get-assigned-identifiers';
const code = `
const { a, b: [ c,, ...x ], d = 10 } = someComplexObject();
let [ e, f ] = otherArray;
var g = 'hello';
`;
try {
const ast = parse(code, { ecmaVersion: 2020 });
// Example 1: Destructuring declaration
const node1 = ast.body[0].declarations[0].id; // { a, b: [ c,, ...x ], d = 10 }
console.log('Identifiers from destructuring:', getAssignedIdentifiers(node1));
// Expected: [{ name: 'a' }, { name: 'c' }, { name: 'x' }, { name: 'd' }]
// Example 2: Array destructuring
const node2 = ast.body[1].declarations[0].id; // [ e, f ]
console.log('Identifiers from array destructuring:', getAssignedIdentifiers(node2));
// Expected: [{ name: 'e' }, { name: 'f' }]
// Example 3: Simple variable declaration
const node3 = ast.body[2].declarations[0].id; // g
console.log('Identifiers from simple declaration:', getAssignedIdentifiers(node3));
// Expected: [{ name: 'g' }]
} catch (error) {
console.error('Error parsing code:', error.message);
}