convertAST.cjs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. const ESLINT_VERSION = require("../utils/eslint-version.cjs");
  2. function* it(children) {
  3. if (Array.isArray(children)) yield* children;else yield children;
  4. }
  5. function traverse(node, visitorKeys, visitor) {
  6. const {
  7. type
  8. } = node;
  9. if (!type) return;
  10. const keys = visitorKeys[type];
  11. if (!keys) return;
  12. for (const key of keys) {
  13. for (const child of it(node[key])) {
  14. if (child && typeof child === "object") {
  15. visitor.enter(child);
  16. traverse(child, visitorKeys, visitor);
  17. visitor.exit(child);
  18. }
  19. }
  20. }
  21. }
  22. const convertNodesVisitor = {
  23. enter(node) {
  24. if (node.innerComments) {
  25. delete node.innerComments;
  26. }
  27. if (node.trailingComments) {
  28. delete node.trailingComments;
  29. }
  30. if (node.leadingComments) {
  31. delete node.leadingComments;
  32. }
  33. },
  34. exit(node) {
  35. if (node.extra) {
  36. delete node.extra;
  37. }
  38. if (node.loc.identifierName) {
  39. delete node.loc.identifierName;
  40. }
  41. if (node.type === "TypeParameter") {
  42. node.type = "Identifier";
  43. node.typeAnnotation = node.bound;
  44. delete node.bound;
  45. }
  46. if (node.type === "QualifiedTypeIdentifier") {
  47. delete node.id;
  48. }
  49. if (node.type === "ObjectTypeProperty") {
  50. delete node.key;
  51. }
  52. if (node.type === "ObjectTypeIndexer") {
  53. delete node.id;
  54. }
  55. if (node.type === "FunctionTypeParam") {
  56. delete node.name;
  57. }
  58. if (node.type === "ImportDeclaration") {
  59. delete node.isType;
  60. }
  61. if (node.type === "TemplateLiteral") {
  62. for (let i = 0; i < node.quasis.length; i++) {
  63. const q = node.quasis[i];
  64. q.range[0] -= 1;
  65. if (q.tail) {
  66. q.range[1] += 1;
  67. } else {
  68. q.range[1] += 2;
  69. }
  70. q.loc.start.column -= 1;
  71. if (q.tail) {
  72. q.loc.end.column += 1;
  73. } else {
  74. q.loc.end.column += 2;
  75. }
  76. if (ESLINT_VERSION >= 8) {
  77. q.start -= 1;
  78. if (q.tail) {
  79. q.end += 1;
  80. } else {
  81. q.end += 2;
  82. }
  83. }
  84. }
  85. }
  86. }
  87. };
  88. function convertNodes(ast, visitorKeys) {
  89. traverse(ast, visitorKeys, convertNodesVisitor);
  90. }
  91. function convertProgramNode(ast) {
  92. ast.type = "Program";
  93. ast.sourceType = ast.program.sourceType;
  94. ast.body = ast.program.body;
  95. delete ast.program;
  96. delete ast.errors;
  97. if (ast.comments.length) {
  98. const lastComment = ast.comments[ast.comments.length - 1];
  99. if (ast.tokens.length) {
  100. const lastToken = ast.tokens[ast.tokens.length - 1];
  101. if (lastComment.end > lastToken.end) {
  102. ast.range[1] = lastToken.end;
  103. ast.loc.end.line = lastToken.loc.end.line;
  104. ast.loc.end.column = lastToken.loc.end.column;
  105. if (ESLINT_VERSION >= 8) {
  106. ast.end = lastToken.end;
  107. }
  108. }
  109. }
  110. } else {
  111. if (!ast.tokens.length) {
  112. ast.loc.start.line = 1;
  113. ast.loc.end.line = 1;
  114. }
  115. }
  116. if (ast.body && ast.body.length > 0) {
  117. ast.loc.start.line = ast.body[0].loc.start.line;
  118. ast.range[0] = ast.body[0].start;
  119. if (ESLINT_VERSION >= 8) {
  120. ast.start = ast.body[0].start;
  121. }
  122. }
  123. }
  124. module.exports = function convertAST(ast, visitorKeys) {
  125. convertNodes(ast, visitorKeys);
  126. convertProgramNode(ast);
  127. };
  128. //# sourceMappingURL=convertAST.cjs.map