-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
createProjectProgram.ts
96 lines (83 loc) · 3.3 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
import debug from 'debug';
import path from 'path';
import * as ts from 'typescript';
import { firstDefined } from '../node-utils';
import type { ParseSettings } from '../parseSettings';
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 parseSettings.code Code of the file being parsed
* @param parseSettings.createDefaultProgram Whether the default program should be created
* @param parseSettings Internal settings for parsing the file
* @returns If found, the source file corresponding to the code and the containing program
*/
function createProjectProgram(
parseSettings: ParseSettings,
): ASTAndProgram | undefined {
log('Creating project program for: %s', parseSettings.filePath);
const astAndProgram = firstDefined(
getProgramsForProjects(parseSettings),
currentProgram => getAstFromProgram(currentProgram, parseSettings),
);
if (!astAndProgram && !parseSettings.createDefaultProgram) {
// the file was either not matched within the tsconfig, or the extension wasn't expected
const errorLines = [
'"parserOptions.project" has been set for @typescript-eslint/parser.',
`The file does not match your project config: ${path.relative(
parseSettings.tsconfigRootDir || process.cwd(),
parseSettings.filePath,
)}.`,
];
let hasMatchedAnError = false;
const extraFileExtensions = parseSettings.extraFileExtensions || [];
extraFileExtensions.forEach(extraExtension => {
if (!extraExtension.startsWith('.')) {
errorLines.push(
`Found unexpected extension "${extraExtension}" specified with the "extraFileExtensions" option. Did you mean ".${extraExtension}"?`,
);
}
if (DEFAULT_EXTRA_FILE_EXTENSIONS.includes(extraExtension)) {
errorLines.push(
`You unnecessarily included the extension "${extraExtension}" with the "extraFileExtensions" option. This extension is already handled by the parser by default.`,
);
}
});
const fileExtension = path.extname(parseSettings.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) {
errorLines.push(
'The file must be included in at least one of the projects provided.',
);
}
throw new Error(errorLines.join('\n'));
}
return astAndProgram;
}
export { createProjectProgram };