1 | /* eslint no-param-reassign: 0 -- stylistic choice */
|
---|
2 |
|
---|
3 | import TokenTranslator from "./token-translator.js";
|
---|
4 | import { normalizeOptions } from "./options.js";
|
---|
5 |
|
---|
6 |
|
---|
7 | const STATE = Symbol("espree's internal state");
|
---|
8 | const ESPRIMA_FINISH_NODE = Symbol("espree's esprimaFinishNode");
|
---|
9 |
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Converts an Acorn comment to a Esprima comment.
|
---|
13 | * @param {boolean} block True if it's a block comment, false if not.
|
---|
14 | * @param {string} text The text of the comment.
|
---|
15 | * @param {int} start The index at which the comment starts.
|
---|
16 | * @param {int} end The index at which the comment ends.
|
---|
17 | * @param {Location} startLoc The location at which the comment starts.
|
---|
18 | * @param {Location} endLoc The location at which the comment ends.
|
---|
19 | * @param {string} code The source code being parsed.
|
---|
20 | * @returns {Object} The comment object.
|
---|
21 | * @private
|
---|
22 | */
|
---|
23 | function convertAcornCommentToEsprimaComment(block, text, start, end, startLoc, endLoc, code) {
|
---|
24 | let type;
|
---|
25 |
|
---|
26 | if (block) {
|
---|
27 | type = "Block";
|
---|
28 | } else if (code.slice(start, start + 2) === "#!") {
|
---|
29 | type = "Hashbang";
|
---|
30 | } else {
|
---|
31 | type = "Line";
|
---|
32 | }
|
---|
33 |
|
---|
34 | const comment = {
|
---|
35 | type,
|
---|
36 | value: text
|
---|
37 | };
|
---|
38 |
|
---|
39 | if (typeof start === "number") {
|
---|
40 | comment.start = start;
|
---|
41 | comment.end = end;
|
---|
42 | comment.range = [start, end];
|
---|
43 | }
|
---|
44 |
|
---|
45 | if (typeof startLoc === "object") {
|
---|
46 | comment.loc = {
|
---|
47 | start: startLoc,
|
---|
48 | end: endLoc
|
---|
49 | };
|
---|
50 | }
|
---|
51 |
|
---|
52 | return comment;
|
---|
53 | }
|
---|
54 |
|
---|
55 | export default () => Parser => {
|
---|
56 | const tokTypes = Object.assign({}, Parser.acorn.tokTypes);
|
---|
57 |
|
---|
58 | if (Parser.acornJsx) {
|
---|
59 | Object.assign(tokTypes, Parser.acornJsx.tokTypes);
|
---|
60 | }
|
---|
61 |
|
---|
62 | return class Espree extends Parser {
|
---|
63 | constructor(opts, code) {
|
---|
64 | if (typeof opts !== "object" || opts === null) {
|
---|
65 | opts = {};
|
---|
66 | }
|
---|
67 | if (typeof code !== "string" && !(code instanceof String)) {
|
---|
68 | code = String(code);
|
---|
69 | }
|
---|
70 |
|
---|
71 | // save original source type in case of commonjs
|
---|
72 | const originalSourceType = opts.sourceType;
|
---|
73 | const options = normalizeOptions(opts);
|
---|
74 | const ecmaFeatures = options.ecmaFeatures || {};
|
---|
75 | const tokenTranslator =
|
---|
76 | options.tokens === true
|
---|
77 | ? new TokenTranslator(tokTypes, code)
|
---|
78 | : null;
|
---|
79 |
|
---|
80 | /*
|
---|
81 | * Data that is unique to Espree and is not represented internally
|
---|
82 | * in Acorn.
|
---|
83 | *
|
---|
84 | * For ES2023 hashbangs, Espree will call `onComment()` during the
|
---|
85 | * constructor, so we must define state before having access to
|
---|
86 | * `this`.
|
---|
87 | */
|
---|
88 | const state = {
|
---|
89 | originalSourceType: originalSourceType || options.sourceType,
|
---|
90 | tokens: tokenTranslator ? [] : null,
|
---|
91 | comments: options.comment === true ? [] : null,
|
---|
92 | impliedStrict: ecmaFeatures.impliedStrict === true && options.ecmaVersion >= 5,
|
---|
93 | ecmaVersion: options.ecmaVersion,
|
---|
94 | jsxAttrValueToken: false,
|
---|
95 | lastToken: null,
|
---|
96 | templateElements: []
|
---|
97 | };
|
---|
98 |
|
---|
99 | // Initialize acorn parser.
|
---|
100 | super({
|
---|
101 |
|
---|
102 | // do not use spread, because we don't want to pass any unknown options to acorn
|
---|
103 | ecmaVersion: options.ecmaVersion,
|
---|
104 | sourceType: options.sourceType,
|
---|
105 | ranges: options.ranges,
|
---|
106 | locations: options.locations,
|
---|
107 | allowReserved: options.allowReserved,
|
---|
108 |
|
---|
109 | // Truthy value is true for backward compatibility.
|
---|
110 | allowReturnOutsideFunction: options.allowReturnOutsideFunction,
|
---|
111 |
|
---|
112 | // Collect tokens
|
---|
113 | onToken(token) {
|
---|
114 | if (tokenTranslator) {
|
---|
115 |
|
---|
116 | // Use `tokens`, `ecmaVersion`, and `jsxAttrValueToken` in the state.
|
---|
117 | tokenTranslator.onToken(token, state);
|
---|
118 | }
|
---|
119 | if (token.type !== tokTypes.eof) {
|
---|
120 | state.lastToken = token;
|
---|
121 | }
|
---|
122 | },
|
---|
123 |
|
---|
124 | // Collect comments
|
---|
125 | onComment(block, text, start, end, startLoc, endLoc) {
|
---|
126 | if (state.comments) {
|
---|
127 | const comment = convertAcornCommentToEsprimaComment(block, text, start, end, startLoc, endLoc, code);
|
---|
128 |
|
---|
129 | state.comments.push(comment);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }, code);
|
---|
133 |
|
---|
134 | /*
|
---|
135 | * We put all of this data into a symbol property as a way to avoid
|
---|
136 | * potential naming conflicts with future versions of Acorn.
|
---|
137 | */
|
---|
138 | this[STATE] = state;
|
---|
139 | }
|
---|
140 |
|
---|
141 | tokenize() {
|
---|
142 | do {
|
---|
143 | this.next();
|
---|
144 | } while (this.type !== tokTypes.eof);
|
---|
145 |
|
---|
146 | // Consume the final eof token
|
---|
147 | this.next();
|
---|
148 |
|
---|
149 | const extra = this[STATE];
|
---|
150 | const tokens = extra.tokens;
|
---|
151 |
|
---|
152 | if (extra.comments) {
|
---|
153 | tokens.comments = extra.comments;
|
---|
154 | }
|
---|
155 |
|
---|
156 | return tokens;
|
---|
157 | }
|
---|
158 |
|
---|
159 | finishNode(...args) {
|
---|
160 | const result = super.finishNode(...args);
|
---|
161 |
|
---|
162 | return this[ESPRIMA_FINISH_NODE](result);
|
---|
163 | }
|
---|
164 |
|
---|
165 | finishNodeAt(...args) {
|
---|
166 | const result = super.finishNodeAt(...args);
|
---|
167 |
|
---|
168 | return this[ESPRIMA_FINISH_NODE](result);
|
---|
169 | }
|
---|
170 |
|
---|
171 | parse() {
|
---|
172 | const extra = this[STATE];
|
---|
173 | const program = super.parse();
|
---|
174 |
|
---|
175 | program.sourceType = extra.originalSourceType;
|
---|
176 |
|
---|
177 | if (extra.comments) {
|
---|
178 | program.comments = extra.comments;
|
---|
179 | }
|
---|
180 | if (extra.tokens) {
|
---|
181 | program.tokens = extra.tokens;
|
---|
182 | }
|
---|
183 |
|
---|
184 | /*
|
---|
185 | * Adjust opening and closing position of program to match Esprima.
|
---|
186 | * Acorn always starts programs at range 0 whereas Esprima starts at the
|
---|
187 | * first AST node's start (the only real difference is when there's leading
|
---|
188 | * whitespace or leading comments). Acorn also counts trailing whitespace
|
---|
189 | * as part of the program whereas Esprima only counts up to the last token.
|
---|
190 | */
|
---|
191 | if (program.body.length) {
|
---|
192 | const [firstNode] = program.body;
|
---|
193 |
|
---|
194 | if (program.range) {
|
---|
195 | program.range[0] = firstNode.range[0];
|
---|
196 | }
|
---|
197 | if (program.loc) {
|
---|
198 | program.loc.start = firstNode.loc.start;
|
---|
199 | }
|
---|
200 | program.start = firstNode.start;
|
---|
201 | }
|
---|
202 | if (extra.lastToken) {
|
---|
203 | if (program.range) {
|
---|
204 | program.range[1] = extra.lastToken.range[1];
|
---|
205 | }
|
---|
206 | if (program.loc) {
|
---|
207 | program.loc.end = extra.lastToken.loc.end;
|
---|
208 | }
|
---|
209 | program.end = extra.lastToken.end;
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | /*
|
---|
214 | * https://github.com/eslint/espree/issues/349
|
---|
215 | * Ensure that template elements have correct range information.
|
---|
216 | * This is one location where Acorn produces a different value
|
---|
217 | * for its start and end properties vs. the values present in the
|
---|
218 | * range property. In order to avoid confusion, we set the start
|
---|
219 | * and end properties to the values that are present in range.
|
---|
220 | * This is done here, instead of in finishNode(), because Acorn
|
---|
221 | * uses the values of start and end internally while parsing, making
|
---|
222 | * it dangerous to change those values while parsing is ongoing.
|
---|
223 | * By waiting until the end of parsing, we can safely change these
|
---|
224 | * values without affect any other part of the process.
|
---|
225 | */
|
---|
226 | this[STATE].templateElements.forEach(templateElement => {
|
---|
227 | const startOffset = -1;
|
---|
228 | const endOffset = templateElement.tail ? 1 : 2;
|
---|
229 |
|
---|
230 | templateElement.start += startOffset;
|
---|
231 | templateElement.end += endOffset;
|
---|
232 |
|
---|
233 | if (templateElement.range) {
|
---|
234 | templateElement.range[0] += startOffset;
|
---|
235 | templateElement.range[1] += endOffset;
|
---|
236 | }
|
---|
237 |
|
---|
238 | if (templateElement.loc) {
|
---|
239 | templateElement.loc.start.column += startOffset;
|
---|
240 | templateElement.loc.end.column += endOffset;
|
---|
241 | }
|
---|
242 | });
|
---|
243 |
|
---|
244 | return program;
|
---|
245 | }
|
---|
246 |
|
---|
247 | parseTopLevel(node) {
|
---|
248 | if (this[STATE].impliedStrict) {
|
---|
249 | this.strict = true;
|
---|
250 | }
|
---|
251 | return super.parseTopLevel(node);
|
---|
252 | }
|
---|
253 |
|
---|
254 | /**
|
---|
255 | * Overwrites the default raise method to throw Esprima-style errors.
|
---|
256 | * @param {int} pos The position of the error.
|
---|
257 | * @param {string} message The error message.
|
---|
258 | * @throws {SyntaxError} A syntax error.
|
---|
259 | * @returns {void}
|
---|
260 | */
|
---|
261 | raise(pos, message) {
|
---|
262 | const loc = Parser.acorn.getLineInfo(this.input, pos);
|
---|
263 | const err = new SyntaxError(message);
|
---|
264 |
|
---|
265 | err.index = pos;
|
---|
266 | err.lineNumber = loc.line;
|
---|
267 | err.column = loc.column + 1; // acorn uses 0-based columns
|
---|
268 | throw err;
|
---|
269 | }
|
---|
270 |
|
---|
271 | /**
|
---|
272 | * Overwrites the default raise method to throw Esprima-style errors.
|
---|
273 | * @param {int} pos The position of the error.
|
---|
274 | * @param {string} message The error message.
|
---|
275 | * @throws {SyntaxError} A syntax error.
|
---|
276 | * @returns {void}
|
---|
277 | */
|
---|
278 | raiseRecoverable(pos, message) {
|
---|
279 | this.raise(pos, message);
|
---|
280 | }
|
---|
281 |
|
---|
282 | /**
|
---|
283 | * Overwrites the default unexpected method to throw Esprima-style errors.
|
---|
284 | * @param {int} pos The position of the error.
|
---|
285 | * @throws {SyntaxError} A syntax error.
|
---|
286 | * @returns {void}
|
---|
287 | */
|
---|
288 | unexpected(pos) {
|
---|
289 | let message = "Unexpected token";
|
---|
290 |
|
---|
291 | if (pos !== null && pos !== void 0) {
|
---|
292 | this.pos = pos;
|
---|
293 |
|
---|
294 | if (this.options.locations) {
|
---|
295 | while (this.pos < this.lineStart) {
|
---|
296 | this.lineStart = this.input.lastIndexOf("\n", this.lineStart - 2) + 1;
|
---|
297 | --this.curLine;
|
---|
298 | }
|
---|
299 | }
|
---|
300 |
|
---|
301 | this.nextToken();
|
---|
302 | }
|
---|
303 |
|
---|
304 | if (this.end > this.start) {
|
---|
305 | message += ` ${this.input.slice(this.start, this.end)}`;
|
---|
306 | }
|
---|
307 |
|
---|
308 | this.raise(this.start, message);
|
---|
309 | }
|
---|
310 |
|
---|
311 | /*
|
---|
312 | * Esprima-FB represents JSX strings as tokens called "JSXText", but Acorn-JSX
|
---|
313 | * uses regular tt.string without any distinction between this and regular JS
|
---|
314 | * strings. As such, we intercept an attempt to read a JSX string and set a flag
|
---|
315 | * on extra so that when tokens are converted, the next token will be switched
|
---|
316 | * to JSXText via onToken.
|
---|
317 | */
|
---|
318 | jsx_readString(quote) { // eslint-disable-line camelcase -- required by API
|
---|
319 | const result = super.jsx_readString(quote);
|
---|
320 |
|
---|
321 | if (this.type === tokTypes.string) {
|
---|
322 | this[STATE].jsxAttrValueToken = true;
|
---|
323 | }
|
---|
324 | return result;
|
---|
325 | }
|
---|
326 |
|
---|
327 | /**
|
---|
328 | * Performs last-minute Esprima-specific compatibility checks and fixes.
|
---|
329 | * @param {ASTNode} result The node to check.
|
---|
330 | * @returns {ASTNode} The finished node.
|
---|
331 | */
|
---|
332 | [ESPRIMA_FINISH_NODE](result) {
|
---|
333 |
|
---|
334 | // Acorn doesn't count the opening and closing backticks as part of templates
|
---|
335 | // so we have to adjust ranges/locations appropriately.
|
---|
336 | if (result.type === "TemplateElement") {
|
---|
337 |
|
---|
338 | // save template element references to fix start/end later
|
---|
339 | this[STATE].templateElements.push(result);
|
---|
340 | }
|
---|
341 |
|
---|
342 | if (result.type.includes("Function") && !result.generator) {
|
---|
343 | result.generator = false;
|
---|
344 | }
|
---|
345 |
|
---|
346 | return result;
|
---|
347 | }
|
---|
348 | };
|
---|
349 | };
|
---|