forked from babel/babel
-
Notifications
You must be signed in to change notification settings - Fork 3
/
convertAST.js
146 lines (125 loc) 路 3.89 KB
/
convertAST.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { types as t, traverse } from "@babel/core";
import VISITOR_KEYS from "../visitor-keys";
function convertNodes(ast, code) {
const astTransformVisitor = {
noScope: true,
enter(path) {
const { node } = path;
if (node.innerComments) {
delete node.innerComments;
}
if (node.trailingComments) {
delete node.trailingComments;
}
if (node.leadingComments) {
delete node.leadingComments;
}
},
exit(path) {
const { node } = path;
// Used internally by @babel/parser.
if (node.extra) {
delete node.extra;
}
if (node?.loc.identifierName) {
delete node.loc.identifierName;
}
if (path.isTypeParameter()) {
node.type = "Identifier";
node.typeAnnotation = node.bound;
delete node.bound;
}
// flow: prevent "no-undef"
// for "Component" in: "let x: React.Component"
if (path.isQualifiedTypeIdentifier()) {
delete node.id;
}
// for "b" in: "var a: { b: Foo }"
if (path.isObjectTypeProperty()) {
delete node.key;
}
// for "indexer" in: "var a: {[indexer: string]: number}"
if (path.isObjectTypeIndexer()) {
delete node.id;
}
// for "param" in: "var a: { func(param: Foo): Bar };"
if (path.isFunctionTypeParam()) {
delete node.name;
}
// modules
if (path.isImportDeclaration()) {
delete node.isType;
}
// template string range fixes
if (path.isTemplateLiteral()) {
for (let i = 0; i < node.quasis.length; i++) {
const q = node.quasis[i];
q.range[0] -= 1;
if (q.tail) {
q.range[1] += 1;
} else {
q.range[1] += 2;
}
q.loc.start.column -= 1;
if (q.tail) {
q.loc.end.column += 1;
} else {
q.loc.end.column += 2;
}
}
}
},
};
const state = { source: code };
const oldExportAllDeclarationKeys = t.VISITOR_KEYS.ExportAllDeclaration;
try {
// Monkey patch visitor keys in order to be able to traverse the estree nodes
t.VISITOR_KEYS.ChainExpression = VISITOR_KEYS.ChainExpression;
t.VISITOR_KEYS.Property = VISITOR_KEYS.Property;
t.VISITOR_KEYS.MethodDefinition = VISITOR_KEYS.MethodDefinition;
// Make sure we visit `exported` key to remove `identifierName` from loc node
t.VISITOR_KEYS.ExportAllDeclaration = t.VISITOR_KEYS.ExportAllDeclaration.concat(
"exported",
);
traverse(ast, astTransformVisitor, null, state);
} finally {
// These can be safely deleted because they are not defined in the original visitor keys.
delete t.VISITOR_KEYS.ChainExpression;
delete t.VISITOR_KEYS.MethodDefinition;
delete t.VISITOR_KEYS.Property;
t.VISITOR_KEYS.ExportAllDeclaration = oldExportAllDeclarationKeys;
}
}
function convertProgramNode(ast) {
ast.type = "Program";
ast.sourceType = ast.program.sourceType;
ast.body = ast.program.body;
delete ast.program;
delete ast.errors;
if (ast.comments.length) {
const lastComment = ast.comments[ast.comments.length - 1];
if (ast.tokens.length) {
const lastToken = ast.tokens[ast.tokens.length - 1];
if (lastComment.end > lastToken.end) {
// If there is a comment after the last token, the program ends at the
// last token and not the comment
ast.range[1] = lastToken.end;
ast.loc.end.line = lastToken.loc.end.line;
ast.loc.end.column = lastToken.loc.end.column;
}
}
} else {
if (!ast.tokens.length) {
ast.loc.start.line = 1;
ast.loc.end.line = 1;
}
}
if (ast.body && ast.body.length > 0) {
ast.loc.start.line = ast.body[0].loc.start.line;
ast.range[0] = ast.body[0].start;
}
}
export default function convertAST(ast, code) {
convertNodes(ast, code);
convertProgramNode(ast);
}