forked from typescript-eslint/typescript-eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.ts
180 lines (163 loc) · 5.6 KB
/
parser.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
import type {
AnalyzeOptions,
ScopeManager,
} from '@typescript-eslint/scope-manager';
import { analyze } from '@typescript-eslint/scope-manager';
import type { Lib, TSESTree } from '@typescript-eslint/types';
import { ParserOptions } from '@typescript-eslint/types';
import type {
ParserServices,
TSESTreeOptions,
} from '@typescript-eslint/typescript-estree';
import { parseAndGenerateServices } from '@typescript-eslint/typescript-estree';
import type { VisitorKeys } from '@typescript-eslint/visitor-keys';
import { visitorKeys } from '@typescript-eslint/visitor-keys';
import debug from 'debug';
import type * as ts from 'typescript';
import { ScriptTarget } from 'typescript';
const log = debug('typescript-eslint:parser:parser');
interface ParseForESLintResult {
ast: TSESTree.Program & {
range?: [number, number];
tokens?: TSESTree.Token[];
comments?: TSESTree.Comment[];
};
services: ParserServices;
visitorKeys: VisitorKeys;
scopeManager: ScopeManager;
}
function validateBoolean(
value: boolean | undefined,
fallback = false,
): boolean {
if (typeof value !== 'boolean') {
return fallback;
}
return value;
}
const LIB_FILENAME_REGEX = /lib\.(.+)\.d\.[cm]?ts$/;
function getLib(compilerOptions: ts.CompilerOptions): Lib[] {
if (compilerOptions.lib) {
return compilerOptions.lib.reduce<Lib[]>((acc, lib) => {
const match = LIB_FILENAME_REGEX.exec(lib.toLowerCase());
if (match) {
acc.push(match[1] as Lib);
}
return acc;
}, []);
}
const target = compilerOptions.target ?? ScriptTarget.ES5;
// https://github.com/microsoft/TypeScript/blob/ae582a22ee1bb052e19b7c1bc4cac60509b574e0/src/compiler/utilitiesPublic.ts#L13-L36
switch (target) {
case ScriptTarget.ESNext:
return ['esnext.full'];
case ScriptTarget.ES2022:
return ['es2022.full'];
case ScriptTarget.ES2021:
return ['es2021.full'];
case ScriptTarget.ES2020:
return ['es2020.full'];
case ScriptTarget.ES2019:
return ['es2019.full'];
case ScriptTarget.ES2018:
return ['es2018.full'];
case ScriptTarget.ES2017:
return ['es2017.full'];
case ScriptTarget.ES2016:
return ['es2016.full'];
case ScriptTarget.ES2015:
return ['es6'];
default:
return ['lib'];
}
}
function parse(
code: ts.SourceFile | string,
options?: ParserOptions,
): ParseForESLintResult['ast'] {
return parseForESLint(code, options).ast;
}
function parseForESLint(
code: ts.SourceFile | string,
options?: ParserOptions | null,
): ParseForESLintResult {
if (!options || typeof options !== 'object') {
options = {};
} else {
options = { ...options };
}
// https://eslint.org/docs/user-guide/configuring#specifying-parser-options
// if sourceType is not provided by default eslint expect that it will be set to "script"
if (options.sourceType !== 'module' && options.sourceType !== 'script') {
options.sourceType = 'script';
}
if (typeof options.ecmaFeatures !== 'object') {
options.ecmaFeatures = {};
}
const parserOptions: TSESTreeOptions = {};
Object.assign(parserOptions, options, {
jsx: validateBoolean(options.ecmaFeatures.jsx),
/**
* Override errorOnTypeScriptSyntacticAndSemanticIssues and set it to false to prevent use from user config
* https://github.com/typescript-eslint/typescript-eslint/issues/8681#issuecomment-2000411834
*/
errorOnTypeScriptSyntacticAndSemanticIssues: false,
});
const analyzeOptions: AnalyzeOptions = {
globalReturn: options.ecmaFeatures.globalReturn,
jsxPragma: options.jsxPragma,
jsxFragmentName: options.jsxFragmentName,
lib: options.lib,
sourceType: options.sourceType,
};
/**
* Allow the user to suppress the warning from typescript-estree if they are using an unsupported
* version of TypeScript
*/
const warnOnUnsupportedTypeScriptVersion = validateBoolean(
options.warnOnUnsupportedTypeScriptVersion,
true,
);
if (!warnOnUnsupportedTypeScriptVersion) {
parserOptions.loggerFn = false;
}
const { ast, services } = parseAndGenerateServices(code, parserOptions);
ast.sourceType = options.sourceType;
if (services.program) {
// automatically apply the options configured for the program
const compilerOptions = services.program.getCompilerOptions();
if (analyzeOptions.lib == null) {
analyzeOptions.lib = getLib(compilerOptions);
log('Resolved libs from program: %o', analyzeOptions.lib);
}
if (
analyzeOptions.jsxPragma === undefined &&
compilerOptions.jsxFactory != null
) {
// in case the user has specified something like "preact.h"
const factory = compilerOptions.jsxFactory.split('.')[0].trim();
analyzeOptions.jsxPragma = factory;
log('Resolved jsxPragma from program: %s', analyzeOptions.jsxPragma);
}
if (
analyzeOptions.jsxFragmentName === undefined &&
compilerOptions.jsxFragmentFactory != null
) {
// in case the user has specified something like "preact.Fragment"
const fragFactory = compilerOptions.jsxFragmentFactory
.split('.')[0]
.trim();
analyzeOptions.jsxFragmentName = fragFactory;
log(
'Resolved jsxFragmentName from program: %s',
analyzeOptions.jsxFragmentName,
);
}
}
// if not defined - override from the parserOptions
services.emitDecoratorMetadata ??= options.emitDecoratorMetadata === true;
services.experimentalDecorators ??= options.experimentalDecorators === true;
const scopeManager = analyze(ast, analyzeOptions);
return { ast, services, scopeManager, visitorKeys };
}
export { parse, parseForESLint, ParserOptions };