-
Notifications
You must be signed in to change notification settings - Fork 6
/
estree-to-babel.js
124 lines (94 loc) · 3.25 KB
/
estree-to-babel.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
'use strict';
const {join} = require('path');
const {
readFileSync,
writeFileSync,
} = require('fs');
const test = require('supertape');
const espree = require('espree');
const estreeToBabel = require('..');
const parse = (source) => {
return espree.parse(source, {
ecmaVersion: 2018,
loc: true,
comment: true,
});
};
const fixtureDir = join(__dirname, 'fixture');
const isUpdate = process.env.UPDATE_FIXTURE;
const update = (a, json) => {
if (!isUpdate)
return;
writeFileSync(`${fixtureDir}/${a}.json`, JSON.stringify(json, null, 4));
};
const readJS = (a) => readFileSync(join(`${fixtureDir}/${a}`), 'utf8');
const readJSON = (a) => require(`${fixtureDir}/${a}`);
const fixture = {
ast: {
property: readJSON('property.json'),
objectMethod: readJSON('object-method.json'),
stringLiteral: readJSON('string-literal.json'),
numericLiteral: readJSON('numeric-literal.json'),
nullLiteral: readJSON('null-literal.json'),
comments: readJSON('comments.json'),
classMethod: readJSON('class-method.json'),
},
js: {
property: readJS('property.js'),
objectMethod: readJS('object-method.js'),
stringLiteral: readJS('string-literal.js'),
numericLiteral: readJS('numeric-literal.js'),
nullLiteral: readJS('null-literal.js'),
comments: readJS('comments.js'),
classMethod: readJS('class-method.js'),
},
};
test('estree-to-babel: property', (t) => {
const ast = parse(fixture.js.property);
const result = estreeToBabel(ast);
update('property', result);
t.jsonEqual(result, fixture.ast.property, 'should equal');
t.end();
});
test('estree-to-babel: object-method', (t) => {
const ast = parse(fixture.js.objectMethod);
const result = estreeToBabel(ast);
update('object-method', result);
t.jsonEqual(result, fixture.ast.objectMethod, 'should equal');
t.end();
});
test('estree-to-babel: string-literal', (t) => {
const ast = parse(fixture.js.stringLiteral);
const result = estreeToBabel(ast);
update('string-literal', result);
t.jsonEqual(result, fixture.ast.stringLiteral, 'should equal');
t.end();
});
test('estree-to-babel: numeric-literal', (t) => {
const ast = parse(fixture.js.numericLiteral);
const result = estreeToBabel(ast);
update('numeric-literal', result);
t.jsonEqual(result, fixture.ast.numericLiteral, 'should equal');
t.end();
});
test('estree-to-babel: null literal', (t) => {
const ast = parse(fixture.js.nullLiteral);
const result = estreeToBabel(ast);
update('null-literal', result);
t.jsonEqual(result, fixture.ast.nullLiteral, 'should equal');
t.end();
});
test('estree-to-babel: comments', (t) => {
const ast = parse(fixture.js.comments);
const result = estreeToBabel(ast);
update('comments', result);
t.jsonEqual(result, fixture.ast.comments, 'should equal');
t.end();
});
test('estree-to-babel: class method', (t) => {
const ast = parse(fixture.js.classMethod);
const result = estreeToBabel(ast);
update('class-method', result);
t.jsonEqual(result, fixture.ast.classMethod, 'should equal');
t.end();
});