forked from typescript-eslint/typescript-eslint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
createParseSettings.ts
205 lines (187 loc) · 6.33 KB
/
createParseSettings.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import debug from 'debug';
import { sync as globSync } from 'globby';
import isGlob from 'is-glob';
import type * as ts from 'typescript';
import type { CanonicalPath } from '../create-program/shared';
import {
ensureAbsolutePath,
getCanonicalFileName,
} from '../create-program/shared';
import type { TSESTreeOptions } from '../parser-options';
import { isSourceFile } from '../source-files';
import type { MutableParseSettings } from './index';
import { inferSingleRun } from './inferSingleRun';
import { warnAboutTSVersion } from './warnAboutTSVersion';
const log = debug(
'typescript-eslint:typescript-estree:parser:parseSettings:createParseSettings',
);
export function createParseSettings(
code: string | ts.SourceFile,
options: Partial<TSESTreeOptions> = {},
): MutableParseSettings {
const codeFullText = enforceCodeString(code);
const tsconfigRootDir =
typeof options.tsconfigRootDir === 'string'
? options.tsconfigRootDir
: process.cwd();
const parseSettings: MutableParseSettings = {
code,
codeFullText,
comment: options.comment === true,
comments: [],
createDefaultProgram: options.createDefaultProgram === true,
debugLevel:
options.debugLevel === true
? new Set(['typescript-eslint'])
: Array.isArray(options.debugLevel)
? new Set(options.debugLevel)
: new Set(),
errorOnTypeScriptSyntacticAndSemanticIssues: false,
errorOnUnknownASTType: options.errorOnUnknownASTType === true,
EXPERIMENTAL_useSourceOfProjectReferenceRedirect:
options.EXPERIMENTAL_useSourceOfProjectReferenceRedirect === true,
extraFileExtensions:
Array.isArray(options.extraFileExtensions) &&
options.extraFileExtensions.every(ext => typeof ext === 'string')
? options.extraFileExtensions
: [],
filePath: ensureAbsolutePath(
typeof options.filePath === 'string' && options.filePath !== '<input>'
? options.filePath
: getFileName(options.jsx),
tsconfigRootDir,
),
jsx: options.jsx === true,
loc: options.loc === true,
log:
typeof options.loggerFn === 'function'
? options.loggerFn
: options.loggerFn === false
? (): void => {}
: console.log, // eslint-disable-line no-console
moduleResolver: options.moduleResolver ?? '',
preserveNodeMaps: options.preserveNodeMaps !== false,
programs: Array.isArray(options.programs) ? options.programs : null,
projects: [],
range: options.range === true,
singleRun: inferSingleRun(options),
tokens: options.tokens === true ? [] : null,
tsconfigRootDir,
};
// debug doesn't support multiple `enable` calls, so have to do it all at once
if (parseSettings.debugLevel.size > 0) {
const namespaces = [];
if (parseSettings.debugLevel.has('typescript-eslint')) {
namespaces.push('typescript-eslint:*');
}
if (
parseSettings.debugLevel.has('eslint') ||
// make sure we don't turn off the eslint debug if it was enabled via --debug
debug.enabled('eslint:*,-eslint:code-path')
) {
// https://github.com/eslint/eslint/blob/9dfc8501fb1956c90dc11e6377b4cb38a6bea65d/bin/eslint.js#L25
namespaces.push('eslint:*,-eslint:code-path');
}
debug.enable(namespaces.join(','));
}
if (Array.isArray(options.programs)) {
if (!options.programs.length) {
throw new Error(
`You have set parserOptions.programs to an empty array. This will cause all files to not be found in existing programs. Either provide one or more existing TypeScript Program instances in the array, or remove the parserOptions.programs setting.`,
);
}
log(
'parserOptions.programs was provided, so parserOptions.project will be ignored.',
);
}
// Providing a program overrides project resolution
if (!parseSettings.programs) {
const projectFolderIgnoreList = (
options.projectFolderIgnoreList ?? ['**/node_modules/**']
)
.reduce<string[]>((acc, folder) => {
if (typeof folder === 'string') {
acc.push(folder);
}
return acc;
}, [])
// prefix with a ! for not match glob
.map(folder => (folder.startsWith('!') ? folder : `!${folder}`));
parseSettings.projects = prepareAndTransformProjects(
tsconfigRootDir,
options.project,
projectFolderIgnoreList,
);
}
warnAboutTSVersion(parseSettings);
return parseSettings;
}
/**
* Ensures source code is a string.
*/
function enforceCodeString(code: unknown): string {
return isSourceFile(code)
? code.getFullText(code)
: typeof code === 'string'
? code
: String(code);
}
/**
* Compute the filename based on the parser options.
*
* Even if jsx option is set in typescript compiler, filename still has to
* contain .tsx file extension.
*
* @param options Parser options
*/
function getFileName(jsx?: boolean): string {
return jsx ? 'estree.tsx' : 'estree.ts';
}
function getTsconfigPath(
tsconfigPath: string,
tsconfigRootDir: string,
): CanonicalPath {
return getCanonicalFileName(
ensureAbsolutePath(tsconfigPath, tsconfigRootDir),
);
}
/**
* Normalizes, sanitizes, resolves and filters the provided project paths
*/
function prepareAndTransformProjects(
tsconfigRootDir: string,
projectsInput: string | string[] | undefined,
ignoreListInput: string[],
): CanonicalPath[] {
const sanitizedProjects: string[] = [];
// Normalize and sanitize the project paths
if (typeof projectsInput === 'string') {
sanitizedProjects.push(projectsInput);
} else if (Array.isArray(projectsInput)) {
for (const project of projectsInput) {
if (typeof project === 'string') {
sanitizedProjects.push(project);
}
}
}
if (sanitizedProjects.length === 0) {
return [];
}
// Transform glob patterns into paths
const nonGlobProjects = sanitizedProjects.filter(project => !isGlob(project));
const globProjects = sanitizedProjects.filter(project => isGlob(project));
const uniqueCanonicalProjectPaths = new Set(
nonGlobProjects
.concat(
globSync([...globProjects, ...ignoreListInput], {
cwd: tsconfigRootDir,
}),
)
.map(project => getTsconfigPath(project, tsconfigRootDir)),
);
log(
'parserOptions.project (excluding ignored) matched projects: %s',
uniqueCanonicalProjectPaths,
);
return Array.from(uniqueCanonicalProjectPaths);
}