convertTokens.cjs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. "use strict";
  2. const ESLINT_VERSION = require("../utils/eslint-version.cjs");
  3. function convertTemplateType(tokens, tl) {
  4. let curlyBrace = null;
  5. let templateTokens = [];
  6. const result = [];
  7. function addTemplateType() {
  8. const start = templateTokens[0];
  9. const end = templateTokens[templateTokens.length - 1];
  10. const value = templateTokens.reduce((result, token) => {
  11. if (token.value) {
  12. result += token.value;
  13. } else if (token.type.label !== tl.template) {
  14. result += token.type.label;
  15. }
  16. return result;
  17. }, "");
  18. result.push({
  19. type: "Template",
  20. value: value,
  21. start: start.start,
  22. end: end.end,
  23. loc: {
  24. start: start.loc.start,
  25. end: end.loc.end
  26. }
  27. });
  28. templateTokens = [];
  29. }
  30. tokens.forEach(token => {
  31. switch (token.type.label) {
  32. case tl.backQuote:
  33. if (curlyBrace) {
  34. result.push(curlyBrace);
  35. curlyBrace = null;
  36. }
  37. templateTokens.push(token);
  38. if (templateTokens.length > 1) {
  39. addTemplateType();
  40. }
  41. break;
  42. case tl.dollarBraceL:
  43. templateTokens.push(token);
  44. addTemplateType();
  45. break;
  46. case tl.braceR:
  47. if (curlyBrace) {
  48. result.push(curlyBrace);
  49. }
  50. curlyBrace = token;
  51. break;
  52. case tl.template:
  53. if (curlyBrace) {
  54. templateTokens.push(curlyBrace);
  55. curlyBrace = null;
  56. }
  57. templateTokens.push(token);
  58. break;
  59. default:
  60. if (curlyBrace) {
  61. result.push(curlyBrace);
  62. curlyBrace = null;
  63. }
  64. result.push(token);
  65. }
  66. });
  67. return result;
  68. }
  69. function convertToken(token, source, tl) {
  70. const {
  71. type
  72. } = token;
  73. const {
  74. label
  75. } = type;
  76. const newToken = token;
  77. newToken.range = [token.start, token.end];
  78. if (label === tl.name) {
  79. const tokenValue = token.value;
  80. if (tokenValue === "let" || tokenValue === "static" || tokenValue === "yield") {
  81. newToken.type = "Keyword";
  82. } else {
  83. newToken.type = "Identifier";
  84. }
  85. } else if (label === tl.semi || label === tl.comma || label === tl.parenL || label === tl.parenR || label === tl.braceL || label === tl.braceR || label === tl.slash || label === tl.dot || label === tl.bracketL || label === tl.bracketR || label === tl.ellipsis || label === tl.arrow || label === tl.pipeline || label === tl.star || label === tl.incDec || label === tl.colon || label === tl.question || label === tl.template || label === tl.backQuote || label === tl.dollarBraceL || label === tl.at || label === tl.logicalOR || label === tl.logicalAND || label === tl.nullishCoalescing || label === tl.bitwiseOR || label === tl.bitwiseXOR || label === tl.bitwiseAND || label === tl.equality || label === tl.relational || label === tl.bitShift || label === tl.plusMin || label === tl.modulo || label === tl.exponent || label === tl.bang || label === tl.tilde || label === tl.doubleColon || label === tl.hash || label === tl.questionDot || label === tl.braceHashL || label === tl.braceBarL || label === tl.braceBarR || label === tl.bracketHashL || label === tl.bracketBarL || label === tl.bracketBarR || label === tl.doubleCaret || label === tl.doubleAt || type.isAssign) {
  86. var _newToken$value;
  87. newToken.type = "Punctuator";
  88. (_newToken$value = newToken.value) != null ? _newToken$value : newToken.value = label;
  89. } else if (label === tl.jsxTagStart) {
  90. newToken.type = "Punctuator";
  91. newToken.value = "<";
  92. } else if (label === tl.jsxTagEnd) {
  93. newToken.type = "Punctuator";
  94. newToken.value = ">";
  95. } else if (label === tl.jsxName) {
  96. newToken.type = "JSXIdentifier";
  97. } else if (label === tl.jsxText) {
  98. newToken.type = "JSXText";
  99. } else if (type.keyword === "null") {
  100. newToken.type = "Null";
  101. } else if (type.keyword === "false" || type.keyword === "true") {
  102. newToken.type = "Boolean";
  103. } else if (type.keyword) {
  104. newToken.type = "Keyword";
  105. } else if (label === tl.num) {
  106. newToken.type = "Numeric";
  107. newToken.value = source.slice(token.start, token.end);
  108. } else if (label === tl.string) {
  109. newToken.type = "String";
  110. newToken.value = source.slice(token.start, token.end);
  111. } else if (label === tl.regexp) {
  112. newToken.type = "RegularExpression";
  113. const value = token.value;
  114. newToken.regex = {
  115. pattern: value.pattern,
  116. flags: value.flags
  117. };
  118. newToken.value = `/${value.pattern}/${value.flags}`;
  119. } else if (label === tl.bigint) {
  120. newToken.type = "Numeric";
  121. newToken.value = `${token.value}n`;
  122. } else if (label === tl.privateName) {
  123. newToken.type = "PrivateIdentifier";
  124. } else if (label === tl.templateNonTail || label === tl.templateTail || label === tl.Template) {
  125. newToken.type = "Template";
  126. }
  127. ;
  128. return newToken;
  129. }
  130. module.exports = function convertTokens(tokens, code, tokLabels) {
  131. const result = [];
  132. const templateTypeMergedTokens = convertTemplateType(tokens, tokLabels);
  133. for (let i = 0, {
  134. length
  135. } = templateTypeMergedTokens; i < length - 1; i++) {
  136. const token = templateTypeMergedTokens[i];
  137. const tokenType = token.type;
  138. if (tokenType === "CommentLine" || tokenType === "CommentBlock") {
  139. continue;
  140. }
  141. {
  142. if (ESLINT_VERSION >= 8 && i + 1 < length && tokenType.label === tokLabels.hash) {
  143. const nextToken = templateTypeMergedTokens[i + 1];
  144. if (nextToken.type.label === tokLabels.name && token.end === nextToken.start) {
  145. i++;
  146. nextToken.type = "PrivateIdentifier";
  147. nextToken.start -= 1;
  148. nextToken.loc.start.column -= 1;
  149. nextToken.range = [nextToken.start, nextToken.end];
  150. result.push(nextToken);
  151. continue;
  152. }
  153. }
  154. }
  155. result.push(convertToken(token, code, tokLabels));
  156. }
  157. return result;
  158. };
  159. //# sourceMappingURL=convertTokens.cjs.map