token-translator.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. * @fileoverview Translates tokens between Acorn format and Esprima format.
  3. * @author Nicholas C. Zakas
  4. */
  5. /* eslint no-underscore-dangle: 0 */
  6. "use strict";
  7. //------------------------------------------------------------------------------
  8. // Requirements
  9. //------------------------------------------------------------------------------
  10. // none!
  11. //------------------------------------------------------------------------------
  12. // Private
  13. //------------------------------------------------------------------------------
  14. // Esprima Token Types
  15. const Token = {
  16. Boolean: "Boolean",
  17. EOF: "<end>",
  18. Identifier: "Identifier",
  19. Keyword: "Keyword",
  20. Null: "Null",
  21. Numeric: "Numeric",
  22. Punctuator: "Punctuator",
  23. String: "String",
  24. RegularExpression: "RegularExpression",
  25. Template: "Template",
  26. JSXIdentifier: "JSXIdentifier",
  27. JSXText: "JSXText"
  28. };
  29. /**
  30. * Converts part of a template into an Esprima token.
  31. * @param {AcornToken[]} tokens The Acorn tokens representing the template.
  32. * @param {string} code The source code.
  33. * @returns {EsprimaToken} The Esprima equivalent of the template token.
  34. * @private
  35. */
  36. function convertTemplatePart(tokens, code) {
  37. const firstToken = tokens[0],
  38. lastTemplateToken = tokens[tokens.length - 1];
  39. const token = {
  40. type: Token.Template,
  41. value: code.slice(firstToken.start, lastTemplateToken.end)
  42. };
  43. if (firstToken.loc) {
  44. token.loc = {
  45. start: firstToken.loc.start,
  46. end: lastTemplateToken.loc.end
  47. };
  48. }
  49. if (firstToken.range) {
  50. token.start = firstToken.range[0];
  51. token.end = lastTemplateToken.range[1];
  52. token.range = [token.start, token.end];
  53. }
  54. return token;
  55. }
  56. /**
  57. * Contains logic to translate Acorn tokens into Esprima tokens.
  58. * @param {Object} acornTokTypes The Acorn token types.
  59. * @param {string} code The source code Acorn is parsing. This is necessary
  60. * to correct the "value" property of some tokens.
  61. * @constructor
  62. */
  63. function TokenTranslator(acornTokTypes, code) {
  64. // token types
  65. this._acornTokTypes = acornTokTypes;
  66. // token buffer for templates
  67. this._tokens = [];
  68. // track the last curly brace
  69. this._curlyBrace = null;
  70. // the source code
  71. this._code = code;
  72. }
  73. TokenTranslator.prototype = {
  74. constructor: TokenTranslator,
  75. /**
  76. * Translates a single Esprima token to a single Acorn token. This may be
  77. * inaccurate due to how templates are handled differently in Esprima and
  78. * Acorn, but should be accurate for all other tokens.
  79. * @param {AcornToken} token The Acorn token to translate.
  80. * @param {Object} extra Espree extra object.
  81. * @returns {EsprimaToken} The Esprima version of the token.
  82. */
  83. translate(token, extra) {
  84. const type = token.type,
  85. tt = this._acornTokTypes;
  86. if (type === tt.name) {
  87. token.type = Token.Identifier;
  88. // TODO: See if this is an Acorn bug
  89. if (token.value === "static") {
  90. token.type = Token.Keyword;
  91. }
  92. if (extra.ecmaVersion > 5 && (token.value === "yield" || token.value === "let")) {
  93. token.type = Token.Keyword;
  94. }
  95. } else if (type === tt.semi || type === tt.comma ||
  96. type === tt.parenL || type === tt.parenR ||
  97. type === tt.braceL || type === tt.braceR ||
  98. type === tt.dot || type === tt.bracketL ||
  99. type === tt.colon || type === tt.question ||
  100. type === tt.bracketR || type === tt.ellipsis ||
  101. type === tt.arrow || type === tt.jsxTagStart ||
  102. type === tt.incDec || type === tt.starstar ||
  103. type === tt.jsxTagEnd || type === tt.prefix ||
  104. type === tt.questionDot ||
  105. (type.binop && !type.keyword) ||
  106. type.isAssign) {
  107. token.type = Token.Punctuator;
  108. token.value = this._code.slice(token.start, token.end);
  109. } else if (type === tt.jsxName) {
  110. token.type = Token.JSXIdentifier;
  111. } else if (type.label === "jsxText" || type === tt.jsxAttrValueToken) {
  112. token.type = Token.JSXText;
  113. } else if (type.keyword) {
  114. if (type.keyword === "true" || type.keyword === "false") {
  115. token.type = Token.Boolean;
  116. } else if (type.keyword === "null") {
  117. token.type = Token.Null;
  118. } else {
  119. token.type = Token.Keyword;
  120. }
  121. } else if (type === tt.num) {
  122. token.type = Token.Numeric;
  123. token.value = this._code.slice(token.start, token.end);
  124. } else if (type === tt.string) {
  125. if (extra.jsxAttrValueToken) {
  126. extra.jsxAttrValueToken = false;
  127. token.type = Token.JSXText;
  128. } else {
  129. token.type = Token.String;
  130. }
  131. token.value = this._code.slice(token.start, token.end);
  132. } else if (type === tt.regexp) {
  133. token.type = Token.RegularExpression;
  134. const value = token.value;
  135. token.regex = {
  136. flags: value.flags,
  137. pattern: value.pattern
  138. };
  139. token.value = `/${value.pattern}/${value.flags}`;
  140. }
  141. return token;
  142. },
  143. /**
  144. * Function to call during Acorn's onToken handler.
  145. * @param {AcornToken} token The Acorn token.
  146. * @param {Object} extra The Espree extra object.
  147. * @returns {void}
  148. */
  149. onToken(token, extra) {
  150. const that = this,
  151. tt = this._acornTokTypes,
  152. tokens = extra.tokens,
  153. templateTokens = this._tokens;
  154. /**
  155. * Flushes the buffered template tokens and resets the template
  156. * tracking.
  157. * @returns {void}
  158. * @private
  159. */
  160. function translateTemplateTokens() {
  161. tokens.push(convertTemplatePart(that._tokens, that._code));
  162. that._tokens = [];
  163. }
  164. if (token.type === tt.eof) {
  165. // might be one last curlyBrace
  166. if (this._curlyBrace) {
  167. tokens.push(this.translate(this._curlyBrace, extra));
  168. }
  169. return;
  170. }
  171. if (token.type === tt.backQuote) {
  172. // if there's already a curly, it's not part of the template
  173. if (this._curlyBrace) {
  174. tokens.push(this.translate(this._curlyBrace, extra));
  175. this._curlyBrace = null;
  176. }
  177. templateTokens.push(token);
  178. // it's the end
  179. if (templateTokens.length > 1) {
  180. translateTemplateTokens();
  181. }
  182. return;
  183. }
  184. if (token.type === tt.dollarBraceL) {
  185. templateTokens.push(token);
  186. translateTemplateTokens();
  187. return;
  188. }
  189. if (token.type === tt.braceR) {
  190. // if there's already a curly, it's not part of the template
  191. if (this._curlyBrace) {
  192. tokens.push(this.translate(this._curlyBrace, extra));
  193. }
  194. // store new curly for later
  195. this._curlyBrace = token;
  196. return;
  197. }
  198. if (token.type === tt.template || token.type === tt.invalidTemplate) {
  199. if (this._curlyBrace) {
  200. templateTokens.push(this._curlyBrace);
  201. this._curlyBrace = null;
  202. }
  203. templateTokens.push(token);
  204. return;
  205. }
  206. if (this._curlyBrace) {
  207. tokens.push(this.translate(this._curlyBrace, extra));
  208. this._curlyBrace = null;
  209. }
  210. tokens.push(this.translate(token, extra));
  211. }
  212. };
  213. //------------------------------------------------------------------------------
  214. // Public
  215. //------------------------------------------------------------------------------
  216. module.exports = TokenTranslator;