| 1 |
import { createRequire } from "module"; |
| 2 |
const require = createRequire(import.meta.url); |
| 3 |
|
| 4 |
|
| 5 |
describe("@babel/standalone", () => { |
| 6 |
let Babel; |
| 7 |
beforeAll(() => { |
| 8 |
Babel = require("../babel.js"); |
| 9 |
}); |
| 10 |
|
| 11 |
it("handles the es2015-no-commonjs preset", () => { |
| 12 |
const output = Babel.transform('const getMessage = () => "Hello World"', { |
| 13 |
presets: ["es2015-no-commonjs"], |
| 14 |
}).code; |
| 15 |
expect(output).toBe( |
| 16 |
"var getMessage = function getMessage() {\n" + |
| 17 |
' return "Hello World";\n' + |
| 18 |
"};", |
| 19 |
); |
| 20 |
}); |
| 21 |
it("handles the es2015-loose preset", () => { |
| 22 |
const output = Babel.transform("class A {}", { |
| 23 |
sourceType: "script", |
| 24 |
presets: ["es2015-loose"], |
| 25 |
}).code; |
| 26 |
expect(output).toBe('var A = function A() {\n "use strict";\n};'); |
| 27 |
}); |
| 28 |
it("handles the typescript preset", () => { |
| 29 |
const output = Babel.transform("var a: string;", { |
| 30 |
presets: [["typescript", { allExtensions: true }]], |
| 31 |
}).code; |
| 32 |
expect(output).toBe("var a;"); |
| 33 |
}); |
| 34 |
it("handles the flow preset", () => { |
| 35 |
const output = Babel.transform("var a: string;", { |
| 36 |
presets: ["flow"], |
| 37 |
}).code; |
| 38 |
expect(output).toBe("var a;"); |
| 39 |
}); |
| 40 |
it("can translate simple ast", () => { |
| 41 |
const ast = { |
| 42 |
type: "Program", |
| 43 |
start: 0, |
| 44 |
end: 2, |
| 45 |
directives: [], |
| 46 |
body: [ |
| 47 |
{ |
| 48 |
type: "ExpressionStatement", |
| 49 |
start: 0, |
| 50 |
end: 1, |
| 51 |
expression: { |
| 52 |
type: "NumericLiteral", |
| 53 |
start: 0, |
| 54 |
end: 2, |
| 55 |
value: 42, |
| 56 |
raw: "42", |
| 57 |
}, |
| 58 |
}, |
| 59 |
], |
| 60 |
sourceType: "script", |
| 61 |
}; |
| 62 |
const output = Babel.transformFromAst(ast, "42", { |
| 63 |
presets: ["es2015"], |
| 64 |
}).code; |
| 65 |
expect(output).toBe("42;"); |
| 66 |
}); |
| 67 |
|
| 68 |
it("handles the react preset", () => { |
| 69 |
const output = Babel.transform( |
| 70 |
"const someDiv = <div>{getMessage()}</div>", |
| 71 |
{ |
| 72 |
presets: [["react", { runtime: "classic" }]], |
| 73 |
}, |
| 74 |
).code; |
| 75 |
expect(output).toBe( |
| 76 |
'const someDiv = /*#__PURE__*/React.createElement("div", null, getMessage());', |
| 77 |
); |
| 78 |
}); |
| 79 |
|
| 80 |
it("handles presets with options", () => { |
| 81 |
const output = Babel.transform("export let x", { |
| 82 |
presets: [["es2015", { modules: false }]], |
| 83 |
}).code; |
| 84 |
expect(output).toBe("export var x;"); |
| 85 |
}); |
| 86 |
|
| 87 |
it("handles specifying a plugin by name", () => { |
| 88 |
const output = Babel.transform('const getMessage = () => "Hello World"', { |
| 89 |
plugins: ["transform-arrow-functions"], |
| 90 |
}).code; |
| 91 |
|
| 92 |
expect(output).toBe( |
| 93 |
"const getMessage = function () {\n" + ' return "Hello World";\n' + "};", |
| 94 |
); |
| 95 |
}); |
| 96 |
|
| 97 |
it("handles plugins with options", () => { |
| 98 |
const output = Babel.transform("`${x}`", { |
| 99 |
plugins: [["transform-template-literals", { loose: true }]], |
| 100 |
}).code; |
| 101 |
expect(output).toBe('"" + x;'); |
| 102 |
}); |
| 103 |
|
| 104 |
it("throws on invalid preset name", () => { |
| 105 |
expect(() => Babel.transform("var foo", { presets: ["lolfail"] })).toThrow( |
| 106 |
/Invalid preset specified in Babel options: "lolfail"/, |
| 107 |
); |
| 108 |
}); |
| 109 |
|
| 110 |
it("throws on invalid plugin name", () => { |
| 111 |
expect(() => Babel.transform("var foo", { plugins: ["lolfail"] })).toThrow( |
| 112 |
/Invalid plugin specified in Babel options: "lolfail"/, |
| 113 |
); |
| 114 |
}); |
| 115 |
|
| 116 |
describe("env preset", () => { |
| 117 |
it("works w/o targets", () => { |
| 118 |
const output = Babel.transform("const a = 1;", { |
| 119 |
sourceType: "script", |
| 120 |
presets: [["env", { targets: { browsers: "ie 6" } }]], |
| 121 |
}).code; |
| 122 |
expect(output).toBe("var a = 1;"); |
| 123 |
}); |
| 124 |
|
| 125 |
it("doesn't transpile `const` with chrome 60", () => { |
| 126 |
const output = Babel.transform("const a = 1;", { |
| 127 |
sourceType: "script", |
| 128 |
presets: [ |
| 129 |
[ |
| 130 |
"env", |
| 131 |
{ |
| 132 |
targets: { |
| 133 |
chrome: 60, |
| 134 |
}, |
| 135 |
}, |
| 136 |
], |
| 137 |
], |
| 138 |
}).code; |
| 139 |
expect(output).toBe("const a = 1;"); |
| 140 |
}); |
| 141 |
|
| 142 |
it("transpiles `const` with chrome 60 and preset-es2015", () => { |
| 143 |
const output = Babel.transform("const a = 1;", { |
| 144 |
sourceType: "script", |
| 145 |
presets: [ |
| 146 |
[ |
| 147 |
"env", |
| 148 |
{ |
| 149 |
targets: { |
| 150 |
chrome: 60, |
| 151 |
}, |
| 152 |
}, |
| 153 |
], |
| 154 |
"es2015", |
| 155 |
], |
| 156 |
}).code; |
| 157 |
expect(output).toBe("var a = 1;"); |
| 158 |
}); |
| 159 |
|
| 160 |
it("uses transform-new-targets plugin", () => { |
| 161 |
const output = Babel.transform("function Foo() {new.target}", { |
| 162 |
sourceType: "script", |
| 163 |
presets: [["env", { targets: { browsers: "ie 6" } }]], |
| 164 |
}).code; |
| 165 |
expect(output).toBe( |
| 166 |
"function Foo() {\n this instanceof Foo ? this.constructor : void 0;\n}", |
| 167 |
); |
| 168 |
}); |
| 169 |
|
| 170 |
it("useBuiltIns works", () => { |
| 171 |
const output = Babel.transform("[].includes(2)", { |
| 172 |
sourceType: "module", |
| 173 |
targets: { ie: 11 }, |
| 174 |
presets: [["env", { useBuiltIns: "usage", corejs: 3, modules: false }]], |
| 175 |
}).code; |
| 176 |
|
| 177 |
expect(output).toMatchInlineSnapshot(` |
| 178 |
"import \\"core-js/modules/es.array.includes.js\\"; |
| 179 |
[].includes(2);" |
| 180 |
`); |
| 181 |
}); |
| 182 |
|
| 183 |
it("regenerator works", () => { |
| 184 |
const output = Babel.transform("function* fn() {}", { |
| 185 |
sourceType: "module", |
| 186 |
targets: { ie: 11 }, |
| 187 |
presets: ["env"], |
| 188 |
}).code; |
| 189 |
|
| 190 |
expect(output).toMatch("regeneratorRuntime().mark(fn)"); |
| 191 |
}); |
| 192 |
}); |
| 193 |
|
| 194 |
describe("custom plugins and presets", () => { |
| 195 |
const lolizer = () => ({ |
| 196 |
visitor: { |
| 197 |
Identifier(path) { |
| 198 |
path.node.name = "LOL"; |
| 199 |
}, |
| 200 |
}, |
| 201 |
}); |
| 202 |
|
| 203 |
it("allows custom plugins to be registered", () => { |
| 204 |
Babel.registerPlugin("lolizer", lolizer); |
| 205 |
const output = Babel.transform( |
| 206 |
"function helloWorld() { alert(hello); }", |
| 207 |
{ plugins: ["lolizer"] }, |
| 208 |
); |
| 209 |
expect(output.code).toBe(`function LOL() { |
| 210 |
LOL(LOL); |
| 211 |
}`); |
| 212 |
}); |
| 213 |
|
| 214 |
it("allows custom presets to be registered", () => { |
| 215 |
Babel.registerPreset("lulz", { plugins: [lolizer] }); |
| 216 |
const output = Babel.transform( |
| 217 |
"function helloWorld() { alert(hello); }", |
| 218 |
{ presets: ["lulz"] }, |
| 219 |
); |
| 220 |
expect(output.code).toBe(`function LOL() { |
| 221 |
LOL(LOL); |
| 222 |
}`); |
| 223 |
}); |
| 224 |
}); |
| 225 |
|
| 226 |
describe("regressions", () => { |
| 227 |
it("#11534 - supports quantifiers in unicode regexps", () => { |
| 228 |
expect(() => |
| 229 |
Babel.transform("/a*/u", { presets: ["es2015"] }), |
| 230 |
).not.toThrow(); |
| 231 |
}); |
| 232 |
it("#11628 - supports stage-0 passing importAssertionsVersion to stage-1", () => { |
| 233 |
expect(() => |
| 234 |
Babel.transform("const getMessage = () => 'Hello World'", { |
| 235 |
presets: [["stage-0", { decoratorsVersion: "legacy" }]], |
| 236 |
}), |
| 237 |
).not.toThrow(); |
| 238 |
}); |
| 239 |
it("#11897 - [...map.keys()] in Babel source should be transformed correctly", () => { |
| 240 |
expect(() => |
| 241 |
Babel.transform("for (let el of []) { s => el }", { |
| 242 |
plugins: ["transform-block-scoping"], |
| 243 |
}), |
| 244 |
).not.toThrow(); |
| 245 |
}); |
| 246 |
it("#12815 - unicode property letter short alias should be transformed", () => { |
| 247 |
expect(() => |
| 248 |
Babel.transform("/\\p{L}/u", { |
| 249 |
plugins: ["proposal-unicode-property-regex"], |
| 250 |
}), |
| 251 |
).not.toThrow(); |
| 252 |
}); |
| 253 |
it("#14425 - numeric separators should be parsed correctly", () => { |
| 254 |
expect(() => Babel.transform("1_1", {})).not.toThrow(); |
| 255 |
}); |
| 256 |
}); |
| 257 |
}); |