-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
acorn-to-esprima.js
58 lines (52 loc) 路 1.62 KB
/
acorn-to-esprima.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
var tokTypes = require("babel").acorn.tokTypes;
var traverse = require("babel").traverse;
var t = require("babel").types;
exports.toToken = function (token) {
var type = token.type;
if (type === tokTypes.name) {
token.type = "Identifier";
} else if (type === tokTypes.semi || type === tokTypes.comma || type === tokTypes.parenL || type === tokTypes.parenR || type === tokTypes.braceL || type === tokTypes.braceR) {
token.type = "Punctuator";
token.value = type.type;
}
return token;
};
exports.toAST = function (ast) {
traverse(ast, astTransformVisitor);
};
var astTransformVisitor = {
noScope: true,
enter: function (node) {
if (t.isImportBatchSpecifier(node)) {
// ImportBatchSpecifier<name> => ImportNamespaceSpecifier<id>
node.type = "ImportNamespaceSpecifier";
node.id = node.name;
delete node.name;
} else if (t.isFunction(node)) {
// defaults
node.defaults = [];
node.params = node.params.map(function (param) {
if (t.isAssignmentPattern(param)) {
node.defaults.push(param.right);
return param.left;
} else {
node.defaults.push(null);
return param;
}
});
// rest
if (t.isRestElement(node.params[node.params.length - 1])) {
node.rest = node.params.pop();
}
} else if (t.isSpreadProperty(node)) {
node.type = "Property";
node.kind = "init";
node.computed = false;
node.key = node.value = node.argument;
delete node.argument;
} else if (t.isClassProperty(node)) {
// eslint doesn't like these
this.remove();
}
}
};