1 | import traverse from '@babel/traverse';
|
2 |
|
3 |
|
4 |
|
5 | export function babel7toBabel6(ast) {
|
6 | const visitor = {
|
7 | ArrowFunctionExpression: node => {
|
8 | node.expression = node.body.type !== 'BlockStatement';
|
9 | },
|
10 | ExistsTypeAnnotation: node => {
|
11 | node.type = 'ExistentialTypeParam';
|
12 | },
|
13 | NumberLiteralTypeAnnotation: node => {
|
14 | node.type = 'NumericLiteralTypeAnnotation';
|
15 | },
|
16 | ObjectTypeIndexer: node => {
|
17 | node.end++;
|
18 | node.loc.end.column++;
|
19 | },
|
20 | ForOfStatement: node => {
|
21 | node.type = 'ForAwaitStatement';
|
22 | delete node.await;
|
23 | },
|
24 | SpreadElement: (node, path) => {
|
25 | if (
|
26 | path.parentPath.isObjectExpression() ||
|
27 | path.parentPath.isArrayExpression()
|
28 | ) {
|
29 | node.type = 'SpreadProperty';
|
30 | }
|
31 | },
|
32 | RestElement: (node, path) => {
|
33 | if (
|
34 | path.parentPath.isObjectPattern() ||
|
35 | path.parentPath.isArrayPattern()
|
36 | ) {
|
37 | node.type = 'RestProperty';
|
38 | }
|
39 | },
|
40 | };
|
41 |
|
42 | traverse(ast, {
|
43 | enter(path) {
|
44 | if (path.node.variance && path.node.variance.type === 'Variance') {
|
45 | path.node.variance = path.node.variance.kind;
|
46 | }
|
47 |
|
48 | let visitorFunc = visitor[path.node.type];
|
49 | if (visitorFunc) {
|
50 | visitorFunc(path.node, path);
|
51 | }
|
52 | },
|
53 | });
|
54 |
|
55 | return ast;
|
56 | }
|
57 |
|
58 | export function babel6toBabel7(ast) {
|
59 | const visitor = {
|
60 | ArrowFunctionExpression: node => {
|
61 | delete node.expression;
|
62 | },
|
63 | ExistentialTypeParam: node => {
|
64 | node.type = 'ExistsTypeAnnotation';
|
65 | },
|
66 | NumericLiteralTypeAnnotation: node => {
|
67 | node.type = 'NumberLiteralTypeAnnotation';
|
68 | },
|
69 | ObjectTypeIndexer: node => {
|
70 | node.end--;
|
71 | node.loc.end.column--;
|
72 | },
|
73 | ForAwaitStatement: node => {
|
74 | node.type = 'ForOfStatement';
|
75 | node.await = true;
|
76 | },
|
77 | SpreadProperty: node => {
|
78 | node.type = 'SpreadElement';
|
79 | },
|
80 | RestProperty: node => {
|
81 | node.type = 'RestElement';
|
82 | },
|
83 | };
|
84 |
|
85 | traverse(ast, {
|
86 | enter(path) {
|
87 | if (path.node.variance && typeof path.node.variance === 'string') {
|
88 | path.node.variance = {
|
89 | type: 'VarianceNode',
|
90 | kind: path.node.variance,
|
91 | };
|
92 | }
|
93 |
|
94 | let visitorFunc = visitor[path.node.type];
|
95 | if (visitorFunc) {
|
96 | visitorFunc(path.node);
|
97 | }
|
98 | },
|
99 | });
|
100 |
|
101 | return ast;
|
102 | }
|