espree.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. "use strict";
  2. /* eslint-disable no-param-reassign*/
  3. const TokenTranslator = require("./token-translator");
  4. const { normalizeOptions } = require("./options");
  5. const STATE = Symbol("espree's internal state");
  6. const ESPRIMA_FINISH_NODE = Symbol("espree's esprimaFinishNode");
  7. /**
  8. * Converts an Acorn comment to a Esprima comment.
  9. * @param {boolean} block True if it's a block comment, false if not.
  10. * @param {string} text The text of the comment.
  11. * @param {int} start The index at which the comment starts.
  12. * @param {int} end The index at which the comment ends.
  13. * @param {Location} startLoc The location at which the comment starts.
  14. * @param {Location} endLoc The location at which the comment ends.
  15. * @returns {Object} The comment object.
  16. * @private
  17. */
  18. function convertAcornCommentToEsprimaComment(block, text, start, end, startLoc, endLoc) {
  19. const comment = {
  20. type: block ? "Block" : "Line",
  21. value: text
  22. };
  23. if (typeof start === "number") {
  24. comment.start = start;
  25. comment.end = end;
  26. comment.range = [start, end];
  27. }
  28. if (typeof startLoc === "object") {
  29. comment.loc = {
  30. start: startLoc,
  31. end: endLoc
  32. };
  33. }
  34. return comment;
  35. }
  36. module.exports = () => Parser => {
  37. const tokTypes = Object.assign({}, Parser.acorn.tokTypes);
  38. if (Parser.acornJsx) {
  39. Object.assign(tokTypes, Parser.acornJsx.tokTypes);
  40. }
  41. return class Espree extends Parser {
  42. constructor(opts, code) {
  43. if (typeof opts !== "object" || opts === null) {
  44. opts = {};
  45. }
  46. if (typeof code !== "string" && !(code instanceof String)) {
  47. code = String(code);
  48. }
  49. const options = normalizeOptions(opts);
  50. const ecmaFeatures = options.ecmaFeatures || {};
  51. const tokenTranslator =
  52. options.tokens === true
  53. ? new TokenTranslator(tokTypes, code)
  54. : null;
  55. // Initialize acorn parser.
  56. super({
  57. // TODO: use {...options} when spread is supported(Node.js >= 8.3.0).
  58. ecmaVersion: options.ecmaVersion,
  59. sourceType: options.sourceType,
  60. ranges: options.ranges,
  61. locations: options.locations,
  62. // Truthy value is true for backward compatibility.
  63. allowReturnOutsideFunction: Boolean(ecmaFeatures.globalReturn),
  64. // Collect tokens
  65. onToken: token => {
  66. if (tokenTranslator) {
  67. // Use `tokens`, `ecmaVersion`, and `jsxAttrValueToken` in the state.
  68. tokenTranslator.onToken(token, this[STATE]);
  69. }
  70. if (token.type !== tokTypes.eof) {
  71. this[STATE].lastToken = token;
  72. }
  73. },
  74. // Collect comments
  75. onComment: (block, text, start, end, startLoc, endLoc) => {
  76. if (this[STATE].comments) {
  77. const comment = convertAcornCommentToEsprimaComment(block, text, start, end, startLoc, endLoc);
  78. this[STATE].comments.push(comment);
  79. }
  80. }
  81. }, code);
  82. // Initialize internal state.
  83. this[STATE] = {
  84. tokens: tokenTranslator ? [] : null,
  85. comments: options.comment === true ? [] : null,
  86. impliedStrict: ecmaFeatures.impliedStrict === true && this.options.ecmaVersion >= 5,
  87. ecmaVersion: this.options.ecmaVersion,
  88. jsxAttrValueToken: false,
  89. lastToken: null
  90. };
  91. }
  92. tokenize() {
  93. do {
  94. this.next();
  95. } while (this.type !== tokTypes.eof);
  96. // Consume the final eof token
  97. this.next();
  98. const extra = this[STATE];
  99. const tokens = extra.tokens;
  100. if (extra.comments) {
  101. tokens.comments = extra.comments;
  102. }
  103. return tokens;
  104. }
  105. finishNode(...args) {
  106. const result = super.finishNode(...args);
  107. return this[ESPRIMA_FINISH_NODE](result);
  108. }
  109. finishNodeAt(...args) {
  110. const result = super.finishNodeAt(...args);
  111. return this[ESPRIMA_FINISH_NODE](result);
  112. }
  113. parse() {
  114. const extra = this[STATE];
  115. const program = super.parse();
  116. program.sourceType = this.options.sourceType;
  117. if (extra.comments) {
  118. program.comments = extra.comments;
  119. }
  120. if (extra.tokens) {
  121. program.tokens = extra.tokens;
  122. }
  123. /*
  124. * Adjust opening and closing position of program to match Esprima.
  125. * Acorn always starts programs at range 0 whereas Esprima starts at the
  126. * first AST node's start (the only real difference is when there's leading
  127. * whitespace or leading comments). Acorn also counts trailing whitespace
  128. * as part of the program whereas Esprima only counts up to the last token.
  129. */
  130. if (program.range) {
  131. program.range[0] = program.body.length ? program.body[0].range[0] : program.range[0];
  132. program.range[1] = extra.lastToken ? extra.lastToken.range[1] : program.range[1];
  133. }
  134. if (program.loc) {
  135. program.loc.start = program.body.length ? program.body[0].loc.start : program.loc.start;
  136. program.loc.end = extra.lastToken ? extra.lastToken.loc.end : program.loc.end;
  137. }
  138. return program;
  139. }
  140. parseTopLevel(node) {
  141. if (this[STATE].impliedStrict) {
  142. this.strict = true;
  143. }
  144. return super.parseTopLevel(node);
  145. }
  146. /**
  147. * Overwrites the default raise method to throw Esprima-style errors.
  148. * @param {int} pos The position of the error.
  149. * @param {string} message The error message.
  150. * @throws {SyntaxError} A syntax error.
  151. * @returns {void}
  152. */
  153. raise(pos, message) {
  154. const loc = Parser.acorn.getLineInfo(this.input, pos);
  155. const err = new SyntaxError(message);
  156. err.index = pos;
  157. err.lineNumber = loc.line;
  158. err.column = loc.column + 1; // acorn uses 0-based columns
  159. throw err;
  160. }
  161. /**
  162. * Overwrites the default raise method to throw Esprima-style errors.
  163. * @param {int} pos The position of the error.
  164. * @param {string} message The error message.
  165. * @throws {SyntaxError} A syntax error.
  166. * @returns {void}
  167. */
  168. raiseRecoverable(pos, message) {
  169. this.raise(pos, message);
  170. }
  171. /**
  172. * Overwrites the default unexpected method to throw Esprima-style errors.
  173. * @param {int} pos The position of the error.
  174. * @throws {SyntaxError} A syntax error.
  175. * @returns {void}
  176. */
  177. unexpected(pos) {
  178. let message = "Unexpected token";
  179. if (pos !== null && pos !== void 0) {
  180. this.pos = pos;
  181. if (this.options.locations) {
  182. while (this.pos < this.lineStart) {
  183. this.lineStart = this.input.lastIndexOf("\n", this.lineStart - 2) + 1;
  184. --this.curLine;
  185. }
  186. }
  187. this.nextToken();
  188. }
  189. if (this.end > this.start) {
  190. message += ` ${this.input.slice(this.start, this.end)}`;
  191. }
  192. this.raise(this.start, message);
  193. }
  194. /*
  195. * Esprima-FB represents JSX strings as tokens called "JSXText", but Acorn-JSX
  196. * uses regular tt.string without any distinction between this and regular JS
  197. * strings. As such, we intercept an attempt to read a JSX string and set a flag
  198. * on extra so that when tokens are converted, the next token will be switched
  199. * to JSXText via onToken.
  200. */
  201. jsx_readString(quote) { // eslint-disable-line camelcase
  202. const result = super.jsx_readString(quote);
  203. if (this.type === tokTypes.string) {
  204. this[STATE].jsxAttrValueToken = true;
  205. }
  206. return result;
  207. }
  208. /**
  209. * Performs last-minute Esprima-specific compatibility checks and fixes.
  210. * @param {ASTNode} result The node to check.
  211. * @returns {ASTNode} The finished node.
  212. */
  213. [ESPRIMA_FINISH_NODE](result) {
  214. // Acorn doesn't count the opening and closing backticks as part of templates
  215. // so we have to adjust ranges/locations appropriately.
  216. if (result.type === "TemplateElement") {
  217. // additional adjustment needed if ${ is the last token
  218. const terminalDollarBraceL = this.input.slice(result.end, result.end + 2) === "${";
  219. if (result.range) {
  220. result.range[0]--;
  221. result.range[1] += (terminalDollarBraceL ? 2 : 1);
  222. }
  223. if (result.loc) {
  224. result.loc.start.column--;
  225. result.loc.end.column += (terminalDollarBraceL ? 2 : 1);
  226. }
  227. }
  228. if (result.type.indexOf("Function") > -1 && !result.generator) {
  229. result.generator = false;
  230. }
  231. return result;
  232. }
  233. };
  234. };