forked from typescript-eslint/typescript-eslint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
useProgramFromProjectService.ts
94 lines (80 loc) · 3 KB
/
useProgramFromProjectService.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
import debug from 'debug';
import { minimatch } from 'minimatch';
import path from 'path';
import { createProjectProgram } from './create-program/createProjectProgram';
import type { ProjectServiceSettings } from './create-program/createProjectService';
import type { ASTAndDefiniteProgram } from './create-program/shared';
import type { MutableParseSettings } from './parseSettings';
const log = debug(
'typescript-eslint:typescript-estree:useProgramFromProjectService',
);
export function useProgramFromProjectService(
{ allowDefaultProjectForFiles, service }: ProjectServiceSettings,
parseSettings: Readonly<MutableParseSettings>,
hasFullTypeInformation: boolean,
): ASTAndDefiniteProgram | undefined {
const filePathAbsolute = absolutify(parseSettings.filePath);
log(
'Opening project service file for: %s at absolute path %s',
parseSettings.filePath,
filePathAbsolute,
);
const opened = service.openClientFile(
filePathAbsolute,
parseSettings.codeFullText,
/* scriptKind */ undefined,
parseSettings.tsconfigRootDir,
);
log('Opened project service file: %o', opened);
if (hasFullTypeInformation) {
log(
'Project service type information enabled; checking for file path match on: %o',
allowDefaultProjectForFiles,
);
const isDefaultProjectAllowedPath = filePathMatchedBy(
parseSettings.filePath,
allowDefaultProjectForFiles,
);
log(
'Default project allowed path: %s, based on config file: %s',
isDefaultProjectAllowedPath,
opened.configFileName,
);
if (opened.configFileName) {
if (isDefaultProjectAllowedPath) {
throw new Error(
`${parseSettings.filePath} was included by allowDefaultProjectForFiles but also was found in the project service. Consider removing it from allowDefaultProjectForFiles.`,
);
}
} else if (!isDefaultProjectAllowedPath) {
throw new Error(
`${parseSettings.filePath} was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProjectForFiles.`,
);
}
}
log('Retrieving script info and then program for: %s', filePathAbsolute);
const scriptInfo = service.getScriptInfo(filePathAbsolute);
const program = service
.getDefaultProjectForFile(scriptInfo!.fileName, true)!
.getLanguageService(/*ensureSynchronized*/ true)
.getProgram();
if (!program) {
log('Could not find project service program for: %s', filePathAbsolute);
return undefined;
}
log('Found project service program for: %s', filePathAbsolute);
return createProjectProgram(parseSettings, [program]);
function absolutify(filePath: string): string {
return path.isAbsolute(filePath)
? filePath
: path.join(service.host.getCurrentDirectory(), filePath);
}
}
function filePathMatchedBy(
filePath: string,
allowDefaultProjectForFiles: string[] | undefined,
): boolean {
return !!allowDefaultProjectForFiles?.some(pattern =>
minimatch(filePath, pattern),
);
}