|
| 1 | +function* it(children) { |
| 2 | + if (Array.isArray(children)) yield* children; |
| 3 | + else yield children; |
| 4 | +} |
| 5 | + |
| 6 | +function traverse(node, visitorKeys, visitor) { |
| 7 | + const { type } = node; |
| 8 | + if (!type) return; |
| 9 | + const keys = visitorKeys[type]; |
| 10 | + if (!keys) return; |
| 11 | + |
| 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 | + |
| 23 | +const convertNodesVisitor = { |
| 24 | + enter(node) { |
| 25 | + if (node.innerComments) { |
| 26 | + delete node.innerComments; |
| 27 | + } |
| 28 | + |
| 29 | + if (node.trailingComments) { |
| 30 | + delete node.trailingComments; |
| 31 | + } |
| 32 | + |
| 33 | + if (node.leadingComments) { |
| 34 | + delete node.leadingComments; |
| 35 | + } |
| 36 | + }, |
| 37 | + exit(node) { |
| 38 | + // Used internally by @babel/parser. |
| 39 | + if (node.extra) { |
| 40 | + delete node.extra; |
| 41 | + } |
| 42 | + |
| 43 | + if (node?.loc.identifierName) { |
| 44 | + delete node.loc.identifierName; |
| 45 | + } |
| 46 | + |
| 47 | + if (node.type === "TypeParameter") { |
| 48 | + node.type = "Identifier"; |
| 49 | + node.typeAnnotation = node.bound; |
| 50 | + delete node.bound; |
| 51 | + } |
| 52 | + |
| 53 | + // flow: prevent "no-undef" |
| 54 | + // for "Component" in: "let x: React.Component" |
| 55 | + if (node.type === "QualifiedTypeIdentifier") { |
| 56 | + delete node.id; |
| 57 | + } |
| 58 | + // for "b" in: "var a: { b: Foo }" |
| 59 | + if (node.type === "ObjectTypeProperty") { |
| 60 | + delete node.key; |
| 61 | + } |
| 62 | + // for "indexer" in: "var a: {[indexer: string]: number}" |
| 63 | + if (node.type === "ObjectTypeIndexer") { |
| 64 | + delete node.id; |
| 65 | + } |
| 66 | + // for "param" in: "var a: { func(param: Foo): Bar };" |
| 67 | + if (node.type === "FunctionTypeParam") { |
| 68 | + delete node.name; |
| 69 | + } |
| 70 | + |
| 71 | + // modules |
| 72 | + if (node.type === "ImportDeclaration") { |
| 73 | + delete node.isType; |
| 74 | + } |
| 75 | + |
| 76 | + // template string range fixes |
| 77 | + if (node.type === "TemplateLiteral") { |
| 78 | + for (let i = 0; i < node.quasis.length; i++) { |
| 79 | + const q = node.quasis[i]; |
| 80 | + q.range[0] -= 1; |
| 81 | + if (q.tail) { |
| 82 | + q.range[1] += 1; |
| 83 | + } else { |
| 84 | + q.range[1] += 2; |
| 85 | + } |
| 86 | + q.loc.start.column -= 1; |
| 87 | + if (q.tail) { |
| 88 | + q.loc.end.column += 1; |
| 89 | + } else { |
| 90 | + q.loc.end.column += 2; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + }, |
| 95 | +}; |
| 96 | + |
| 97 | +function convertNodes(ast, visitorKeys) { |
| 98 | + traverse(ast, visitorKeys, convertNodesVisitor); |
| 99 | +} |
| 100 | + |
| 101 | +function convertProgramNode(ast) { |
| 102 | + ast.type = "Program"; |
| 103 | + ast.sourceType = ast.program.sourceType; |
| 104 | + ast.body = ast.program.body; |
| 105 | + delete ast.program; |
| 106 | + delete ast.errors; |
| 107 | + |
| 108 | + if (ast.comments.length) { |
| 109 | + const lastComment = ast.comments[ast.comments.length - 1]; |
| 110 | + |
| 111 | + if (ast.tokens.length) { |
| 112 | + const lastToken = ast.tokens[ast.tokens.length - 1]; |
| 113 | + |
| 114 | + if (lastComment.end > lastToken.end) { |
| 115 | + // If there is a comment after the last token, the program ends at the |
| 116 | + // last token and not the comment |
| 117 | + ast.range[1] = lastToken.end; |
| 118 | + ast.loc.end.line = lastToken.loc.end.line; |
| 119 | + ast.loc.end.column = lastToken.loc.end.column; |
| 120 | + } |
| 121 | + } |
| 122 | + } else { |
| 123 | + if (!ast.tokens.length) { |
| 124 | + ast.loc.start.line = 1; |
| 125 | + ast.loc.end.line = 1; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if (ast.body && ast.body.length > 0) { |
| 130 | + ast.loc.start.line = ast.body[0].loc.start.line; |
| 131 | + ast.range[0] = ast.body[0].start; |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +module.exports = function convertAST(ast, visitorKeys) { |
| 136 | + convertNodes(ast, visitorKeys); |
| 137 | + convertProgramNode(ast); |
| 138 | +}; |
0 commit comments