[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, '__esModule', { value: true });
|
---|
| 4 |
|
---|
| 5 | var acorn = require('acorn');
|
---|
| 6 | var jsx = require('acorn-jsx');
|
---|
| 7 | var visitorKeys = require('eslint-visitor-keys');
|
---|
| 8 |
|
---|
| 9 | function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
---|
| 10 |
|
---|
| 11 | function _interopNamespace(e) {
|
---|
| 12 | if (e && e.__esModule) return e;
|
---|
| 13 | var n = Object.create(null);
|
---|
| 14 | if (e) {
|
---|
| 15 | Object.keys(e).forEach(function (k) {
|
---|
| 16 | if (k !== 'default') {
|
---|
| 17 | var d = Object.getOwnPropertyDescriptor(e, k);
|
---|
| 18 | Object.defineProperty(n, k, d.get ? d : {
|
---|
| 19 | enumerable: true,
|
---|
| 20 | get: function () { return e[k]; }
|
---|
| 21 | });
|
---|
| 22 | }
|
---|
| 23 | });
|
---|
| 24 | }
|
---|
| 25 | n["default"] = e;
|
---|
| 26 | return Object.freeze(n);
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | var acorn__namespace = /*#__PURE__*/_interopNamespace(acorn);
|
---|
| 30 | var jsx__default = /*#__PURE__*/_interopDefaultLegacy(jsx);
|
---|
| 31 | var visitorKeys__namespace = /*#__PURE__*/_interopNamespace(visitorKeys);
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @fileoverview Translates tokens between Acorn format and Esprima format.
|
---|
| 35 | * @author Nicholas C. Zakas
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | //------------------------------------------------------------------------------
|
---|
| 39 | // Requirements
|
---|
| 40 | //------------------------------------------------------------------------------
|
---|
| 41 |
|
---|
| 42 | // none!
|
---|
| 43 |
|
---|
| 44 | //------------------------------------------------------------------------------
|
---|
| 45 | // Private
|
---|
| 46 | //------------------------------------------------------------------------------
|
---|
| 47 |
|
---|
| 48 |
|
---|
| 49 | // Esprima Token Types
|
---|
| 50 | const Token = {
|
---|
| 51 | Boolean: "Boolean",
|
---|
| 52 | EOF: "<end>",
|
---|
| 53 | Identifier: "Identifier",
|
---|
| 54 | PrivateIdentifier: "PrivateIdentifier",
|
---|
| 55 | Keyword: "Keyword",
|
---|
| 56 | Null: "Null",
|
---|
| 57 | Numeric: "Numeric",
|
---|
| 58 | Punctuator: "Punctuator",
|
---|
| 59 | String: "String",
|
---|
| 60 | RegularExpression: "RegularExpression",
|
---|
| 61 | Template: "Template",
|
---|
| 62 | JSXIdentifier: "JSXIdentifier",
|
---|
| 63 | JSXText: "JSXText"
|
---|
| 64 | };
|
---|
| 65 |
|
---|
| 66 | /**
|
---|
| 67 | * Converts part of a template into an Esprima token.
|
---|
| 68 | * @param {AcornToken[]} tokens The Acorn tokens representing the template.
|
---|
| 69 | * @param {string} code The source code.
|
---|
| 70 | * @returns {EsprimaToken} The Esprima equivalent of the template token.
|
---|
| 71 | * @private
|
---|
| 72 | */
|
---|
| 73 | function convertTemplatePart(tokens, code) {
|
---|
| 74 | const firstToken = tokens[0],
|
---|
| 75 | lastTemplateToken = tokens[tokens.length - 1];
|
---|
| 76 |
|
---|
| 77 | const token = {
|
---|
| 78 | type: Token.Template,
|
---|
| 79 | value: code.slice(firstToken.start, lastTemplateToken.end)
|
---|
| 80 | };
|
---|
| 81 |
|
---|
| 82 | if (firstToken.loc) {
|
---|
| 83 | token.loc = {
|
---|
| 84 | start: firstToken.loc.start,
|
---|
| 85 | end: lastTemplateToken.loc.end
|
---|
| 86 | };
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | if (firstToken.range) {
|
---|
| 90 | token.start = firstToken.range[0];
|
---|
| 91 | token.end = lastTemplateToken.range[1];
|
---|
| 92 | token.range = [token.start, token.end];
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | return token;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /**
|
---|
| 99 | * Contains logic to translate Acorn tokens into Esprima tokens.
|
---|
| 100 | * @param {Object} acornTokTypes The Acorn token types.
|
---|
| 101 | * @param {string} code The source code Acorn is parsing. This is necessary
|
---|
| 102 | * to correct the "value" property of some tokens.
|
---|
| 103 | * @constructor
|
---|
| 104 | */
|
---|
| 105 | function TokenTranslator(acornTokTypes, code) {
|
---|
| 106 |
|
---|
| 107 | // token types
|
---|
| 108 | this._acornTokTypes = acornTokTypes;
|
---|
| 109 |
|
---|
| 110 | // token buffer for templates
|
---|
| 111 | this._tokens = [];
|
---|
| 112 |
|
---|
| 113 | // track the last curly brace
|
---|
| 114 | this._curlyBrace = null;
|
---|
| 115 |
|
---|
| 116 | // the source code
|
---|
| 117 | this._code = code;
|
---|
| 118 |
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | TokenTranslator.prototype = {
|
---|
| 122 | constructor: TokenTranslator,
|
---|
| 123 |
|
---|
| 124 | /**
|
---|
| 125 | * Translates a single Esprima token to a single Acorn token. This may be
|
---|
| 126 | * inaccurate due to how templates are handled differently in Esprima and
|
---|
| 127 | * Acorn, but should be accurate for all other tokens.
|
---|
| 128 | * @param {AcornToken} token The Acorn token to translate.
|
---|
| 129 | * @param {Object} extra Espree extra object.
|
---|
| 130 | * @returns {EsprimaToken} The Esprima version of the token.
|
---|
| 131 | */
|
---|
| 132 | translate(token, extra) {
|
---|
| 133 |
|
---|
| 134 | const type = token.type,
|
---|
| 135 | tt = this._acornTokTypes;
|
---|
| 136 |
|
---|
| 137 | if (type === tt.name) {
|
---|
| 138 | token.type = Token.Identifier;
|
---|
| 139 |
|
---|
| 140 | // TODO: See if this is an Acorn bug
|
---|
| 141 | if (token.value === "static") {
|
---|
| 142 | token.type = Token.Keyword;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | if (extra.ecmaVersion > 5 && (token.value === "yield" || token.value === "let")) {
|
---|
| 146 | token.type = Token.Keyword;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | } else if (type === tt.privateId) {
|
---|
| 150 | token.type = Token.PrivateIdentifier;
|
---|
| 151 |
|
---|
| 152 | } else if (type === tt.semi || type === tt.comma ||
|
---|
| 153 | type === tt.parenL || type === tt.parenR ||
|
---|
| 154 | type === tt.braceL || type === tt.braceR ||
|
---|
| 155 | type === tt.dot || type === tt.bracketL ||
|
---|
| 156 | type === tt.colon || type === tt.question ||
|
---|
| 157 | type === tt.bracketR || type === tt.ellipsis ||
|
---|
| 158 | type === tt.arrow || type === tt.jsxTagStart ||
|
---|
| 159 | type === tt.incDec || type === tt.starstar ||
|
---|
| 160 | type === tt.jsxTagEnd || type === tt.prefix ||
|
---|
| 161 | type === tt.questionDot ||
|
---|
| 162 | (type.binop && !type.keyword) ||
|
---|
| 163 | type.isAssign) {
|
---|
| 164 |
|
---|
| 165 | token.type = Token.Punctuator;
|
---|
| 166 | token.value = this._code.slice(token.start, token.end);
|
---|
| 167 | } else if (type === tt.jsxName) {
|
---|
| 168 | token.type = Token.JSXIdentifier;
|
---|
| 169 | } else if (type.label === "jsxText" || type === tt.jsxAttrValueToken) {
|
---|
| 170 | token.type = Token.JSXText;
|
---|
| 171 | } else if (type.keyword) {
|
---|
| 172 | if (type.keyword === "true" || type.keyword === "false") {
|
---|
| 173 | token.type = Token.Boolean;
|
---|
| 174 | } else if (type.keyword === "null") {
|
---|
| 175 | token.type = Token.Null;
|
---|
| 176 | } else {
|
---|
| 177 | token.type = Token.Keyword;
|
---|
| 178 | }
|
---|
| 179 | } else if (type === tt.num) {
|
---|
| 180 | token.type = Token.Numeric;
|
---|
| 181 | token.value = this._code.slice(token.start, token.end);
|
---|
| 182 | } else if (type === tt.string) {
|
---|
| 183 |
|
---|
| 184 | if (extra.jsxAttrValueToken) {
|
---|
| 185 | extra.jsxAttrValueToken = false;
|
---|
| 186 | token.type = Token.JSXText;
|
---|
| 187 | } else {
|
---|
| 188 | token.type = Token.String;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | token.value = this._code.slice(token.start, token.end);
|
---|
| 192 | } else if (type === tt.regexp) {
|
---|
| 193 | token.type = Token.RegularExpression;
|
---|
| 194 | const value = token.value;
|
---|
| 195 |
|
---|
| 196 | token.regex = {
|
---|
| 197 | flags: value.flags,
|
---|
| 198 | pattern: value.pattern
|
---|
| 199 | };
|
---|
| 200 | token.value = `/${value.pattern}/${value.flags}`;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | return token;
|
---|
| 204 | },
|
---|
| 205 |
|
---|
| 206 | /**
|
---|
| 207 | * Function to call during Acorn's onToken handler.
|
---|
| 208 | * @param {AcornToken} token The Acorn token.
|
---|
| 209 | * @param {Object} extra The Espree extra object.
|
---|
| 210 | * @returns {void}
|
---|
| 211 | */
|
---|
| 212 | onToken(token, extra) {
|
---|
| 213 |
|
---|
| 214 | const tt = this._acornTokTypes,
|
---|
| 215 | tokens = extra.tokens,
|
---|
| 216 | templateTokens = this._tokens;
|
---|
| 217 |
|
---|
| 218 | /**
|
---|
| 219 | * Flushes the buffered template tokens and resets the template
|
---|
| 220 | * tracking.
|
---|
| 221 | * @returns {void}
|
---|
| 222 | * @private
|
---|
| 223 | */
|
---|
| 224 | const translateTemplateTokens = () => {
|
---|
| 225 | tokens.push(convertTemplatePart(this._tokens, this._code));
|
---|
| 226 | this._tokens = [];
|
---|
| 227 | };
|
---|
| 228 |
|
---|
| 229 | if (token.type === tt.eof) {
|
---|
| 230 |
|
---|
| 231 | // might be one last curlyBrace
|
---|
| 232 | if (this._curlyBrace) {
|
---|
| 233 | tokens.push(this.translate(this._curlyBrace, extra));
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | return;
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | if (token.type === tt.backQuote) {
|
---|
| 240 |
|
---|
| 241 | // if there's already a curly, it's not part of the template
|
---|
| 242 | if (this._curlyBrace) {
|
---|
| 243 | tokens.push(this.translate(this._curlyBrace, extra));
|
---|
| 244 | this._curlyBrace = null;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | templateTokens.push(token);
|
---|
| 248 |
|
---|
| 249 | // it's the end
|
---|
| 250 | if (templateTokens.length > 1) {
|
---|
| 251 | translateTemplateTokens();
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | return;
|
---|
| 255 | }
|
---|
| 256 | if (token.type === tt.dollarBraceL) {
|
---|
| 257 | templateTokens.push(token);
|
---|
| 258 | translateTemplateTokens();
|
---|
| 259 | return;
|
---|
| 260 | }
|
---|
| 261 | if (token.type === tt.braceR) {
|
---|
| 262 |
|
---|
| 263 | // if there's already a curly, it's not part of the template
|
---|
| 264 | if (this._curlyBrace) {
|
---|
| 265 | tokens.push(this.translate(this._curlyBrace, extra));
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | // store new curly for later
|
---|
| 269 | this._curlyBrace = token;
|
---|
| 270 | return;
|
---|
| 271 | }
|
---|
| 272 | if (token.type === tt.template || token.type === tt.invalidTemplate) {
|
---|
| 273 | if (this._curlyBrace) {
|
---|
| 274 | templateTokens.push(this._curlyBrace);
|
---|
| 275 | this._curlyBrace = null;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | templateTokens.push(token);
|
---|
| 279 | return;
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | if (this._curlyBrace) {
|
---|
| 283 | tokens.push(this.translate(this._curlyBrace, extra));
|
---|
| 284 | this._curlyBrace = null;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | tokens.push(this.translate(token, extra));
|
---|
| 288 | }
|
---|
| 289 | };
|
---|
| 290 |
|
---|
| 291 | /**
|
---|
| 292 | * @fileoverview A collection of methods for processing Espree's options.
|
---|
| 293 | * @author Kai Cataldo
|
---|
| 294 | */
|
---|
| 295 |
|
---|
| 296 | //------------------------------------------------------------------------------
|
---|
| 297 | // Helpers
|
---|
| 298 | //------------------------------------------------------------------------------
|
---|
| 299 |
|
---|
| 300 | const SUPPORTED_VERSIONS = [
|
---|
| 301 | 3,
|
---|
| 302 | 5,
|
---|
| 303 | 6, // 2015
|
---|
| 304 | 7, // 2016
|
---|
| 305 | 8, // 2017
|
---|
| 306 | 9, // 2018
|
---|
| 307 | 10, // 2019
|
---|
| 308 | 11, // 2020
|
---|
| 309 | 12, // 2021
|
---|
| 310 | 13, // 2022
|
---|
| 311 | 14, // 2023
|
---|
| 312 | 15 // 2024
|
---|
| 313 | ];
|
---|
| 314 |
|
---|
| 315 | /**
|
---|
| 316 | * Get the latest ECMAScript version supported by Espree.
|
---|
| 317 | * @returns {number} The latest ECMAScript version.
|
---|
| 318 | */
|
---|
| 319 | function getLatestEcmaVersion() {
|
---|
| 320 | return SUPPORTED_VERSIONS[SUPPORTED_VERSIONS.length - 1];
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | /**
|
---|
| 324 | * Get the list of ECMAScript versions supported by Espree.
|
---|
| 325 | * @returns {number[]} An array containing the supported ECMAScript versions.
|
---|
| 326 | */
|
---|
| 327 | function getSupportedEcmaVersions() {
|
---|
| 328 | return [...SUPPORTED_VERSIONS];
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | /**
|
---|
| 332 | * Normalize ECMAScript version from the initial config
|
---|
| 333 | * @param {(number|"latest")} ecmaVersion ECMAScript version from the initial config
|
---|
| 334 | * @throws {Error} throws an error if the ecmaVersion is invalid.
|
---|
| 335 | * @returns {number} normalized ECMAScript version
|
---|
| 336 | */
|
---|
| 337 | function normalizeEcmaVersion(ecmaVersion = 5) {
|
---|
| 338 |
|
---|
| 339 | let version = ecmaVersion === "latest" ? getLatestEcmaVersion() : ecmaVersion;
|
---|
| 340 |
|
---|
| 341 | if (typeof version !== "number") {
|
---|
| 342 | throw new Error(`ecmaVersion must be a number or "latest". Received value of type ${typeof ecmaVersion} instead.`);
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | // Calculate ECMAScript edition number from official year version starting with
|
---|
| 346 | // ES2015, which corresponds with ES6 (or a difference of 2009).
|
---|
| 347 | if (version >= 2015) {
|
---|
| 348 | version -= 2009;
|
---|
| 349 | }
|
---|
| 350 |
|
---|
| 351 | if (!SUPPORTED_VERSIONS.includes(version)) {
|
---|
| 352 | throw new Error("Invalid ecmaVersion.");
|
---|
| 353 | }
|
---|
| 354 |
|
---|
| 355 | return version;
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | /**
|
---|
| 359 | * Normalize sourceType from the initial config
|
---|
| 360 | * @param {string} sourceType to normalize
|
---|
| 361 | * @throws {Error} throw an error if sourceType is invalid
|
---|
| 362 | * @returns {string} normalized sourceType
|
---|
| 363 | */
|
---|
| 364 | function normalizeSourceType(sourceType = "script") {
|
---|
| 365 | if (sourceType === "script" || sourceType === "module") {
|
---|
| 366 | return sourceType;
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 | if (sourceType === "commonjs") {
|
---|
| 370 | return "script";
|
---|
| 371 | }
|
---|
| 372 |
|
---|
| 373 | throw new Error("Invalid sourceType.");
|
---|
| 374 | }
|
---|
| 375 |
|
---|
| 376 | /**
|
---|
| 377 | * Normalize parserOptions
|
---|
| 378 | * @param {Object} options the parser options to normalize
|
---|
| 379 | * @throws {Error} throw an error if found invalid option.
|
---|
| 380 | * @returns {Object} normalized options
|
---|
| 381 | */
|
---|
| 382 | function normalizeOptions(options) {
|
---|
| 383 | const ecmaVersion = normalizeEcmaVersion(options.ecmaVersion);
|
---|
| 384 | const sourceType = normalizeSourceType(options.sourceType);
|
---|
| 385 | const ranges = options.range === true;
|
---|
| 386 | const locations = options.loc === true;
|
---|
| 387 |
|
---|
| 388 | if (ecmaVersion !== 3 && options.allowReserved) {
|
---|
| 389 |
|
---|
| 390 | // a value of `false` is intentionally allowed here, so a shared config can overwrite it when needed
|
---|
| 391 | throw new Error("`allowReserved` is only supported when ecmaVersion is 3");
|
---|
| 392 | }
|
---|
| 393 | if (typeof options.allowReserved !== "undefined" && typeof options.allowReserved !== "boolean") {
|
---|
| 394 | throw new Error("`allowReserved`, when present, must be `true` or `false`");
|
---|
| 395 | }
|
---|
| 396 | const allowReserved = ecmaVersion === 3 ? (options.allowReserved || "never") : false;
|
---|
| 397 | const ecmaFeatures = options.ecmaFeatures || {};
|
---|
| 398 | const allowReturnOutsideFunction = options.sourceType === "commonjs" ||
|
---|
| 399 | Boolean(ecmaFeatures.globalReturn);
|
---|
| 400 |
|
---|
| 401 | if (sourceType === "module" && ecmaVersion < 6) {
|
---|
| 402 | throw new Error("sourceType 'module' is not supported when ecmaVersion < 2015. Consider adding `{ ecmaVersion: 2015 }` to the parser options.");
|
---|
| 403 | }
|
---|
| 404 |
|
---|
| 405 | return Object.assign({}, options, {
|
---|
| 406 | ecmaVersion,
|
---|
| 407 | sourceType,
|
---|
| 408 | ranges,
|
---|
| 409 | locations,
|
---|
| 410 | allowReserved,
|
---|
| 411 | allowReturnOutsideFunction
|
---|
| 412 | });
|
---|
| 413 | }
|
---|
| 414 |
|
---|
| 415 | /* eslint no-param-reassign: 0 -- stylistic choice */
|
---|
| 416 |
|
---|
| 417 |
|
---|
| 418 | const STATE = Symbol("espree's internal state");
|
---|
| 419 | const ESPRIMA_FINISH_NODE = Symbol("espree's esprimaFinishNode");
|
---|
| 420 |
|
---|
| 421 |
|
---|
| 422 | /**
|
---|
| 423 | * Converts an Acorn comment to a Esprima comment.
|
---|
| 424 | * @param {boolean} block True if it's a block comment, false if not.
|
---|
| 425 | * @param {string} text The text of the comment.
|
---|
| 426 | * @param {int} start The index at which the comment starts.
|
---|
| 427 | * @param {int} end The index at which the comment ends.
|
---|
| 428 | * @param {Location} startLoc The location at which the comment starts.
|
---|
| 429 | * @param {Location} endLoc The location at which the comment ends.
|
---|
| 430 | * @param {string} code The source code being parsed.
|
---|
| 431 | * @returns {Object} The comment object.
|
---|
| 432 | * @private
|
---|
| 433 | */
|
---|
| 434 | function convertAcornCommentToEsprimaComment(block, text, start, end, startLoc, endLoc, code) {
|
---|
| 435 | let type;
|
---|
| 436 |
|
---|
| 437 | if (block) {
|
---|
| 438 | type = "Block";
|
---|
| 439 | } else if (code.slice(start, start + 2) === "#!") {
|
---|
| 440 | type = "Hashbang";
|
---|
| 441 | } else {
|
---|
| 442 | type = "Line";
|
---|
| 443 | }
|
---|
| 444 |
|
---|
| 445 | const comment = {
|
---|
| 446 | type,
|
---|
| 447 | value: text
|
---|
| 448 | };
|
---|
| 449 |
|
---|
| 450 | if (typeof start === "number") {
|
---|
| 451 | comment.start = start;
|
---|
| 452 | comment.end = end;
|
---|
| 453 | comment.range = [start, end];
|
---|
| 454 | }
|
---|
| 455 |
|
---|
| 456 | if (typeof startLoc === "object") {
|
---|
| 457 | comment.loc = {
|
---|
| 458 | start: startLoc,
|
---|
| 459 | end: endLoc
|
---|
| 460 | };
|
---|
| 461 | }
|
---|
| 462 |
|
---|
| 463 | return comment;
|
---|
| 464 | }
|
---|
| 465 |
|
---|
| 466 | var espree = () => Parser => {
|
---|
| 467 | const tokTypes = Object.assign({}, Parser.acorn.tokTypes);
|
---|
| 468 |
|
---|
| 469 | if (Parser.acornJsx) {
|
---|
| 470 | Object.assign(tokTypes, Parser.acornJsx.tokTypes);
|
---|
| 471 | }
|
---|
| 472 |
|
---|
| 473 | return class Espree extends Parser {
|
---|
| 474 | constructor(opts, code) {
|
---|
| 475 | if (typeof opts !== "object" || opts === null) {
|
---|
| 476 | opts = {};
|
---|
| 477 | }
|
---|
| 478 | if (typeof code !== "string" && !(code instanceof String)) {
|
---|
| 479 | code = String(code);
|
---|
| 480 | }
|
---|
| 481 |
|
---|
| 482 | // save original source type in case of commonjs
|
---|
| 483 | const originalSourceType = opts.sourceType;
|
---|
| 484 | const options = normalizeOptions(opts);
|
---|
| 485 | const ecmaFeatures = options.ecmaFeatures || {};
|
---|
| 486 | const tokenTranslator =
|
---|
| 487 | options.tokens === true
|
---|
| 488 | ? new TokenTranslator(tokTypes, code)
|
---|
| 489 | : null;
|
---|
| 490 |
|
---|
| 491 | /*
|
---|
| 492 | * Data that is unique to Espree and is not represented internally
|
---|
| 493 | * in Acorn.
|
---|
| 494 | *
|
---|
| 495 | * For ES2023 hashbangs, Espree will call `onComment()` during the
|
---|
| 496 | * constructor, so we must define state before having access to
|
---|
| 497 | * `this`.
|
---|
| 498 | */
|
---|
| 499 | const state = {
|
---|
| 500 | originalSourceType: originalSourceType || options.sourceType,
|
---|
| 501 | tokens: tokenTranslator ? [] : null,
|
---|
| 502 | comments: options.comment === true ? [] : null,
|
---|
| 503 | impliedStrict: ecmaFeatures.impliedStrict === true && options.ecmaVersion >= 5,
|
---|
| 504 | ecmaVersion: options.ecmaVersion,
|
---|
| 505 | jsxAttrValueToken: false,
|
---|
| 506 | lastToken: null,
|
---|
| 507 | templateElements: []
|
---|
| 508 | };
|
---|
| 509 |
|
---|
| 510 | // Initialize acorn parser.
|
---|
| 511 | super({
|
---|
| 512 |
|
---|
| 513 | // do not use spread, because we don't want to pass any unknown options to acorn
|
---|
| 514 | ecmaVersion: options.ecmaVersion,
|
---|
| 515 | sourceType: options.sourceType,
|
---|
| 516 | ranges: options.ranges,
|
---|
| 517 | locations: options.locations,
|
---|
| 518 | allowReserved: options.allowReserved,
|
---|
| 519 |
|
---|
| 520 | // Truthy value is true for backward compatibility.
|
---|
| 521 | allowReturnOutsideFunction: options.allowReturnOutsideFunction,
|
---|
| 522 |
|
---|
| 523 | // Collect tokens
|
---|
| 524 | onToken(token) {
|
---|
| 525 | if (tokenTranslator) {
|
---|
| 526 |
|
---|
| 527 | // Use `tokens`, `ecmaVersion`, and `jsxAttrValueToken` in the state.
|
---|
| 528 | tokenTranslator.onToken(token, state);
|
---|
| 529 | }
|
---|
| 530 | if (token.type !== tokTypes.eof) {
|
---|
| 531 | state.lastToken = token;
|
---|
| 532 | }
|
---|
| 533 | },
|
---|
| 534 |
|
---|
| 535 | // Collect comments
|
---|
| 536 | onComment(block, text, start, end, startLoc, endLoc) {
|
---|
| 537 | if (state.comments) {
|
---|
| 538 | const comment = convertAcornCommentToEsprimaComment(block, text, start, end, startLoc, endLoc, code);
|
---|
| 539 |
|
---|
| 540 | state.comments.push(comment);
|
---|
| 541 | }
|
---|
| 542 | }
|
---|
| 543 | }, code);
|
---|
| 544 |
|
---|
| 545 | /*
|
---|
| 546 | * We put all of this data into a symbol property as a way to avoid
|
---|
| 547 | * potential naming conflicts with future versions of Acorn.
|
---|
| 548 | */
|
---|
| 549 | this[STATE] = state;
|
---|
| 550 | }
|
---|
| 551 |
|
---|
| 552 | tokenize() {
|
---|
| 553 | do {
|
---|
| 554 | this.next();
|
---|
| 555 | } while (this.type !== tokTypes.eof);
|
---|
| 556 |
|
---|
| 557 | // Consume the final eof token
|
---|
| 558 | this.next();
|
---|
| 559 |
|
---|
| 560 | const extra = this[STATE];
|
---|
| 561 | const tokens = extra.tokens;
|
---|
| 562 |
|
---|
| 563 | if (extra.comments) {
|
---|
| 564 | tokens.comments = extra.comments;
|
---|
| 565 | }
|
---|
| 566 |
|
---|
| 567 | return tokens;
|
---|
| 568 | }
|
---|
| 569 |
|
---|
| 570 | finishNode(...args) {
|
---|
| 571 | const result = super.finishNode(...args);
|
---|
| 572 |
|
---|
| 573 | return this[ESPRIMA_FINISH_NODE](result);
|
---|
| 574 | }
|
---|
| 575 |
|
---|
| 576 | finishNodeAt(...args) {
|
---|
| 577 | const result = super.finishNodeAt(...args);
|
---|
| 578 |
|
---|
| 579 | return this[ESPRIMA_FINISH_NODE](result);
|
---|
| 580 | }
|
---|
| 581 |
|
---|
| 582 | parse() {
|
---|
| 583 | const extra = this[STATE];
|
---|
| 584 | const program = super.parse();
|
---|
| 585 |
|
---|
| 586 | program.sourceType = extra.originalSourceType;
|
---|
| 587 |
|
---|
| 588 | if (extra.comments) {
|
---|
| 589 | program.comments = extra.comments;
|
---|
| 590 | }
|
---|
| 591 | if (extra.tokens) {
|
---|
| 592 | program.tokens = extra.tokens;
|
---|
| 593 | }
|
---|
| 594 |
|
---|
| 595 | /*
|
---|
| 596 | * Adjust opening and closing position of program to match Esprima.
|
---|
| 597 | * Acorn always starts programs at range 0 whereas Esprima starts at the
|
---|
| 598 | * first AST node's start (the only real difference is when there's leading
|
---|
| 599 | * whitespace or leading comments). Acorn also counts trailing whitespace
|
---|
| 600 | * as part of the program whereas Esprima only counts up to the last token.
|
---|
| 601 | */
|
---|
| 602 | if (program.body.length) {
|
---|
| 603 | const [firstNode] = program.body;
|
---|
| 604 |
|
---|
| 605 | if (program.range) {
|
---|
| 606 | program.range[0] = firstNode.range[0];
|
---|
| 607 | }
|
---|
| 608 | if (program.loc) {
|
---|
| 609 | program.loc.start = firstNode.loc.start;
|
---|
| 610 | }
|
---|
| 611 | program.start = firstNode.start;
|
---|
| 612 | }
|
---|
| 613 | if (extra.lastToken) {
|
---|
| 614 | if (program.range) {
|
---|
| 615 | program.range[1] = extra.lastToken.range[1];
|
---|
| 616 | }
|
---|
| 617 | if (program.loc) {
|
---|
| 618 | program.loc.end = extra.lastToken.loc.end;
|
---|
| 619 | }
|
---|
| 620 | program.end = extra.lastToken.end;
|
---|
| 621 | }
|
---|
| 622 |
|
---|
| 623 |
|
---|
| 624 | /*
|
---|
| 625 | * https://github.com/eslint/espree/issues/349
|
---|
| 626 | * Ensure that template elements have correct range information.
|
---|
| 627 | * This is one location where Acorn produces a different value
|
---|
| 628 | * for its start and end properties vs. the values present in the
|
---|
| 629 | * range property. In order to avoid confusion, we set the start
|
---|
| 630 | * and end properties to the values that are present in range.
|
---|
| 631 | * This is done here, instead of in finishNode(), because Acorn
|
---|
| 632 | * uses the values of start and end internally while parsing, making
|
---|
| 633 | * it dangerous to change those values while parsing is ongoing.
|
---|
| 634 | * By waiting until the end of parsing, we can safely change these
|
---|
| 635 | * values without affect any other part of the process.
|
---|
| 636 | */
|
---|
| 637 | this[STATE].templateElements.forEach(templateElement => {
|
---|
| 638 | const startOffset = -1;
|
---|
| 639 | const endOffset = templateElement.tail ? 1 : 2;
|
---|
| 640 |
|
---|
| 641 | templateElement.start += startOffset;
|
---|
| 642 | templateElement.end += endOffset;
|
---|
| 643 |
|
---|
| 644 | if (templateElement.range) {
|
---|
| 645 | templateElement.range[0] += startOffset;
|
---|
| 646 | templateElement.range[1] += endOffset;
|
---|
| 647 | }
|
---|
| 648 |
|
---|
| 649 | if (templateElement.loc) {
|
---|
| 650 | templateElement.loc.start.column += startOffset;
|
---|
| 651 | templateElement.loc.end.column += endOffset;
|
---|
| 652 | }
|
---|
| 653 | });
|
---|
| 654 |
|
---|
| 655 | return program;
|
---|
| 656 | }
|
---|
| 657 |
|
---|
| 658 | parseTopLevel(node) {
|
---|
| 659 | if (this[STATE].impliedStrict) {
|
---|
| 660 | this.strict = true;
|
---|
| 661 | }
|
---|
| 662 | return super.parseTopLevel(node);
|
---|
| 663 | }
|
---|
| 664 |
|
---|
| 665 | /**
|
---|
| 666 | * Overwrites the default raise method to throw Esprima-style errors.
|
---|
| 667 | * @param {int} pos The position of the error.
|
---|
| 668 | * @param {string} message The error message.
|
---|
| 669 | * @throws {SyntaxError} A syntax error.
|
---|
| 670 | * @returns {void}
|
---|
| 671 | */
|
---|
| 672 | raise(pos, message) {
|
---|
| 673 | const loc = Parser.acorn.getLineInfo(this.input, pos);
|
---|
| 674 | const err = new SyntaxError(message);
|
---|
| 675 |
|
---|
| 676 | err.index = pos;
|
---|
| 677 | err.lineNumber = loc.line;
|
---|
| 678 | err.column = loc.column + 1; // acorn uses 0-based columns
|
---|
| 679 | throw err;
|
---|
| 680 | }
|
---|
| 681 |
|
---|
| 682 | /**
|
---|
| 683 | * Overwrites the default raise method to throw Esprima-style errors.
|
---|
| 684 | * @param {int} pos The position of the error.
|
---|
| 685 | * @param {string} message The error message.
|
---|
| 686 | * @throws {SyntaxError} A syntax error.
|
---|
| 687 | * @returns {void}
|
---|
| 688 | */
|
---|
| 689 | raiseRecoverable(pos, message) {
|
---|
| 690 | this.raise(pos, message);
|
---|
| 691 | }
|
---|
| 692 |
|
---|
| 693 | /**
|
---|
| 694 | * Overwrites the default unexpected method to throw Esprima-style errors.
|
---|
| 695 | * @param {int} pos The position of the error.
|
---|
| 696 | * @throws {SyntaxError} A syntax error.
|
---|
| 697 | * @returns {void}
|
---|
| 698 | */
|
---|
| 699 | unexpected(pos) {
|
---|
| 700 | let message = "Unexpected token";
|
---|
| 701 |
|
---|
| 702 | if (pos !== null && pos !== void 0) {
|
---|
| 703 | this.pos = pos;
|
---|
| 704 |
|
---|
| 705 | if (this.options.locations) {
|
---|
| 706 | while (this.pos < this.lineStart) {
|
---|
| 707 | this.lineStart = this.input.lastIndexOf("\n", this.lineStart - 2) + 1;
|
---|
| 708 | --this.curLine;
|
---|
| 709 | }
|
---|
| 710 | }
|
---|
| 711 |
|
---|
| 712 | this.nextToken();
|
---|
| 713 | }
|
---|
| 714 |
|
---|
| 715 | if (this.end > this.start) {
|
---|
| 716 | message += ` ${this.input.slice(this.start, this.end)}`;
|
---|
| 717 | }
|
---|
| 718 |
|
---|
| 719 | this.raise(this.start, message);
|
---|
| 720 | }
|
---|
| 721 |
|
---|
| 722 | /*
|
---|
| 723 | * Esprima-FB represents JSX strings as tokens called "JSXText", but Acorn-JSX
|
---|
| 724 | * uses regular tt.string without any distinction between this and regular JS
|
---|
| 725 | * strings. As such, we intercept an attempt to read a JSX string and set a flag
|
---|
| 726 | * on extra so that when tokens are converted, the next token will be switched
|
---|
| 727 | * to JSXText via onToken.
|
---|
| 728 | */
|
---|
| 729 | jsx_readString(quote) { // eslint-disable-line camelcase -- required by API
|
---|
| 730 | const result = super.jsx_readString(quote);
|
---|
| 731 |
|
---|
| 732 | if (this.type === tokTypes.string) {
|
---|
| 733 | this[STATE].jsxAttrValueToken = true;
|
---|
| 734 | }
|
---|
| 735 | return result;
|
---|
| 736 | }
|
---|
| 737 |
|
---|
| 738 | /**
|
---|
| 739 | * Performs last-minute Esprima-specific compatibility checks and fixes.
|
---|
| 740 | * @param {ASTNode} result The node to check.
|
---|
| 741 | * @returns {ASTNode} The finished node.
|
---|
| 742 | */
|
---|
| 743 | [ESPRIMA_FINISH_NODE](result) {
|
---|
| 744 |
|
---|
| 745 | // Acorn doesn't count the opening and closing backticks as part of templates
|
---|
| 746 | // so we have to adjust ranges/locations appropriately.
|
---|
| 747 | if (result.type === "TemplateElement") {
|
---|
| 748 |
|
---|
| 749 | // save template element references to fix start/end later
|
---|
| 750 | this[STATE].templateElements.push(result);
|
---|
| 751 | }
|
---|
| 752 |
|
---|
| 753 | if (result.type.includes("Function") && !result.generator) {
|
---|
| 754 | result.generator = false;
|
---|
| 755 | }
|
---|
| 756 |
|
---|
| 757 | return result;
|
---|
| 758 | }
|
---|
| 759 | };
|
---|
| 760 | };
|
---|
| 761 |
|
---|
| 762 | const version$1 = "9.6.1";
|
---|
| 763 |
|
---|
| 764 | /* eslint-disable jsdoc/no-multi-asterisks -- needed to preserve original formatting of licences */
|
---|
| 765 |
|
---|
| 766 |
|
---|
| 767 | // To initialize lazily.
|
---|
| 768 | const parsers = {
|
---|
| 769 | _regular: null,
|
---|
| 770 | _jsx: null,
|
---|
| 771 |
|
---|
| 772 | get regular() {
|
---|
| 773 | if (this._regular === null) {
|
---|
| 774 | this._regular = acorn__namespace.Parser.extend(espree());
|
---|
| 775 | }
|
---|
| 776 | return this._regular;
|
---|
| 777 | },
|
---|
| 778 |
|
---|
| 779 | get jsx() {
|
---|
| 780 | if (this._jsx === null) {
|
---|
| 781 | this._jsx = acorn__namespace.Parser.extend(jsx__default["default"](), espree());
|
---|
| 782 | }
|
---|
| 783 | return this._jsx;
|
---|
| 784 | },
|
---|
| 785 |
|
---|
| 786 | get(options) {
|
---|
| 787 | const useJsx = Boolean(
|
---|
| 788 | options &&
|
---|
| 789 | options.ecmaFeatures &&
|
---|
| 790 | options.ecmaFeatures.jsx
|
---|
| 791 | );
|
---|
| 792 |
|
---|
| 793 | return useJsx ? this.jsx : this.regular;
|
---|
| 794 | }
|
---|
| 795 | };
|
---|
| 796 |
|
---|
| 797 | //------------------------------------------------------------------------------
|
---|
| 798 | // Tokenizer
|
---|
| 799 | //------------------------------------------------------------------------------
|
---|
| 800 |
|
---|
| 801 | /**
|
---|
| 802 | * Tokenizes the given code.
|
---|
| 803 | * @param {string} code The code to tokenize.
|
---|
| 804 | * @param {Object} options Options defining how to tokenize.
|
---|
| 805 | * @returns {Token[]} An array of tokens.
|
---|
| 806 | * @throws {SyntaxError} If the input code is invalid.
|
---|
| 807 | * @private
|
---|
| 808 | */
|
---|
| 809 | function tokenize(code, options) {
|
---|
| 810 | const Parser = parsers.get(options);
|
---|
| 811 |
|
---|
| 812 | // Ensure to collect tokens.
|
---|
| 813 | if (!options || options.tokens !== true) {
|
---|
| 814 | options = Object.assign({}, options, { tokens: true }); // eslint-disable-line no-param-reassign -- stylistic choice
|
---|
| 815 | }
|
---|
| 816 |
|
---|
| 817 | return new Parser(options, code).tokenize();
|
---|
| 818 | }
|
---|
| 819 |
|
---|
| 820 | //------------------------------------------------------------------------------
|
---|
| 821 | // Parser
|
---|
| 822 | //------------------------------------------------------------------------------
|
---|
| 823 |
|
---|
| 824 | /**
|
---|
| 825 | * Parses the given code.
|
---|
| 826 | * @param {string} code The code to tokenize.
|
---|
| 827 | * @param {Object} options Options defining how to tokenize.
|
---|
| 828 | * @returns {ASTNode} The "Program" AST node.
|
---|
| 829 | * @throws {SyntaxError} If the input code is invalid.
|
---|
| 830 | */
|
---|
| 831 | function parse(code, options) {
|
---|
| 832 | const Parser = parsers.get(options);
|
---|
| 833 |
|
---|
| 834 | return new Parser(options, code).parse();
|
---|
| 835 | }
|
---|
| 836 |
|
---|
| 837 | //------------------------------------------------------------------------------
|
---|
| 838 | // Public
|
---|
| 839 | //------------------------------------------------------------------------------
|
---|
| 840 |
|
---|
| 841 | const version = version$1;
|
---|
| 842 | const name = "espree";
|
---|
| 843 |
|
---|
| 844 | /* istanbul ignore next */
|
---|
| 845 | const VisitorKeys = (function() {
|
---|
| 846 | return visitorKeys__namespace.KEYS;
|
---|
| 847 | }());
|
---|
| 848 |
|
---|
| 849 | // Derive node types from VisitorKeys
|
---|
| 850 | /* istanbul ignore next */
|
---|
| 851 | const Syntax = (function() {
|
---|
| 852 | let key,
|
---|
| 853 | types = {};
|
---|
| 854 |
|
---|
| 855 | if (typeof Object.create === "function") {
|
---|
| 856 | types = Object.create(null);
|
---|
| 857 | }
|
---|
| 858 |
|
---|
| 859 | for (key in VisitorKeys) {
|
---|
| 860 | if (Object.hasOwnProperty.call(VisitorKeys, key)) {
|
---|
| 861 | types[key] = key;
|
---|
| 862 | }
|
---|
| 863 | }
|
---|
| 864 |
|
---|
| 865 | if (typeof Object.freeze === "function") {
|
---|
| 866 | Object.freeze(types);
|
---|
| 867 | }
|
---|
| 868 |
|
---|
| 869 | return types;
|
---|
| 870 | }());
|
---|
| 871 |
|
---|
| 872 | const latestEcmaVersion = getLatestEcmaVersion();
|
---|
| 873 |
|
---|
| 874 | const supportedEcmaVersions = getSupportedEcmaVersions();
|
---|
| 875 |
|
---|
| 876 | exports.Syntax = Syntax;
|
---|
| 877 | exports.VisitorKeys = VisitorKeys;
|
---|
| 878 | exports.latestEcmaVersion = latestEcmaVersion;
|
---|
| 879 | exports.name = name;
|
---|
| 880 | exports.parse = parse;
|
---|
| 881 | exports.supportedEcmaVersions = supportedEcmaVersions;
|
---|
| 882 | exports.tokenize = tokenize;
|
---|
| 883 | exports.version = version;
|
---|