toAST.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. var t = require("@babel/types");
  3. var convertComments = require("./convertComments");
  4. module.exports = function(ast, traverse, code) {
  5. var state = { source: code };
  6. // Monkey patch visitor keys in order to be able to traverse the estree nodes
  7. t.VISITOR_KEYS.Property = t.VISITOR_KEYS.ObjectProperty;
  8. t.VISITOR_KEYS.MethodDefinition = [
  9. "key",
  10. "value",
  11. "decorators",
  12. "returnType",
  13. "typeParameters",
  14. ];
  15. traverse(ast, astTransformVisitor, null, state);
  16. delete t.VISITOR_KEYS.Property;
  17. delete t.VISITOR_KEYS.MethodDefinition;
  18. };
  19. var astTransformVisitor = {
  20. noScope: true,
  21. enter(path) {
  22. var node = path.node;
  23. // private var to track original node type
  24. node._babelType = node.type;
  25. if (node.innerComments) {
  26. node.trailingComments = node.innerComments;
  27. delete node.innerComments;
  28. }
  29. if (node.trailingComments) {
  30. convertComments(node.trailingComments);
  31. }
  32. if (node.leadingComments) {
  33. convertComments(node.leadingComments);
  34. }
  35. },
  36. exit(path) {
  37. var node = path.node;
  38. if (path.isJSXText()) {
  39. node.type = "Literal";
  40. }
  41. if (
  42. path.isRestElement() &&
  43. path.parent &&
  44. path.parent.type === "ObjectPattern"
  45. ) {
  46. node.type = "ExperimentalRestProperty";
  47. }
  48. if (
  49. path.isSpreadElement() &&
  50. path.parent &&
  51. path.parent.type === "ObjectExpression"
  52. ) {
  53. node.type = "ExperimentalSpreadProperty";
  54. }
  55. if (path.isTypeParameter()) {
  56. node.type = "Identifier";
  57. node.typeAnnotation = node.bound;
  58. delete node.bound;
  59. }
  60. // flow: prevent "no-undef"
  61. // for "Component" in: "let x: React.Component"
  62. if (path.isQualifiedTypeIdentifier()) {
  63. delete node.id;
  64. }
  65. // for "b" in: "var a: { b: Foo }"
  66. if (path.isObjectTypeProperty()) {
  67. delete node.key;
  68. }
  69. // for "indexer" in: "var a: {[indexer: string]: number}"
  70. if (path.isObjectTypeIndexer()) {
  71. delete node.id;
  72. }
  73. // for "param" in: "var a: { func(param: Foo): Bar };"
  74. if (path.isFunctionTypeParam()) {
  75. delete node.name;
  76. }
  77. // modules
  78. if (path.isImportDeclaration()) {
  79. delete node.isType;
  80. }
  81. // template string range fixes
  82. if (path.isTemplateLiteral()) {
  83. for (var j = 0; j < node.quasis.length; j++) {
  84. var q = node.quasis[j];
  85. q.range[0] -= 1;
  86. if (q.tail) {
  87. q.range[1] += 1;
  88. } else {
  89. q.range[1] += 2;
  90. }
  91. q.loc.start.column -= 1;
  92. if (q.tail) {
  93. q.loc.end.column += 1;
  94. } else {
  95. q.loc.end.column += 2;
  96. }
  97. }
  98. }
  99. },
  100. };