espree.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * @fileoverview Main Espree file that converts Acorn into Esprima output.
  3. *
  4. * This file contains code from the following MIT-licensed projects:
  5. * 1. Acorn
  6. * 2. Babylon
  7. * 3. Babel-ESLint
  8. *
  9. * This file also contains code from Esprima, which is BSD licensed.
  10. *
  11. * Acorn is Copyright 2012-2015 Acorn Contributors (https://github.com/marijnh/acorn/blob/master/AUTHORS)
  12. * Babylon is Copyright 2014-2015 various contributors (https://github.com/babel/babel/blob/master/packages/babylon/AUTHORS)
  13. * Babel-ESLint is Copyright 2014-2015 Sebastian McKenzie <sebmck@gmail.com>
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions are met:
  17. *
  18. * * Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. * * Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in the
  22. * documentation and/or other materials provided with the distribution.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  28. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  29. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  30. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  31. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  33. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. * Esprima is Copyright (c) jQuery Foundation, Inc. and Contributors, All Rights Reserved.
  36. *
  37. * Redistribution and use in source and binary forms, with or without
  38. * modification, are permitted provided that the following conditions are met:
  39. *
  40. * * Redistributions of source code must retain the above copyright
  41. * notice, this list of conditions and the following disclaimer.
  42. * * Redistributions in binary form must reproduce the above copyright
  43. * notice, this list of conditions and the following disclaimer in the
  44. * documentation and/or other materials provided with the distribution.
  45. *
  46. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  47. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  48. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  49. * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  50. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  51. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  52. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  53. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  54. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  55. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56. */
  57. import * as acorn from "acorn";
  58. import jsx from "acorn-jsx";
  59. import espree from "./lib/espree.js";
  60. import espreeVersion from "./lib/version.js";
  61. import * as visitorKeys from "eslint-visitor-keys";
  62. import { getLatestEcmaVersion, getSupportedEcmaVersions } from "./lib/options.js";
  63. // To initialize lazily.
  64. const parsers = {
  65. _regular: null,
  66. _jsx: null,
  67. get regular() {
  68. if (this._regular === null) {
  69. this._regular = acorn.Parser.extend(espree());
  70. }
  71. return this._regular;
  72. },
  73. get jsx() {
  74. if (this._jsx === null) {
  75. this._jsx = acorn.Parser.extend(jsx(), espree());
  76. }
  77. return this._jsx;
  78. },
  79. get(options) {
  80. const useJsx = Boolean(
  81. options &&
  82. options.ecmaFeatures &&
  83. options.ecmaFeatures.jsx
  84. );
  85. return useJsx ? this.jsx : this.regular;
  86. }
  87. };
  88. //------------------------------------------------------------------------------
  89. // Tokenizer
  90. //------------------------------------------------------------------------------
  91. /**
  92. * Tokenizes the given code.
  93. * @param {string} code The code to tokenize.
  94. * @param {Object} options Options defining how to tokenize.
  95. * @returns {Token[]} An array of tokens.
  96. * @throws {SyntaxError} If the input code is invalid.
  97. * @private
  98. */
  99. export function tokenize(code, options) {
  100. const Parser = parsers.get(options);
  101. // Ensure to collect tokens.
  102. if (!options || options.tokens !== true) {
  103. options = Object.assign({}, options, { tokens: true }); // eslint-disable-line no-param-reassign -- stylistic choice
  104. }
  105. return new Parser(options, code).tokenize();
  106. }
  107. //------------------------------------------------------------------------------
  108. // Parser
  109. //------------------------------------------------------------------------------
  110. /**
  111. * Parses the given code.
  112. * @param {string} code The code to tokenize.
  113. * @param {Object} options Options defining how to tokenize.
  114. * @returns {ASTNode} The "Program" AST node.
  115. * @throws {SyntaxError} If the input code is invalid.
  116. */
  117. export function parse(code, options) {
  118. const Parser = parsers.get(options);
  119. return new Parser(options, code).parse();
  120. }
  121. //------------------------------------------------------------------------------
  122. // Public
  123. //------------------------------------------------------------------------------
  124. export const version = espreeVersion;
  125. export const name = "espree";
  126. /* istanbul ignore next */
  127. export const VisitorKeys = (function() {
  128. return visitorKeys.KEYS;
  129. }());
  130. // Derive node types from VisitorKeys
  131. /* istanbul ignore next */
  132. export const Syntax = (function() {
  133. let key,
  134. types = {};
  135. if (typeof Object.create === "function") {
  136. types = Object.create(null);
  137. }
  138. for (key in VisitorKeys) {
  139. if (Object.hasOwn(VisitorKeys, key)) {
  140. types[key] = key;
  141. }
  142. }
  143. if (typeof Object.freeze === "function") {
  144. Object.freeze(types);
  145. }
  146. return types;
  147. }());
  148. export const latestEcmaVersion = getLatestEcmaVersion();
  149. export const supportedEcmaVersions = getSupportedEcmaVersions();