README.md
1<h1 align="center">TypeScript Scope Manager</h1>
2
3<p align="center">
4 <img src="https://github.com/typescript-eslint/typescript-eslint/workflows/CI/badge.svg" alt="CI" />
5 <a href="https://www.npmjs.com/package/@typescript-eslint/scope-manager"><img src="https://img.shields.io/npm/v/@typescript-eslint/scope-manager.svg?style=flat-square" alt="NPM Version" /></a>
6 <a href="https://www.npmjs.com/package/@typescript-eslint/scope-manager"><img src="https://img.shields.io/npm/dm/@typescript-eslint/scope-manager.svg?style=flat-square" alt="NPM Downloads" /></a>
7</p>
8
9This is a fork of [`eslint-scope`](https://github.com/eslint/eslint-scope), enhanced to support TypeScript functionality.
10[You can view the original licence for the code here](https://github.com/eslint/eslint-scope/blob/dbddf14d5771b21b5da704213e4508c660ca1c64/LICENSE).
11
12This package is consumed automatically by [`@typescript-eslint/parser`](../parser).
13You probably don't want to use it directly.
14
15## Getting Started
16
17**[You can find our Getting Started docs here](../../docs/getting-started/linting/README.md)**
18
19## Installation
20
21```bash
22$ yarn add -D typescript @typescript-eslint/scope-manager
23$ npm i --save-dev typescript @typescript-eslint/scope-manager
24```
25
26## API
27
28### `analyze(tree, options)`
29
30Analyses a given AST and returns the resulting `ScopeManager`.
31
32```ts
33interface AnalyzeOptions {
34 /**
35 * Known visitor keys.
36 */
37 childVisitorKeys?: Record<string, string[]> | null;
38
39 /**
40 * Which ECMAScript version is considered.
41 * Defaults to `2018`.
42 */
43 ecmaVersion?: EcmaVersion;
44
45 /**
46 * Whether the whole script is executed under node.js environment.
47 * When enabled, the scope manager adds a function scope immediately following the global scope.
48 * Defaults to `false`.
49 */
50 globalReturn?: boolean;
51
52 /**
53 * Implied strict mode (if ecmaVersion >= 5).
54 * Defaults to `false`.
55 */
56 impliedStrict?: boolean;
57
58 /**
59 * The identifier that's used for JSX Element creation (after transpilation).
60 * This should not be a member expression - just the root identifier (i.e. use "React" instead of "React.createElement").
61 * Defaults to `"React"`.
62 */
63 jsxPragma?: string;
64
65 /**
66 * The identifier that's used for JSX fragment elements (after transpilation).
67 * If `null`, assumes transpilation will always use a member on `jsxFactory` (i.e. React.Fragment).
68 * This should not be a member expression - just the root identifier (i.e. use "h" instead of "h.Fragment").
69 * Defaults to `null`.
70 */
71 jsxFragmentName?: string | null;
72
73 /**
74 * The lib used by the project.
75 * This automatically defines a type variable for any types provided by the configured TS libs.
76 * For more information, see https://www.typescriptlang.org/tsconfig#lib
77 *
78 * Defaults to the lib for the provided `ecmaVersion`.
79 */
80 lib?: Lib[];
81
82 /**
83 * The source type of the script.
84 */
85 sourceType?: 'script' | 'module';
86}
87```
88
89Example usage:
90
91```ts
92import { analyze } from '@typescript-eslint/scope-manager';
93import { parse } from '@typescript-eslint/typescript-estree';
94
95const code = `const hello: string = 'world';`;
96const ast = parse(code, {
97 // note that scope-manager requires ranges on the AST
98 range: true,
99});
100const scope = analyze(ast, {
101 ecmaVersion: 2020,
102 sourceType: 'module',
103});
104```
105
106## References
107
108- https://eslint.org/docs/developer-guide/scope-manager-interface
109- https://github.com/eslint/eslint-scope
110
111## Contributing
112
113[See the contributing guide here](../../CONTRIBUTING.md)
114