toToken.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. "use strict";
  2. module.exports = function(token, tt, source) {
  3. var type = token.type;
  4. token.range = [token.start, token.end];
  5. if (type === tt.name) {
  6. token.type = "Identifier";
  7. } else if (
  8. type === tt.semi ||
  9. type === tt.comma ||
  10. type === tt.parenL ||
  11. type === tt.parenR ||
  12. type === tt.braceL ||
  13. type === tt.braceR ||
  14. type === tt.slash ||
  15. type === tt.dot ||
  16. type === tt.bracketL ||
  17. type === tt.bracketR ||
  18. type === tt.ellipsis ||
  19. type === tt.arrow ||
  20. type === tt.pipeline ||
  21. type === tt.star ||
  22. type === tt.incDec ||
  23. type === tt.colon ||
  24. type === tt.question ||
  25. type === tt.questionDot ||
  26. type === tt.template ||
  27. type === tt.backQuote ||
  28. type === tt.dollarBraceL ||
  29. type === tt.at ||
  30. type === tt.logicalOR ||
  31. type === tt.logicalAND ||
  32. type === tt.nullishCoalescing ||
  33. type === tt.bitwiseOR ||
  34. type === tt.bitwiseXOR ||
  35. type === tt.bitwiseAND ||
  36. type === tt.equality ||
  37. type === tt.relational ||
  38. type === tt.bitShift ||
  39. type === tt.plusMin ||
  40. type === tt.modulo ||
  41. type === tt.exponent ||
  42. type === tt.bang ||
  43. type === tt.tilde ||
  44. type === tt.doubleColon ||
  45. type.isAssign
  46. ) {
  47. token.type = "Punctuator";
  48. if (!token.value) token.value = type.label;
  49. } else if (type === tt.jsxTagStart) {
  50. token.type = "Punctuator";
  51. token.value = "<";
  52. } else if (type === tt.jsxTagEnd) {
  53. token.type = "Punctuator";
  54. token.value = ">";
  55. } else if (type === tt.jsxName) {
  56. token.type = "JSXIdentifier";
  57. } else if (type === tt.jsxText) {
  58. token.type = "JSXText";
  59. } else if (type.keyword === "null") {
  60. token.type = "Null";
  61. } else if (type.keyword === "false" || type.keyword === "true") {
  62. token.type = "Boolean";
  63. } else if (type.keyword) {
  64. token.type = "Keyword";
  65. } else if (type === tt.num) {
  66. token.type = "Numeric";
  67. token.value = source.slice(token.start, token.end);
  68. } else if (type === tt.string) {
  69. token.type = "String";
  70. token.value = source.slice(token.start, token.end);
  71. } else if (type === tt.regexp) {
  72. token.type = "RegularExpression";
  73. var value = token.value;
  74. token.regex = {
  75. pattern: value.pattern,
  76. flags: value.flags,
  77. };
  78. token.value = `/${value.pattern}/${value.flags}`;
  79. }
  80. return token;
  81. };