UNPKG

2.71 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.babel7toBabel6 = babel7toBabel6;
7exports.babel6toBabel7 = babel6toBabel7;
8
9var _traverse = _interopRequireDefault(require("@babel/traverse"));
10
11function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
13// Convert between babel 7 and babel 6 AST
14// More info on the AST Changes: https://babeljs.io/docs/en/v7-migration-api#ast-changes
15function babel7toBabel6(ast) {
16 const visitor = {
17 ArrowFunctionExpression: node => {
18 node.expression = node.body.type !== 'BlockStatement';
19 },
20 ExistsTypeAnnotation: node => {
21 node.type = 'ExistentialTypeParam';
22 },
23 NumberLiteralTypeAnnotation: node => {
24 node.type = 'NumericLiteralTypeAnnotation';
25 },
26 ObjectTypeIndexer: node => {
27 node.end++;
28 node.loc.end.column++;
29 },
30 ForOfStatement: node => {
31 node.type = 'ForAwaitStatement';
32 delete node.await;
33 },
34 SpreadElement: (node, path) => {
35 if (path.parentPath.isObjectExpression() || path.parentPath.isArrayExpression()) {
36 node.type = 'SpreadProperty';
37 }
38 },
39 RestElement: (node, path) => {
40 if (path.parentPath.isObjectPattern() || path.parentPath.isArrayPattern()) {
41 node.type = 'RestProperty';
42 }
43 }
44 };
45 (0, _traverse.default)(ast, {
46 enter(path) {
47 if (path.node.variance && path.node.variance.type === 'Variance') {
48 path.node.variance = path.node.variance.kind;
49 }
50
51 let visitorFunc = visitor[path.node.type];
52
53 if (visitorFunc) {
54 visitorFunc(path.node, path);
55 }
56 }
57
58 });
59 return ast;
60}
61
62function babel6toBabel7(ast) {
63 const visitor = {
64 ArrowFunctionExpression: node => {
65 delete node.expression;
66 },
67 ExistentialTypeParam: node => {
68 node.type = 'ExistsTypeAnnotation';
69 },
70 NumericLiteralTypeAnnotation: node => {
71 node.type = 'NumberLiteralTypeAnnotation';
72 },
73 ObjectTypeIndexer: node => {
74 node.end--;
75 node.loc.end.column--;
76 },
77 ForAwaitStatement: node => {
78 node.type = 'ForOfStatement';
79 node.await = true;
80 },
81 SpreadProperty: node => {
82 node.type = 'SpreadElement';
83 },
84 RestProperty: node => {
85 node.type = 'RestElement';
86 }
87 };
88 (0, _traverse.default)(ast, {
89 enter(path) {
90 if (path.node.variance && typeof path.node.variance === 'string') {
91 path.node.variance = {
92 type: 'VarianceNode',
93 kind: path.node.variance
94 };
95 }
96
97 let visitorFunc = visitor[path.node.type];
98
99 if (visitorFunc) {
100 visitorFunc(path.node);
101 }
102 }
103
104 });
105 return ast;
106}
\No newline at end of file