/
createDefaultProgram.ts
72 lines (60 loc) · 2.09 KB
/
createDefaultProgram.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
import debug from 'debug';
import path from 'path';
import * as ts from 'typescript';
import type { ParseSettings } from '../parseSettings';
import type { ASTAndDefiniteProgram } from './shared';
import { createDefaultCompilerOptionsFromExtra } from './shared';
const log = debug('typescript-eslint:typescript-estree:createDefaultProgram');
/**
* @param parseSettings Internal settings for parsing the file
* @returns If found, returns the source file corresponding to the code and the containing program
* @deprecated
* This is a legacy option that comes with severe performance penalties.
* Please do not use it.
*/
function createDefaultProgram(
parseSettings: ParseSettings,
): ASTAndDefiniteProgram | undefined {
log(
'Getting default program for: %s',
parseSettings.filePath || 'unnamed file',
);
if (parseSettings.projects.length !== 1) {
return undefined;
}
const tsconfigPath = parseSettings.projects[0];
const commandLine = ts.getParsedCommandLineOfConfigFile(
tsconfigPath,
createDefaultCompilerOptionsFromExtra(parseSettings),
{
...ts.sys,
// TODO: file issue on TypeScript to suggest making optional?
// eslint-disable-next-line @typescript-eslint/no-empty-function
onUnRecoverableConfigFileDiagnostic: () => {},
},
);
if (!commandLine) {
return undefined;
}
const compilerHost = ts.createCompilerHost(
commandLine.options,
/* setParentNodes */ true,
);
const oldReadFile = compilerHost.readFile;
compilerHost.readFile = (fileName: string): string | undefined =>
path.normalize(fileName) === path.normalize(parseSettings.filePath)
? parseSettings.codeFullText
: oldReadFile(fileName);
const program = ts.createProgram(
[parseSettings.filePath],
{
...commandLine.options,
jsDocParsingMode: parseSettings.jsDocParsingMode,
},
compilerHost,
);
const ast = program.getSourceFile(parseSettings.filePath);
return ast && { ast, program };
}
// eslint-disable-next-line deprecation/deprecation -- will be cleaned up with the next major
export { createDefaultProgram };