convertAST.cjs 3.3 KB

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