README.md
1<h1 align="center">TypeScript ESLint Parser</h1>
2
3<p align="center">An ESLint parser which leverages <a href="https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/typescript-estree">TypeScript ESTree</a> to allow for ESLint to lint TypeScript source code.</p>
4
5<p align="center">
6 <img src="https://github.com/typescript-eslint/typescript-eslint/workflows/CI/badge.svg" alt="CI" />
7 <a href="https://www.npmjs.com/package/@typescript-eslint/parser"><img src="https://img.shields.io/npm/v/@typescript-eslint/parser.svg?style=flat-square" alt="NPM Version" /></a>
8 <a href="https://www.npmjs.com/package/@typescript-eslint/parser"><img src="https://img.shields.io/npm/dm/@typescript-eslint/parser.svg?style=flat-square" alt="NPM Downloads" /></a>
9</p>
10
11## Getting Started
12
13**[You can find our Getting Started docs here](../../docs/getting-started/linting/README.md)**
14
15These docs walk you through setting up ESLint, this parser, and our plugin. If you know what you're doing and just want to quick start, read on...
16
17## Quick-start
18
19### Installation
20
21```bash
22$ yarn add -D typescript @typescript-eslint/parser
23$ npm i --save-dev typescript @typescript-eslint/parser
24```
25
26### Usage
27
28In your ESLint configuration file, set the `parser` property:
29
30```json
31{
32 "parser": "@typescript-eslint/parser"
33}
34```
35
36There is sometimes an incorrect assumption that the parser itself is what does everything necessary to facilitate the use of ESLint with TypeScript. In actuality, it is the combination of the parser _and_ one or more plugins which allow you to maximize your usage of ESLint with TypeScript.
37
38For example, once this parser successfully produces an AST for the TypeScript source code, it might well contain some information which simply does not exist in a standard JavaScript context, such as the data for a TypeScript-specific construct, like an `interface`.
39
40The core rules built into ESLint, such as `indent` have no knowledge of such constructs, so it is impossible to expect them to work out of the box with them.
41
42Instead, you also need to make use of one more plugins which will add or extend rules with TypeScript-specific features.
43
44By far the most common case will be installing the [`@typescript-eslint/eslint-plugin`](https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin) plugin, but there are also other relevant options available such a [`@typescript-eslint/eslint-plugin-tslint`](https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin-tslint).
45
46## Configuration
47
48The following additional configuration options are available by specifying them in [`parserOptions`](https://eslint.org/docs/user-guide/configuring#specifying-parser-options) in your ESLint configuration file.
49
50```ts
51interface ParserOptions {
52 ecmaFeatures?: {
53 jsx?: boolean;
54 globalReturn?: boolean;
55 };
56 ecmaVersion?: number;
57
58 jsxPragma?: string;
59 jsxFragmentName?: string | null;
60 lib?: string[];
61
62 project?: string | string[];
63 projectFolderIgnoreList?: string[];
64 tsconfigRootDir?: string;
65 extraFileExtensions?: string[];
66 warnOnUnsupportedTypeScriptVersion?: boolean;
67}
68```
69
70### `parserOptions.ecmaFeatures.jsx`
71
72Default `false`.
73
74Enable parsing JSX when `true`. More details can be found [here](https://www.typescriptlang.org/docs/handbook/jsx.html).
75
76**NOTE:** this setting does not affect known file types (`.js`, `.jsx`, `.ts`, `.tsx`, `.json`) because the TypeScript compiler has its own internal handling for known file extensions. The exact behavior is as follows:
77
78- if `parserOptions.project` is _not_ provided:
79 - `.js`, `.jsx`, `.tsx` files are parsed as if this is true.
80 - `.ts` files are parsed as if this is false.
81 - unknown extensions (`.md`, `.vue`) will respect this setting.
82- if `parserOptions.project` is provided (i.e. you are using rules with type information):
83 - `.js`, `.jsx`, `.tsx` files are parsed as if this is true.
84 - `.ts` files are parsed as if this is false.
85 - "unknown" extensions (`.md`, `.vue`) **are parsed as if this is false**.
86
87### `parserOptions.ecmaFeatures.globalReturn`
88
89Default `false`.
90
91This options allows you to tell the parser if you want to allow global `return` statements in your codebase.
92
93### `parserOptions.ecmaVersion`
94
95Default `2018`.
96
97Accepts any valid ECMAScript version number:
98
99- A version: es3, es5, es6, es7, es8, es9, es10, es11, ..., or
100- A year: es2015, es2016, es2017, es2018, es2019, es2020, ...
101
102Specifies the version of ECMAScript syntax you want to use. This is used by the parser to determine how to perform scope analysis, and it affects the default
103
104### `parserOptions.jsxPragma`
105
106Default `'React'`
107
108The identifier that's used for JSX Elements creation (after transpilation).
109If you're using a library other than React (like `preact`), then you should change this value.
110
111This should not be a member expression - just the root identifier (i.e. use `"React"` instead of `"React.createElement"`).
112
113If you provide `parserOptions.project`, you do not need to set this, as it will automatically detected from the compiler.
114
115### `parserOptions.jsxFragmentName`
116
117Default `null`
118
119The identifier that's used for JSX fragment elements (after transpilation).
120If `null`, assumes transpilation will always use a member of the configured `jsxPragma`.
121This should not be a member expression - just the root identifier (i.e. use `"h"` instead of `"h.Fragment"`).
122
123If you provide `parserOptions.project`, you do not need to set this, as it will automatically detected from the compiler.
124
125### `parserOptions.lib`
126
127Default `['es2018']`
128
129For valid options, see the [TypeScript compiler options](https://www.typescriptlang.org/tsconfig#lib).
130
131Specifies the TypeScript `lib`s that are available. This is used by the scope analyser to ensure there are global variables declared for the types exposed by TypeScript.
132
133If you provide `parserOptions.project`, you do not need to set this, as it will automatically detected from the compiler.
134
135### `parserOptions.project`
136
137Default `undefined`.
138
139This option allows you to provide a path to your project's `tsconfig.json`. **This setting is required if you want to use rules which require type information**. Relative paths are interpreted relative to the current working directory if `tsconfigRootDir` is not set. If you intend on running ESLint from directories other than the project root, you should consider using `tsconfigRootDir`.
140
141- Accepted values:
142
143 ```js
144 // path
145 project: './tsconfig.json';
146
147 // glob pattern
148 project: './packages/**/tsconfig.json';
149
150 // array of paths and/or glob patterns
151 project: ['./packages/**/tsconfig.json', './separate-package/tsconfig.json'];
152 ```
153
154- If you use project references, TypeScript will not automatically use project references to resolve files. This means that you will have to add each referenced tsconfig to the `project` field either separately, or via a glob.
155
156- TypeScript will ignore files with duplicate filenames in the same folder (for example, `src/file.ts` and `src/file.js`). TypeScript purposely ignore all but one of the files, only keeping the one file with the highest priority extension (the extension priority order (from highest to lowest) is `.ts`, `.tsx`, `.d.ts`, `.js`, `.jsx`). For more info see #955.
157
158- Note that if this setting is specified and `createDefaultProgram` is not, you must only lint files that are included in the projects as defined by the provided `tsconfig.json` files. If your existing configuration does not include all of the files you would like to lint, you can create a separate `tsconfig.eslint.json` as follows:
159
160 ```jsonc
161 {
162 // extend your base config so you don't have to redefine your compilerOptions
163 "extends": "./tsconfig.json",
164 "include": [
165 "src/**/*.ts",
166 "test/**/*.ts",
167 "typings/**/*.ts",
168 // etc
169
170 // if you have a mixed JS/TS codebase, don't forget to include your JS files
171 "src/**/*.js"
172 ]
173 }
174 ```
175
176### `parserOptions.tsconfigRootDir`
177
178Default `undefined`.
179
180This option allows you to provide the root directory for relative tsconfig paths specified in the `project` option above.
181
182### `parserOptions.projectFolderIgnoreList`
183
184Default `["**/node_modules/**"]`.
185
186This option allows you to ignore folders from being included in your provided list of `project`s.
187This is useful if you have configured glob patterns, but want to make sure you ignore certain folders.
188
189It accepts an array of globs to exclude from the `project` globs.
190
191For example, by default it will ensure that a glob like `./**/tsconfig.json` will not match any `tsconfig`s within your `node_modules` folder (some npm packages do not exclude their source files from their published packages).
192
193### `parserOptions.extraFileExtensions`
194
195Default `undefined`.
196
197This option allows you to provide one or more additional file extensions which should be considered in the TypeScript Program compilation.
198The default extensions are `.ts`, `.tsx`, `.js`, and `.jsx`. Add extensions starting with `.`, followed by the file extension. E.g. for a `.vue` file use `"extraFileExtensions: [".vue"]`.
199
200### `parserOptions.warnOnUnsupportedTypeScriptVersion`
201
202Default `true`.
203
204This option allows you to toggle the warning that the parser will give you if you use a version of TypeScript which is not explicitly supported
205
206### `parserOptions.createDefaultProgram`
207
208Default `false`.
209
210This option allows you to request that when the `project` setting is specified, files will be allowed when not included in the projects defined by the provided `tsconfig.json` files. **Using this option will incur significant performance costs. This option is primarily included for backwards-compatibility.** See the **`project`** section above for more information.
211
212## Supported TypeScript Version
213
214Please see [`typescript-eslint`](https://github.com/typescript-eslint/typescript-eslint) for the supported TypeScript version.
215
216**Please ensure that you are using a supported version before submitting any issues/bug reports.**
217
218## Reporting Issues
219
220Please use the `@typescript-eslint/parser` issue template when creating your issue and fill out the information requested as best you can. This will really help us when looking into your issue.
221
222## License
223
224TypeScript ESLint Parser is licensed under a permissive BSD 2-clause license.
225
226## Contributing
227
228[See the contributing guide here](../../CONTRIBUTING.md)
229