convertAST.cjs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. if (node.loc.identifierName) {
  40. delete node.loc.identifierName;
  41. }
  42. if (node.type === "TypeParameter") {
  43. node.type = "Identifier";
  44. node.typeAnnotation = node.bound;
  45. delete node.bound;
  46. }
  47. if (node.type === "QualifiedTypeIdentifier") {
  48. delete node.id;
  49. }
  50. if (node.type === "ObjectTypeProperty") {
  51. delete node.key;
  52. }
  53. if (node.type === "ObjectTypeIndexer") {
  54. delete node.id;
  55. }
  56. if (node.type === "FunctionTypeParam") {
  57. delete node.name;
  58. }
  59. if (node.type === "ImportDeclaration") {
  60. delete node.isType;
  61. }
  62. if (node.type === "TemplateLiteral") {
  63. for (let i = 0; i < node.quasis.length; i++) {
  64. const q = node.quasis[i];
  65. q.range[0] -= 1;
  66. if (q.tail) {
  67. q.range[1] += 1;
  68. } else {
  69. q.range[1] += 2;
  70. }
  71. q.loc.start.column -= 1;
  72. if (q.tail) {
  73. q.loc.end.column += 1;
  74. } else {
  75. q.loc.end.column += 2;
  76. }
  77. if (ESLINT_VERSION >= 8) {
  78. q.start -= 1;
  79. if (q.tail) {
  80. q.end += 1;
  81. } else {
  82. q.end += 2;
  83. }
  84. }
  85. }
  86. }
  87. }
  88. };
  89. function convertNodes(ast, visitorKeys) {
  90. traverse(ast, visitorKeys, convertNodesVisitor);
  91. }
  92. function convertProgramNode(ast) {
  93. const body = ast.program.body;
  94. Object.assign(ast, {
  95. type: "Program",
  96. sourceType: ast.program.sourceType,
  97. body
  98. });
  99. delete ast.program;
  100. delete ast.errors;
  101. if (ast.comments.length) {
  102. const lastComment = ast.comments[ast.comments.length - 1];
  103. if (ast.tokens.length) {
  104. const lastToken = ast.tokens[ast.tokens.length - 1];
  105. if (lastComment.end > lastToken.end) {
  106. ast.range[1] = lastToken.end;
  107. ast.loc.end.line = lastToken.loc.end.line;
  108. ast.loc.end.column = lastToken.loc.end.column;
  109. if (ESLINT_VERSION >= 8) {
  110. ast.end = lastToken.end;
  111. }
  112. }
  113. }
  114. } else {
  115. if (!ast.tokens.length) {
  116. ast.loc.start.line = 1;
  117. ast.loc.end.line = 1;
  118. }
  119. }
  120. if (body != null && body.length) {
  121. ast.loc.start.line = body[0].loc.start.line;
  122. ast.range[0] = body[0].start;
  123. if (ESLINT_VERSION >= 8) {
  124. ast.start = body[0].start;
  125. }
  126. }
  127. }
  128. module.exports = function convertAST(ast, visitorKeys) {
  129. convertNodes(ast, visitorKeys);
  130. convertProgramNode(ast);
  131. };
  132. //# sourceMappingURL=convertAST.cjs.map