forked from eslint/typescript-eslint-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.js
246 lines (204 loc) · 7.77 KB
/
parser.js
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**
* @fileoverview Parser that converts TypeScript into ESTree format.
* @author Nicholas C. Zakas
* @author James Henry <https://github.com/JamesHenry>
* @copyright jQuery Foundation and other contributors, https://jquery.org/
* MIT License
*/
"use strict";
const astNodeTypes = require("./lib/ast-node-types"),
ts = require("typescript"),
convert = require("./lib/ast-converter"),
semver = require("semver");
const SUPPORTED_TYPESCRIPT_VERSIONS = require("./package.json").devDependencies.typescript;
const ACTIVE_TYPESCRIPT_VERSION = ts.version;
const isRunningSupportedTypeScriptVersion = semver.satisfies(ACTIVE_TYPESCRIPT_VERSION, SUPPORTED_TYPESCRIPT_VERSIONS);
const WARNING_BORDER = "=============";
const DEFAULT_ESLINT_FILEPATH = "<text>";
let extra;
let warnedAboutTSVersion = false;
let warnedAboutJSXOverride = false;
/**
* Resets the extra config object
* @returns {void}
*/
function resetExtra() {
extra = {
tokens: null,
range: false,
loc: false,
comment: false,
comments: [],
tolerant: false,
errors: [],
strict: false,
ecmaFeatures: {},
useJSXTextNode: false,
log: console.log // eslint-disable-line no-console
};
}
//------------------------------------------------------------------------------
// Parser
//------------------------------------------------------------------------------
/**
* Parses the given source code to produce a valid AST
* @param {mixed} code TypeScript code
* @param {Object} options configuration object for the parser
* @param {Object} additionalParsingContext additional internal configuration
* @returns {Object} the AST
*/
function generateAST(code, options, additionalParsingContext) {
additionalParsingContext = additionalParsingContext || {};
const toString = String;
if (typeof code !== "string" && !(code instanceof String)) {
code = toString(code);
}
resetExtra();
if (typeof options !== "undefined") {
extra.range = (typeof options.range === "boolean") && options.range;
extra.loc = (typeof options.loc === "boolean") && options.loc;
if (extra.loc && options.source !== null && options.source !== undefined) {
extra.source = toString(options.source);
}
if (typeof options.tokens === "boolean" && options.tokens) {
extra.tokens = [];
}
if (typeof options.comment === "boolean" && options.comment) {
extra.comment = true;
extra.comments = [];
}
if (typeof options.tolerant === "boolean" && options.tolerant) {
extra.errors = [];
}
const hasEcmaFeatures = options.ecmaFeatures && typeof options.ecmaFeatures === "object";
// Allows user to parse a string of text passed on the command line in JSX mode.
if (hasEcmaFeatures) {
extra.ecmaFeatures.jsx = options.ecmaFeatures.jsx;
}
const hasFilePath = additionalParsingContext.isParseForESLint ? options.filePath !== DEFAULT_ESLINT_FILEPATH : options.filePath;
const hasTsxExtension = hasFilePath && /.tsx$/.test(options.filePath);
// Infer whether or not the parser should parse in "JSX mode" or not.
// This will override the parserOptions.ecmaFeatures.jsx config option if a filePath is provided.
if (hasFilePath) {
extra.ecmaFeatures.jsx = hasTsxExtension;
}
/**
* Allow the user to cause the parser to error if it encounters an unknown AST Node Type
* (used in testing).
*/
if (options.errorOnUnknownASTType) {
extra.errorOnUnknownASTType = true;
}
if (typeof options.useJSXTextNode === "boolean" && options.useJSXTextNode) {
extra.useJSXTextNode = true;
}
/**
* Allow the user to override the function used for logging
*/
if (typeof options.loggerFn === "function") {
extra.log = options.loggerFn;
} else if (options.loggerFn === false) {
extra.log = Function.prototype;
}
/**
* Provide the context as to whether or not we are parsing for ESLint,
* specifically
*/
if (additionalParsingContext.isParseForESLint) {
extra.parseForESLint = true;
}
if (!warnedAboutJSXOverride && hasFilePath && hasEcmaFeatures && typeof options.ecmaFeatures.jsx !== "undefined") {
const warning = [
WARNING_BORDER,
"typescript-eslint-parser will automatically detect whether it should be parsing in JSX mode or not based on file extension.",
"Consider removing parserOptions.ecmaFeatures.jsx from your configuration, as it will be overridden by the extension of the file being parsed.",
WARNING_BORDER
];
extra.log(warning.join("\n\n"));
warnedAboutJSXOverride = true;
}
}
if (!isRunningSupportedTypeScriptVersion && !warnedAboutTSVersion) {
const versionWarning = [
WARNING_BORDER,
"WARNING: You are currently running a version of TypeScript which is not officially supported by typescript-eslint-parser.",
"You may find that it works just fine, or you may not.",
`SUPPORTED TYPESCRIPT VERSIONS: ${SUPPORTED_TYPESCRIPT_VERSIONS}`,
`YOUR TYPESCRIPT VERSION: ${ACTIVE_TYPESCRIPT_VERSION}`,
"Please only submit bug reports when using the officially supported version.",
WARNING_BORDER
];
extra.log(versionWarning.join("\n\n"));
warnedAboutTSVersion = true;
}
// Even if jsx option is set in typescript compiler, filename still has to
// contain .tsx file extension
const FILENAME = (extra.ecmaFeatures.jsx) ? "eslint.tsx" : "eslint.ts";
const compilerHost = {
fileExists() {
return true;
},
getCanonicalFileName() {
return FILENAME;
},
getCurrentDirectory() {
return "";
},
getDefaultLibFileName() {
return "lib.d.ts";
},
// TODO: Support Windows CRLF
getNewLine() {
return "\n";
},
getSourceFile(filename) {
return ts.createSourceFile(filename, code, ts.ScriptTarget.Latest, true);
},
readFile() {
return null;
},
useCaseSensitiveFileNames() {
return true;
},
writeFile() {
return null;
}
};
const program = ts.createProgram([FILENAME], {
noResolve: true,
target: ts.ScriptTarget.Latest,
jsx: extra.ecmaFeatures.jsx ? "preserve" : undefined
}, compilerHost);
const ast = program.getSourceFile(FILENAME);
extra.code = code;
return convert(ast, extra);
}
//------------------------------------------------------------------------------
// Public
//------------------------------------------------------------------------------
exports.version = require("./package.json").version;
exports.parse = function parse(code, options) {
return generateAST(code, options, { isParseForESLint: false });
};
exports.parseForESLint = function parseForESLint(code, options) {
const ast = generateAST(code, options, { isParseForESLint: true });
return { ast };
};
// Deep copy.
/* istanbul ignore next */
exports.Syntax = (function() {
let name,
types = {};
if (typeof Object.create === "function") {
types = Object.create(null);
}
for (name in astNodeTypes) {
if (astNodeTypes.hasOwnProperty(name)) {
types[name] = astNodeTypes[name];
}
}
if (typeof Object.freeze === "function") {
Object.freeze(types);
}
return types;
}());