本文へ移動

@typescript-eslint/scope-manager

npm: @typescript-eslint/scope-manager v7.13.1

TypeScript機能をサポートするように拡張されたeslint-scope のフォークです。 ✨

"スコープアナライザー" はASTをトラバースし、変数(そして私たちのケースでは型)がソースコードでどのように定義され、使用されているかのモデルを構築します。この静的分析により、はるかに重い型情報に頼ることなく、プログラム全体を通して変数を理解し、追跡することができます。

API

analyze(tree, options)

与えられたASTを分析し、結果のScopeManagerを返します。

interface AnalyzeOptions {
/**
* Known visitor keys.
*/
childVisitorKeys?: Record<string, string[]> | null;

/**
* 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;

/**
* 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.
* For more information, see https://typescript.dokyumento.jp/tsconfig#lib
*
* Defaults to ['esnext'].
*/
lib?: Lib[];

/**
* The source type of the script.
*/
sourceType?: 'script' | 'module';

/**
* Emit design-type metadata for decorated declarations in source.
* Defaults to `false`.
*/
emitDecoratorMetadata?: boolean;
}

使用例

import { analyze } from '@typescript-eslint/scope-manager';
import { parse } from '@typescript-eslint/typescript-estree';

const code = `const hello: string = 'world';`;
const ast = parse(code, {
// note that scope-manager requires ranges on the AST
range: true,
});
const scope = analyze(ast, {
sourceType: 'module',
});

参照