Skip to content

Commit 8f3b0a8

Browse files
authored
fix(typescript-estree): improve missing project file error msg (#866)
Fixes #853
1 parent c68e033 commit 8f3b0a8

23 files changed

+190
-38
lines changed

packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,6 @@ foo(str!);
9999
`
100100
declare function a(a: string): any;
101101
declare const b: string | null;
102-
class Mx {
103-
@a(b!)
104-
private prop = 1;
105-
}
106-
`,
107-
`
108102
class Mx {
109103
@a(b!)
110104
private prop = 1;

packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,37 @@ ruleTester.run('restrict-plus-operands', rule, {
2626
`var foo = BigInt(1) + 1n`,
2727
`var foo = 1n; foo + 2n`,
2828
`
29-
function test () : number { return 2; }
29+
function test(s: string, n: number) : number { return 2; }
3030
var foo = test("5.5", 10) + 10;
31-
`,
31+
`,
3232
`
3333
var x = 5;
3434
var z = 8.2;
3535
var foo = x + z;
36-
`,
36+
`,
3737
`
3838
var w = "6.5";
3939
var y = "10";
4040
var foo = y + w;
41-
`,
41+
`,
4242
'var foo = 1 + 1;',
4343
"var foo = '1' + '1';",
4444
`
4545
var pair: { first: number, second: string } = { first: 5, second: "10" };
4646
var foo = pair.first + 10;
47-
`,
47+
`,
4848
`
4949
var pair: { first: number, second: string } = { first: 5, second: "10" };
5050
var foo = pair.first + (10 as number);
51-
`,
51+
`,
5252
`
5353
var pair: { first: number, second: string } = { first: 5, second: "10" };
5454
var foo = "5.5" + pair.second;
55-
`,
55+
`,
5656
`
5757
var pair: { first: number, second: string } = { first: 5, second: "10" };
5858
var foo = ("5.5" as string) + pair.second;
59-
`,
59+
`,
6060
`const foo = 'hello' + (someBoolean ? 'a' : 'b') + (() => someBoolean ? 'c' : 'd')() + 'e';`,
6161
`const balls = true;`,
6262
`balls === true;`,

packages/parser/README.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,30 @@ The following additional configuration options are available by specifying them
5252

5353
- 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:
5454

55-
```ts
56-
{
57-
"extends": "./tsconfig.json", // path to existing tsconfig
58-
"include": [
59-
"src/**/*.ts",
60-
"test/**/*.ts",
61-
// etc
62-
]
63-
}
64-
```
55+
```ts
56+
{
57+
// extend your base config so you don't have to redefine your compilerOptions
58+
"extends": "./tsconfig.json",
59+
"include": [
60+
"src/**/*.ts",
61+
"test/**/*.ts",
62+
"typings/**/*.ts",
63+
// etc
64+
65+
// if you have a mixed JS/TS codebase, don't forget to include your JS files
66+
"src/**/*.js"
67+
]
68+
}
69+
```
6570

6671
- **`tsconfigRootDir`** - default `undefined`. This option allows you to provide the root directory for relative tsconfig paths specified in the `project` option above.
6772

68-
- **`createDefaultProgram`** - default `false`. This 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. However, this may incur significant performance costs, so this option is primarily included for backwards-compatibility. See the **`project`** section for more information.
69-
7073
- **`extraFileExtensions`** - default `undefined`. This option allows you to provide one or more additional file extensions which should be considered in the TypeScript Program compilation. E.g. a `.vue` file
7174

7275
- **`warnOnUnsupportedTypeScriptVersion`** - default `true`. This 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
7376

77+
- **`createDefaultProgram`** - default `false`. This 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.
78+
7479
### .eslintrc.json
7580

7681
```json

packages/typescript-estree/jest.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,12 @@ module.exports = {
1010
collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}'],
1111
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
1212
coverageReporters: ['text-summary', 'lcov'],
13+
globals: {
14+
'ts-jest': {
15+
diagnostics: {
16+
// ignore the diagnostic error for the invalidFileErrors fixtures
17+
ignoreCodes: [5056],
18+
},
19+
},
20+
},
1321
};

packages/typescript-estree/src/convert.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export class Converter {
6767
*/
6868
constructor(ast: ts.SourceFile, options: ConverterOptions) {
6969
this.ast = ast;
70-
this.options = options;
70+
this.options = { ...options };
7171
}
7272

7373
getASTMaps(): ASTMaps {

packages/typescript-estree/src/parser.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from 'path';
12
import semver from 'semver';
23
import * as ts from 'typescript'; // leave this as * as ts so people using util package don't need syntheticDefaultImports
34
import { astConverter } from './ast-converter';
@@ -9,6 +10,7 @@ import { TSESTree } from './ts-estree';
910
import {
1011
calculateProjectParserOptions,
1112
createProgram,
13+
defaultCompilerOptions,
1214
} from './tsconfig-parser';
1315

1416
/**
@@ -87,9 +89,39 @@ function getASTFromProject(
8789
);
8890

8991
if (!astAndProgram && !createDefaultProgram) {
90-
throw new Error(
91-
`If "parserOptions.project" has been set for @typescript-eslint/parser, ${filePath} must be included in at least one of the projects provided.`,
92-
);
92+
// the file was either not matched within the tsconfig, or the extension wasn't expected
93+
const errorLines = [
94+
'"parserOptions.project" has been set for @typescript-eslint/parser.',
95+
`The file does not match your project config: ${filePath}.`,
96+
];
97+
let hasMatchedAnError = false;
98+
99+
const fileExtension = path.extname(filePath);
100+
if (!['.ts', '.tsx', '.js', '.jsx'].includes(fileExtension)) {
101+
const nonStandardExt = `The extension for the file (${fileExtension}) is non-standard`;
102+
if (extra.extraFileExtensions && extra.extraFileExtensions.length > 0) {
103+
if (!extra.extraFileExtensions.includes(fileExtension)) {
104+
errorLines.push(
105+
`${nonStandardExt}. It should be added to your existing "parserOptions.extraFileExtensions".`,
106+
);
107+
hasMatchedAnError = true;
108+
}
109+
} else {
110+
errorLines.push(
111+
`${nonStandardExt}. You should add "parserOptions.extraFileExtensions" to your config.`,
112+
);
113+
hasMatchedAnError = true;
114+
}
115+
}
116+
117+
if (!hasMatchedAnError) {
118+
errorLines.push(
119+
'The file must be included in at least one of the projects provided.',
120+
);
121+
hasMatchedAnError = true;
122+
}
123+
124+
throw new Error(errorLines.join('\n'));
93125
}
94126

95127
return astAndProgram;
@@ -158,6 +190,7 @@ function createNewProgram(code: string): ASTAndProgram {
158190
noResolve: true,
159191
target: ts.ScriptTarget.Latest,
160192
jsx: extra.jsx ? ts.JsxEmit.Preserve : undefined,
193+
...defaultCompilerOptions,
161194
},
162195
compilerHost,
163196
);

packages/typescript-estree/src/tsconfig-parser.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import { Extra } from './parser-options';
99
/**
1010
* Default compiler options for program generation from single root file
1111
*/
12-
const defaultCompilerOptions: ts.CompilerOptions = {
12+
export const defaultCompilerOptions: ts.CompilerOptions = {
1313
allowNonTsExtensions: true,
1414
allowJs: true,
15+
checkJs: true,
1516
};
1617

1718
/**
@@ -109,7 +110,7 @@ export function calculateProjectParserOptions(
109110
// create compiler host
110111
const watchCompilerHost = ts.createWatchCompilerHost(
111112
tsconfigPath,
112-
/*optionsToExtend*/ { allowNonTsExtensions: true } as ts.CompilerOptions,
113+
defaultCompilerOptions,
113114
ts.sys,
114115
ts.createSemanticDiagnosticsBuilderProgram,
115116
diagnosticReporter,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export var a = true;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export var a = true;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export var a = true;

0 commit comments

Comments
 (0)