TypeScript scope analyser for ESLint that provides comprehensive scope analysis capabilities for JavaScript and TypeScript code
—
The core analysis functionality provides the main entry point for analyzing AST nodes and generating comprehensive scope information with TypeScript awareness.
The primary function that takes a TypeScript ESTree AST node and optional configuration options, then returns a ScopeManager instance containing complete scope analysis.
/**
* Takes an AST and returns the analyzed scopes.
* @param tree - The TypeScript ESTree AST node to analyze
* @param providedOptions - Optional configuration for the analysis
* @returns ScopeManager instance with complete scope information
*/
function analyze(
tree: TSESTree.Node,
providedOptions?: AnalyzeOptions
): ScopeManager;Configuration interface for customizing the scope analysis behavior.
interface AnalyzeOptions {
/**
* Known visitor keys for AST traversal.
*/
childVisitorKeys?: Record<string, string[]>;
/**
* Whether the whole script is executed under node.js environment.
* When enabled, the scope manager adds a function scope immediately following the global scope.
* Defaults to false.
*/
globalReturn?: boolean;
/**
* Implied strict mode.
* Defaults to false.
*/
impliedStrict?: boolean;
/**
* The identifier that's used for JSX Element creation (after transpilation).
* This should not be a member expression - just the root identifier (i.e. use "React" instead of "React.createElement").
* Defaults to "React".
*/
jsxPragma?: string | null;
/**
* The identifier that's used for JSX fragment elements (after transpilation).
* If null, assumes transpilation will always use a member on jsxFactory (i.e. React.Fragment).
* This should not be a member expression - just the root identifier (i.e. use "h" instead of "h.Fragment").
* Defaults to null.
*/
jsxFragmentName?: string | null;
/**
* The lib used by the project.
* This automatically defines a type variable for any types provided by the configured TS libs.
* Defaults to ['esnext'] when options are provided, ['es2018'] when no options are provided.
*
* @see https://www.typescriptlang.org/tsconfig#lib
*/
lib?: Lib[];
/**
* The source type of the script.
* Can be 'script' or 'module'.
*/
sourceType?: SourceType;
// TODO - remove this in v10
/**
* @deprecated This option never did what it was intended for and will be removed in a future major release.
*/
emitDecoratorMetadata?: boolean;
}Usage Examples:
import { analyze } from "@typescript-eslint/scope-manager";
import type { TSESTree } from "@typescript-eslint/types";
// Basic analysis with default options
const scopeManager = analyze(astNode);
// Analysis with custom options
const scopeManager = analyze(astNode, {
globalReturn: true,
impliedStrict: true,
jsxPragma: 'h',
jsxFragmentName: 'Fragment',
lib: ['es2020', 'dom'],
sourceType: 'module'
});
// Node.js environment analysis
const scopeManager = analyze(astNode, {
globalReturn: true,
sourceType: 'script',
lib: ['es2020', 'node']
});
// React JSX analysis
const scopeManager = analyze(astNode, {
jsxPragma: 'React',
jsxFragmentName: null, // Uses React.Fragment
lib: ['es2020', 'dom'],
sourceType: 'module'
});The analyze function uses these default values when options are not specified:
const DEFAULT_OPTIONS: Required<AnalyzeOptions> = {
childVisitorKeys: visitorKeys, // from @typescript-eslint/visitor-keys
emitDecoratorMetadata: false, // deprecated
globalReturn: false,
impliedStrict: false,
jsxFragmentName: null,
jsxPragma: 'React',
lib: ['es2018'],
sourceType: 'script'
};The analyze function works with TypeScript ESTree AST nodes from @typescript-eslint/types:
import { parse } from "@typescript-eslint/parser";
import { analyze } from "@typescript-eslint/scope-manager";
// Parse TypeScript code
const code = `
interface User {
name: string;
age: number;
}
function createUser(name: string, age: number): User {
return { name, age };
}
`;
const ast = parse(code, {
loc: true,
range: true,
});
// Analyze the parsed AST
const scopeManager = analyze(ast, {
sourceType: 'module',
lib: ['es2020']
});
// Access analysis results
console.log(`Found ${scopeManager.scopes.length} scopes`);
console.log(`Global variables: ${scopeManager.globalScope?.variables.length}`);The analyze function may throw errors for invalid AST nodes or configuration:
Always ensure your AST is properly formed using a compatible parser like @typescript-eslint/parser.
Install with Tessl CLI
npx tessl i tessl/npm-typescript-eslint--scope-manager