Undeclared Identifiers Finder
This package, `undeclared-identifiers`, provides a utility to identify undeclared identifiers and property accesses within JavaScript source code or an existing Abstract Syntax Tree (AST). It primarily targets older JavaScript environments, as indicated by its last significant update (v1.1.3 in 2018) and its internal reliance on `acorn-node` for parsing. It returns an object containing arrays of identified undeclared variable names and their associated property accesses. It does not perform full scope resolution, but rather flags any identifier not explicitly declared within its immediate context, making it suitable for basic linting, dependency analysis, or static code checks in environments where modern tooling might be overkill or incompatible. The package has seen infrequent updates since its initial releases, suggesting it is in a maintenance mode, with no new feature development expected.
Common errors
-
TypeError: undeclaredIdentifiers is not a function
cause Attempting to use `undeclaredIdentifiers` in an ESM module with a named import, e.g., `import { undeclaredIdentifiers } from 'undeclared-identifiers'`, when the package only provides a default (CommonJS) export.fixUse a default import instead: `import undeclaredIdentifiers from 'undeclared-identifiers'`. If using `require`, ensure it's assigned to a variable directly: `const undeclaredIdentifiers = require('undeclared-identifiers')`. -
SyntaxError: The keyword 'await' is reserved (and similar errors for modern JS features)
cause The package uses `acorn-node` for parsing, which may be an older version and not support recent ECMAScript syntax features (e.g., top-level await, private class fields, new module syntax).fixIf analyzing modern JavaScript, you might need to pre-process your code to an older standard (e.g., using Babel) or consider a different code analysis tool that uses an up-to-date parser. This package is best suited for older JS codebases or environments.
Warnings
- gotcha The package was last updated in 2018 (v1.1.3) and relies on an older version of `acorn-node` for parsing. It may not correctly parse or fully support modern JavaScript syntax (e.g., ES2019+, private class fields, top-level await, import assertions/attributes).
- gotcha This utility identifies references to undeclared identifiers but does not perform full semantic scope analysis. It will not detect all cases where an identifier is declared in an outer scope but not the immediate one, or issues related to shadowing or global variable pollution beyond a simple check for 'is it declared in this AST context?'.
- breaking Prior to v1.1.3, class names and method names were incorrectly flagged as undeclared identifiers, leading to false positives when analyzing JavaScript classes.
- gotcha The `opts.wildcard` option, introduced in v1.1.0, had incorrect behavior in versions v1.1.0 and v1.1.1. Specifically, wildcard uses were not detected after property use, and standard property accesses were sometimes incorrectly flagged as wildcards.
Install
-
npm install undeclared-identifiers -
yarn add undeclared-identifiers -
pnpm add undeclared-identifiers
Imports
- undeclaredIdentifiers (CommonJS)
const undeclaredIdentifiers = require('undeclared-identifiers') - undeclaredIdentifiers (ESM Default Import)
import { undeclaredIdentifiers } from 'undeclared-identifiers'import undeclaredIdentifiers from 'undeclared-identifiers'
- Dynamic Import
const undeclaredIdentifiers = await import('undeclared-identifiers')const { default: undeclaredIdentifiers } = await import('undeclared-identifiers')
Quickstart
const undeclaredIdentifiers = require('undeclared-identifiers');
const sourceCode = `
function greet(name) {
console.log('Hello, ' + name + '!');
console.warn('Consider using a logger.');
anotherGlobalVar = 'test';
Buffer.from('hi');
MyClass.staticMethod();
}
greet('World');
`;
const result = undeclaredIdentifiers(sourceCode, { wildcard: true });
console.log('Undeclared identifiers:', result.identifiers);
console.log('Undeclared properties:', result.properties);
// Expected output (approximately):
// Undeclared identifiers: [ 'console', 'anotherGlobalVar', 'Buffer', 'MyClass' ]
// Undeclared properties: [ 'console.log', 'console.warn', 'Buffer.from', 'MyClass.staticMethod' ]