-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
createParseSettings.ts
155 lines (143 loc) · 5.23 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
import debug from 'debug';
import type * as ts from 'typescript';
import { ensureAbsolutePath } from '../create-program/shared';
import type { TSESTreeOptions } from '../parser-options';
import { isSourceFile } from '../source-files';
import {
DEFAULT_TSCONFIG_CACHE_DURATION_SECONDS,
ExpiringCache,
} from './ExpiringCache';
import { getProjectConfigFiles } from './getProjectConfigFiles';
import type { MutableParseSettings } from './index';
import { inferSingleRun } from './inferSingleRun';
import { resolveProjectList } from './resolveProjectList';
import { warnAboutTSVersion } from './warnAboutTSVersion';
const log = debug(
'typescript-eslint:typescript-estree:parser:parseSettings:createParseSettings',
);
let TSCONFIG_MATCH_CACHE: ExpiringCache<string, string> | null;
export function createParseSettings(
code: string | ts.SourceFile,
options: Partial<TSESTreeOptions> = {},
): MutableParseSettings {
const codeFullText = enforceCodeString(code);
const singleRun = inferSingleRun(options);
const tsconfigRootDir =
typeof options.tsconfigRootDir === 'string'
? options.tsconfigRootDir
: process.cwd();
const parseSettings: MutableParseSettings = {
allowInvalidAST: options.allowInvalidAST === true,
code,
codeFullText,
comment: options.comment === true,
comments: [],
DEPRECATED__createDefaultProgram:
// eslint-disable-next-line deprecation/deprecation -- will be cleaned up with the next major
options.DEPRECATED__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
preserveNodeMaps: options.preserveNodeMaps !== false,
programs: Array.isArray(options.programs) ? options.programs : null,
projects: [],
range: options.range === true,
singleRun,
tokens: options.tokens === true ? [] : null,
tsconfigMatchCache: (TSCONFIG_MATCH_CACHE ??= new ExpiringCache(
singleRun
? 'Infinity'
: options.cacheLifetime?.glob ??
DEFAULT_TSCONFIG_CACHE_DURATION_SECONDS,
)),
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) {
parseSettings.projects = resolveProjectList({
cacheLifetime: options.cacheLifetime,
project: getProjectConfigFiles(parseSettings, options.project),
projectFolderIgnoreList: options.projectFolderIgnoreList,
singleRun: parseSettings.singleRun,
tsconfigRootDir: tsconfigRootDir,
});
}
warnAboutTSVersion(parseSettings);
return parseSettings;
}
export function clearTSConfigMatchCache(): void {
TSCONFIG_MATCH_CACHE?.clear();
}
/**
* 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';
}