convertTokens.cjs 5.6 KB

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