forked from typescript-eslint/typescript-eslint
/
createProjectProgram.ts
123 lines (109 loc) · 4.39 KB
/
createProjectProgram.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import debug from 'debug';
import path from 'path';
import * as ts from 'typescript';
import { firstDefined } from '../node-utils';
import type { Extra } from '../parser-options';
import { getProgramsForProjects } from './createWatchProgram';
import type { ASTAndProgram } from './shared';
import { getAstFromProgram } from './shared';
const log = debug('typescript-eslint:typescript-estree:createProjectProgram');
const DEFAULT_EXTRA_FILE_EXTENSIONS = [
ts.Extension.Ts,
ts.Extension.Tsx,
ts.Extension.Js,
ts.Extension.Jsx,
ts.Extension.Mjs,
ts.Extension.Mts,
ts.Extension.Cjs,
ts.Extension.Cts,
] as readonly string[];
/**
* @param code The code of the file being linted
* @param createDefaultProgram True if the default program should be created
* @param extra The config object
* @returns If found, returns the source file corresponding to the code and the containing program
*/
function createProjectProgram(
code: string,
createDefaultProgram: boolean,
extra: Extra,
): ASTAndProgram | undefined {
log('Creating project program for: %s', extra.filePath);
const programsForProjects = getProgramsForProjects(
code,
extra.filePath,
extra,
);
const astAndProgram = firstDefined(programsForProjects, currentProgram =>
getAstFromProgram(currentProgram, extra),
);
// The file was either matched within the tsconfig, or we allow creating a default program
if (astAndProgram || createDefaultProgram) {
return astAndProgram;
}
const describeFilePath = (filePath: string): string => {
const relative = path.relative(
extra.tsconfigRootDir || process.cwd(),
filePath,
);
if (extra.tsconfigRootDir) {
return `<tsconfigRootDir>/${relative}`;
}
return `<cwd>/${relative}`;
};
const describedFilePath = describeFilePath(extra.filePath);
const relativeProjects = extra.projects.map(describeFilePath);
const describedPrograms =
relativeProjects.length === 1
? relativeProjects[0]
: `\n${relativeProjects.map(project => `- ${project}`).join('\n')}`;
const errorLines = [
`ESLint was configured to run on \`${describedFilePath}\` using \`parserOptions.project\`: ${describedPrograms}`,
];
let hasMatchedAnError = false;
const extraFileExtensions = extra.extraFileExtensions || [];
extraFileExtensions.forEach(extraExtension => {
if (!extraExtension.startsWith('.')) {
errorLines.push(
`Found unexpected extension \`${extraExtension}\` specified with the \`parserOptions.extraFileExtensions\` option. Did you mean \`.${extraExtension}\`?`,
);
}
if (DEFAULT_EXTRA_FILE_EXTENSIONS.includes(extraExtension)) {
errorLines.push(
`You unnecessarily included the extension \`${extraExtension}\` with the \`parserOptions.extraFileExtensions\` option. This extension is already handled by the parser by default.`,
);
}
});
const fileExtension = path.extname(extra.filePath);
if (!DEFAULT_EXTRA_FILE_EXTENSIONS.includes(fileExtension)) {
const nonStandardExt = `The extension for the file (\`${fileExtension}\`) is non-standard`;
if (extraFileExtensions.length > 0) {
if (!extraFileExtensions.includes(fileExtension)) {
errorLines.push(
`${nonStandardExt}. It should be added to your existing \`parserOptions.extraFileExtensions\`.`,
);
hasMatchedAnError = true;
}
} else {
errorLines.push(
`${nonStandardExt}. You should add \`parserOptions.extraFileExtensions\` to your config.`,
);
hasMatchedAnError = true;
}
}
if (!hasMatchedAnError) {
const [describedInclusions, describedSpecifiers] =
extra.projects.length === 1
? ['that TSConfig does not', 'that TSConfig']
: ['none of those TSConfigs', 'one of those TSConfigs'];
errorLines.push(
`However, ${describedInclusions} include this file. Either:`,
`- Change ESLint's list of included files to not include this file`,
`- Change ${describedSpecifiers} to include this file`,
`- Create a new TSConfig that includes this file and include it in your parserOptions.project`,
`See the TypeScript ESLint docs for more info: https://typescript-eslint.io/docs/linting/troubleshooting##i-get-errors-telling-me-eslint-was-configured-to-run--however-that-tsconfig-does-not--none-of-those-tsconfigs-include-this-file`,
);
}
throw new Error(errorLines.join('\n'));
}
export { createProjectProgram };