/
babel.js
200 lines (186 loc) 路 5.64 KB
/
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Basic smoke tests for @babel/standalone
(process.env.TEST_TYPE === "cov" ? describe.skip : describe)(
"@babel/standalone",
() => {
const Babel = require("../babel");
it("handles the es2015-no-commonjs preset", () => {
const output = Babel.transform('const getMessage = () => "Hello World"', {
presets: ["es2015-no-commonjs"],
}).code;
expect(output).toBe(
"var getMessage = function getMessage() {\n" +
' return "Hello World";\n' +
"};",
);
});
it("handles the es2015-loose preset", () => {
const output = Babel.transform("class A {}", {
sourceType: "script",
presets: ["es2015-loose"],
}).code;
expect(output).toBe('var A = function A() {\n "use strict";\n};');
});
it("handles the typescript preset", () => {
const output = Babel.transform("var a: string;", {
presets: [["typescript", { allExtensions: true }]],
}).code;
expect(output).toBe("var a;");
});
it("handles the flow preset", () => {
const output = Babel.transform("var a: string;", {
presets: ["flow"],
}).code;
expect(output).toBe("var a;");
});
it("can translate simple ast", () => {
const ast = {
type: "Program",
start: 0,
end: 2,
directives: [],
body: [
{
type: "ExpressionStatement",
start: 0,
end: 1,
expression: {
type: "NumericLiteral",
start: 0,
end: 2,
value: 42,
raw: "42",
},
},
],
sourceType: "script",
};
const output = Babel.transformFromAst(ast, "42", { presets: ["es2015"] })
.code;
expect(output).toBe("42;");
});
it("handles the react preset", () => {
const output = Babel.transform(
"const someDiv = <div>{getMessage()}</div>",
{
presets: ["react"],
},
).code;
expect(output).toBe(
'const someDiv = React.createElement("div", null, getMessage());',
);
});
it("handles presets with options", () => {
const output = Babel.transform("export let x", {
presets: [["es2015", { modules: false }]],
}).code;
expect(output).toBe("export var x;");
});
it("handles specifying a plugin by name", () => {
const output = Babel.transform('const getMessage = () => "Hello World"', {
plugins: ["transform-arrow-functions"],
}).code;
// Transforms arrow syntax but NOT "const".
expect(output).toBe(
"const getMessage = function () {\n" +
' return "Hello World";\n' +
"};",
);
});
it("handles plugins with options", () => {
const output = Babel.transform("`${x}`", {
plugins: [["transform-template-literals", { loose: true }]],
}).code;
expect(output).toBe('"" + x;');
});
it("throws on invalid preset name", () => {
expect(() =>
Babel.transform("var foo", { presets: ["lolfail"] }),
).toThrow(/Invalid preset specified in Babel options: "lolfail"/);
});
it("throws on invalid plugin name", () => {
expect(() =>
Babel.transform("var foo", { plugins: ["lolfail"] }),
).toThrow(/Invalid plugin specified in Babel options: "lolfail"/);
});
describe("env preset", () => {
it("works w/o targets", () => {
const output = Babel.transform("const a = 1;", {
sourceType: "script",
presets: ["env"],
}).code;
expect(output).toBe("var a = 1;");
});
it("doesn't transpile `const` with chrome 60", () => {
const output = Babel.transform("const a = 1;", {
sourceType: "script",
presets: [
[
"env",
{
targets: {
chrome: 60,
},
},
],
],
}).code;
expect(output).toBe("const a = 1;");
});
it("transpiles `const` with chrome 60 and preset-es2015", () => {
const output = Babel.transform("const a = 1;", {
sourceType: "script",
presets: [
[
"env",
{
targets: {
chrome: 60,
},
},
],
"es2015",
],
}).code;
expect(output).toBe("var a = 1;");
});
it("uses transform-new-targets plugin", () => {
const output = Babel.transform("function Foo() {new.target}", {
sourceType: "script",
presets: ["env"],
}).code;
expect(output).toBe(
"function Foo() {\n this instanceof Foo ? this.constructor : void 0;\n}",
);
});
});
describe("custom plugins and presets", () => {
const lolizer = () => ({
visitor: {
Identifier(path) {
path.node.name = "LOL";
},
},
});
it("allows custom plugins to be registered", () => {
Babel.registerPlugin("lolizer", lolizer);
const output = Babel.transform(
"function helloWorld() { alert(hello); }",
{ plugins: ["lolizer"] },
);
expect(output.code).toBe(`function LOL() {
LOL(LOL);
}`);
});
it("allows custom presets to be registered", () => {
Babel.registerPreset("lulz", { plugins: [lolizer] });
const output = Babel.transform(
"function helloWorld() { alert(hello); }",
{ presets: ["lulz"] },
);
expect(output.code).toBe(`function LOL() {
LOL(LOL);
}`);
});
});
},
);