Debian Codegraph @

packages/babel-core/test/api.js

raw
1 import * as babel from "../lib/index.js";
2 import { TraceMap, originalPositionFor } from "@jridgewell/trace-mapping";
3 import path from "path";
4 import generator from "@babel/generator";
5 import { fileURLToPath } from "url";
6
7 import _Plugin from "../lib/config/plugin.js";
8 const Plugin = _Plugin.default || _Plugin;
9
10 import presetEnv from "@babel/preset-env";
11 import pluginSyntaxFlow from "@babel/plugin-syntax-flow";
12 import pluginSyntaxJSX from "@babel/plugin-syntax-jsx";
13 import pluginFlowStripTypes from "@babel/plugin-transform-flow-strip-types";
14
15 const cwd = path.dirname(fileURLToPath(import.meta.url));
16
17 function assertIgnored(result) {
18 expect(result).toBeNull();
19 }
20
21 function assertNotIgnored(result) {
22 expect(result).not.toBeNull();
23 }
24
25 function parse(code, opts) {
26 return babel.parse(code, { cwd, configFile: false, ...opts });
27 }
28
29 function parseSync(code, opts) {
30 return babel.parseSync(code, { cwd, configFile: false, ...opts });
31 }
32
33 function transform(code, opts) {
34 return babel.transform(code, { cwd, configFile: false, ...opts });
35 }
36
37 function transformSync(code, opts) {
38 return babel.transformSync(code, { cwd, configFile: false, ...opts });
39 }
40
41 function transformFile(filename, opts, cb) {
42 return babel.transformFile(filename, { cwd, configFile: false, ...opts }, cb);
43 }
44 function transformFileSync(filename, opts) {
45 return babel.transformFileSync(filename, { cwd, configFile: false, ...opts });
46 }
47 function transformFileAsync(filename, opts) {
48 return babel.transformFileAsync(filename, {
49 cwd,
50 configFile: false,
51 ...opts,
52 });
53 }
54
55 function transformAsync(code, opts) {
56 return babel.transformAsync(code, { cwd, configFile: false, ...opts });
57 }
58
59 function transformFromAst(ast, code, opts) {
60 return babel.transformFromAst(ast, code, { cwd, configFile: false, ...opts });
61 }
62
63 function transformFromAstSync(ast, code, opts) {
64 return babel.transformFromAstSync(ast, code, {
65 cwd,
66 configFile: false,
67 ...opts,
68 });
69 }
70
71 describe("parser and generator options", function () {
72 const recast = {
73 parse: function (code, opts) {
74 return opts.parser.parse(code);
75 },
76 print: function (ast) {
77 return (generator.default || generator)(ast);
78 },
79 };
80
81 function newTransform(string) {
82 return transformSync(string, {
83 ast: true,
84 parserOpts: {
85 parser: recast.parse,
86 plugins: ["flow"],
87 allowImportExportEverywhere: true,
88 },
89 generatorOpts: {
90 generator: recast.print,
91 },
92 });
93 }
94
95 it("options", function () {
96 const string = "original;";
97 expect(newTransform(string).ast).toEqual(
98 transformSync(string, { ast: true }).ast,
99 );
100 expect(newTransform(string).code).toBe(string);
101 });
102
103 it("experimental syntax", function () {
104 const experimental = "var a: number = 1;";
105
106 expect(newTransform(experimental).ast).toEqual(
107 transformSync(experimental, {
108 ast: true,
109 parserOpts: {
110 plugins: ["flow"],
111 },
112 }).ast,
113 );
114 expect(newTransform(experimental).code).toBe(experimental);
115
116 function newTransformWithPlugins(string) {
117 return transformSync(string, {
118 ast: true,
119 plugins: [pluginSyntaxFlow],
120 parserOpts: {
121 parser: recast.parse,
122 },
123 generatorOpts: {
124 generator: recast.print,
125 },
126 });
127 }
128
129 expect(newTransformWithPlugins(experimental).ast).toEqual(
130 transformSync(experimental, {
131 ast: true,
132 parserOpts: {
133 plugins: ["flow"],
134 },
135 }).ast,
136 );
137 expect(newTransformWithPlugins(experimental).code).toBe(experimental);
138 });
139
140 it("other options", function () {
141 const experimental = "if (true) {\n import a from 'a';\n}";
142
143 expect(newTransform(experimental).ast).not.toBe(
144 transformSync(experimental, {
145 ast: true,
146 parserOpts: {
147 allowImportExportEverywhere: true,
148 },
149 }).ast,
150 );
151 expect(newTransform(experimental).code).toBe(experimental);
152 });
153 });
154
155 describe("api", function () {
156 it("exposes the resolvePlugin method", function () {
157 expect(() => babel.resolvePlugin("nonexistent-plugin")).toThrow(
158 /Cannot (?:find|resolve) module 'babel-plugin-nonexistent-plugin'/,
159 );
160 });
161
162 it("exposes the resolvePreset method", function () {
163 expect(() => babel.resolvePreset("nonexistent-preset")).toThrow(
164 /Cannot (?:find|resolve) module 'babel-preset-nonexistent-preset'/,
165 );
166 });
167
168 it("exposes types", function () {
169 expect(babel.types).toBeDefined();
170 });
171
172 it("exposes the parser's token types", function () {
173 expect(babel.tokTypes).toBeDefined();
174 });
175
176 (process.env.BABEL_8_BREAKING ? it : it.skip)(
177 "parse throws on undefined callback",
178 () => {
179 expect(() => parse("", {})).toThrowErrorMatchingInlineSnapshot(
180 `"Starting from Babel 8.0.0, the 'parse' function expects a callback. If you need to call it synchronously, please use 'parseSync'."`,
181 );
182 },
183 );
184
185 (process.env.BABEL_8_BREAKING ? it : it.skip)(
186 "transform throws on undefined callback",
187 () => {
188 const options = {
189 filename: "example.js",
190 };
191 expect(() => transform("", options)).toThrowErrorMatchingInlineSnapshot(
192 `"Starting from Babel 8.0.0, the 'transform' function expects a callback. If you need to call it synchronously, please use 'transformSync'."`,
193 );
194 },
195 );
196
197 it("transformFile", function () {
198 const options = {
199 babelrc: false,
200 };
201 Object.freeze(options);
202 return new Promise((resolve, reject) => {
203 transformFile(
204 cwd + "/fixtures/api/file.js",
205 options,
206 function (err, res) {
207 if (err) return reject(err);
208 expect(res.code).toBe("foo();");
209 // keep user options untouched
210 expect(options).toEqual({ babelrc: false });
211 resolve();
212 },
213 );
214 });
215 });
216
217 it("transformFileAsync", async function () {
218 const options = {
219 babelrc: false,
220 };
221 Object.freeze(options);
222 const res = await transformFileAsync(
223 cwd + "/fixtures/api/file.js",
224 options,
225 );
226 expect(res.code).toBe("foo();");
227 // keep user options untouched
228 expect(options).toEqual({ babelrc: false });
229 });
230 it("transformFile throws on undefined callback", () => {
231 const options = {
232 babelrc: false,
233 };
234 expect(() =>
235 transformFile(cwd + "/fixtures/api/file.js", options),
236 ).toThrowErrorMatchingInlineSnapshot(
237 `"Asynchronous function called without callback"`,
238 );
239 });
240
241 it("transformFileSync", function () {
242 const options = {
243 babelrc: false,
244 };
245 Object.freeze(options);
246 expect(transformFileSync(cwd + "/fixtures/api/file.js", options).code).toBe(
247 "foo();",
248 );
249 expect(options).toEqual({ babelrc: false });
250 });
251
252 (process.env.BABEL_8_BREAKING ? it : it.skip)(
253 "transformFromAst throws on undefined callback",
254 () => {
255 const program = "const identifier = 1";
256 const node = parseSync(program);
257 expect(() =>
258 transformFromAst(node, program),
259 ).toThrowErrorMatchingInlineSnapshot(
260 `"Starting from Babel 8.0.0, the 'transformFromAst' function expects a callback. If you need to call it synchronously, please use 'transformFromAstSync'."`,
261 );
262 },
263 );
264
265 it("transformFromAst should generate same code with different cloneInputAst", function () {
266 const program = `//test1
267 /*test2*/var/*test3*/ a = 1/*test4*/;//test5
268 //test6
269 var b;
270 `;
271 const node = parseSync(program);
272 const { code } = transformFromAstSync(node, program, {
273 plugins: [
274 function () {
275 return {
276 visitor: {
277 Identifier: function (path) {
278 path.node.name = "replaced";
279 },
280 },
281 };
282 },
283 ],
284 });
285 const { code: code2 } = transformFromAstSync(node, program, {
286 cloneInputAst: false,
287 plugins: [
288 function () {
289 return {
290 visitor: {
291 Identifier: function (path) {
292 path.node.name = "replaced";
293 },
294 },
295 };
296 },
297 ],
298 });
299
300 expect(code).toBe(code2);
301 });
302
303 it("transformFromAst should not mutate the AST", function () {
304 const program = "const identifier = 1";
305 const node = parseSync(program);
306 const { code } = transformFromAstSync(node, program, {
307 plugins: [
308 function () {
309 return {
310 visitor: {
311 Identifier: function (path) {
312 path.node.name = "replaced";
313 },
314 },
315 };
316 },
317 ],
318 });
319
320 expect(code).toBe("const replaced = 1;");
321 expect(node.program.body[0].declarations[0].id.name).toBe(
322 "identifier",
323 "original ast should not have been mutated",
324 );
325 });
326
327 it("transformFromAstSync should mutate the AST when cloneInputAst is false", function () {
328 const program = "const identifier = 1";
329 const node = parseSync(program);
330 const { code } = transformFromAstSync(node, program, {
331 cloneInputAst: false,
332 plugins: [
333 function () {
334 return {
335 visitor: {
336 Identifier: function (path) {
337 path.node.name = "replaced";
338 },
339 },
340 };
341 },
342 ],
343 });
344
345 expect(code).toBe("const replaced = 1;");
346 expect(node.program.body[0].declarations[0].id.name).toBe(
347 "replaced",
348 "original ast should have been mutated",
349 );
350 });
351
352 it("options throw on falsy true", function () {
353 return expect(function () {
354 transformSync("", {
355 plugins: [pluginSyntaxJSX, false],
356 });
357 }).toThrow(/.plugins\[1\] must be a string, object, function/);
358 });
359
360 it("options merge backwards", async function () {
361 const result = await transformAsync("", {
362 cwd,
363 presets: ["@babel/preset-env"],
364 plugins: ["@babel/plugin-syntax-jsx"],
365 });
366
367 expect(result.options.plugins[0].manipulateOptions.toString()).toEqual(
368 expect.stringContaining("jsx"),
369 );
370 });
371
372 it("option wrapPluginVisitorMethod", function () {
373 let calledRaw = 0;
374 let calledIntercept = 0;
375
376 transformSync("function foo() { bar(foobar); }", {
377 wrapPluginVisitorMethod: function (pluginAlias, visitorType, callback) {
378 if (pluginAlias !== "foobar") {
379 return callback;
380 }
381
382 expect(visitorType).toBe("enter");
383
384 return function () {
385 calledIntercept++;
386 return callback.apply(this, arguments);
387 };
388 },
389
390 plugins: [
391 new Plugin({
392 name: "foobar",
393 visitor: {
394 "Program|Identifier": function () {
395 calledRaw++;
396 },
397 },
398 }),
399 ],
400 });
401
402 expect(calledRaw).toBe(4);
403 expect(calledIntercept).toBe(4);
404 });
405
406 it("pass per preset", function () {
407 let aliasBaseType = null;
408
409 function execTest(passPerPreset) {
410 return transformSync("type Foo = number; let x = (y): Foo => y;", {
411 sourceType: "script",
412 passPerPreset: passPerPreset,
413 presets: [
414 // First preset with our plugin, "before"
415 function () {
416 return {
417 plugins: [
418 new Plugin({
419 visitor: {
420 Function: function (path) {
421 const alias = path.scope
422 .getProgramParent()
423 .path.get("body")[0].node;
424 if (!babel.types.isTypeAlias(alias)) return;
425
426 // In case of `passPerPreset` being `false`, the
427 // alias node is already removed by Flow plugin.
428 if (!alias) {
429 return;
430 }
431
432 // In case of `passPerPreset` being `true`, the
433 // alias node should still exist.
434 aliasBaseType = alias.right.type; // NumberTypeAnnotation
435 },
436 },
437 }),
438 ],
439 };
440 },
441
442 // env preset
443 [presetEnv, { targets: { browsers: "ie 6" } }],
444
445 // Third preset for Flow.
446 () => ({
447 plugins: [pluginSyntaxFlow, pluginFlowStripTypes],
448 }),
449 ],
450 });
451 }
452
453 // 1. passPerPreset: true
454
455 let result = execTest(true);
456
457 expect(aliasBaseType).toBe("NumberTypeAnnotation");
458
459 expect(result.code).toBe("var x = function x(y) {\n return y;\n};");
460
461 // 2. passPerPreset: false
462
463 aliasBaseType = null;
464
465 result = execTest(false);
466
467 expect(aliasBaseType).toBeNull();
468
469 expect(result.code).toBe("var x = function x(y) {\n return y;\n};");
470 });
471
472 it("complex plugin and preset ordering", function () {
473 function pushPlugin(str) {
474 return {
475 visitor: {
476 Program(path) {
477 path.pushContainer(
478 "body",
479 babel.types.expressionStatement(babel.types.identifier(str)),
480 );
481 },
482 },
483 };
484 }
485
486 function pushPreset(str) {
487 return { plugins: [pushPlugin(str)] };
488 }
489
490 const oldEnv = process.env.BABEL_ENV;
491 process.env.BABEL_ENV = "development";
492
493 const result = transformSync("", {
494 cwd: path.join(cwd, "fixtures", "config", "complex-plugin-config"),
495 filename: path.join(
496 cwd,
497 "fixtures",
498 "config",
499 "complex-plugin-config",
500 "file.js",
501 ),
502 presets: [pushPreset("argone"), pushPreset("argtwo")],
503 env: {
504 development: {
505 passPerPreset: true,
506 presets: [pushPreset("argthree"), pushPreset("argfour")],
507 },
508 },
509 });
510
511 if (oldEnv === undefined) {
512 delete process.env.BABEL_ENV;
513 } else {
514 process.env.BABEL_ENV = oldEnv;
515 }
516
517 expect(result.code).toBe(
518 [
519 "thirteen;",
520 "fourteen;",
521 "seventeen;",
522 "eighteen;",
523 "one;",
524 "two;",
525 "eleven;",
526 "twelve;",
527 "argtwo;",
528 "argone;",
529 "five;",
530 "six;",
531 "twentyone;",
532 "twentytwo;",
533 "three;",
534 "four;",
535 "nineteen;",
536 "twenty;",
537 "fifteen;",
538 "sixteen;",
539 "seven;",
540 "eight;",
541 "nine;",
542 "ten;",
543 "argthree;",
544 "argfour;",
545 ].join("\n"),
546 );
547 });
548
549 it("interpreter directive backward-compat", function () {
550 function doTransform(code, preHandler) {
551 return transformSync(code, {
552 plugins: [
553 {
554 pre: preHandler,
555 },
556 ],
557 }).code;
558 }
559
560 // Writes value properly.
561 expect(
562 doTransform("", file => {
563 file.shebang = "env node";
564 }),
565 ).toBe(`#!env node`);
566 expect(
567 doTransform("#!env node", file => {
568 file.shebang = "env node2";
569 }),
570 ).toBe(`#!env node2`);
571 expect(
572 doTransform("", file => {
573 file.shebang = "";
574 }),
575 ).toBe(``);
576 expect(
577 doTransform("#!env node", file => {
578 file.shebang = "";
579 }),
580 ).toBe(``);
581
582 // Reads value properly.
583 doTransform("", file => {
584 expect(file.shebang).toBe("");
585 });
586 doTransform("#!env node", file => {
587 expect(file.shebang).toBe("env node");
588 });
589
590 // Reads and writes properly.
591 expect(
592 doTransform("#!env node", file => {
593 expect(file.shebang).toBe("env node");
594
595 file.shebang = "env node2";
596 expect(file.shebang).toBe("env node2");
597
598 file.shebang = "env node3";
599 }),
600 ).toBe(`#!env node3`);
601 });
602
603 it("source map merging", function () {
604 const result = transformSync(
605 [
606 /* eslint-disable max-len */
607 'function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }',
608 "",
609 "let Foo = function Foo() {",
610 " _classCallCheck(this, Foo);",
611 "};",
612 "",
613 "//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0ZG91dCJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztJQUFNLEdBQUcsWUFBSCxHQUFHO3dCQUFILEdBQUciLCJmaWxlIjoidW5kZWZpbmVkIiwic291cmNlc0NvbnRlbnQiOlsiY2xhc3MgRm9vIHt9XG4iXX0=",
614 /* eslint-enable max-len */
615 ].join("\n"),
616 {
617 sourceMap: true,
618 },
619 );
620
621 expect(
622 [
623 "function _classCallCheck(instance, Constructor) {",
624 " if (!(instance instanceof Constructor)) {",
625 ' throw new TypeError("Cannot call a class as a function");',
626 " }",
627 "}",
628 "let Foo = function Foo() {",
629 " _classCallCheck(this, Foo);",
630 "};",
631 ].join("\n"),
632 ).toBe(result.code);
633
634 const consumer = new TraceMap(result.map);
635
636 expect(
637 originalPositionFor(consumer, {
638 line: 6,
639 column: 4,
640 }),
641 ).toEqual({
642 name: "Foo",
643 source: "stdout",
644 line: 1,
645 column: 6,
646 });
647 });
648
649 it("default source map filename", function () {
650 return transformAsync("var a = 10;", {
651 cwd: "/some/absolute",
652 filename: "/some/absolute/file/path.js",
653 sourceMaps: true,
654 }).then(function (result) {
655 expect(result.map.sources).toEqual(["path.js"]);
656 });
657 });
658
659 it("code option false", function () {
660 return transformAsync("foo('bar');", { code: false }).then(function (
661 result,
662 ) {
663 expect(result.code).toBeFalsy();
664 });
665 });
666
667 it("ast option false", function () {
668 return transformAsync("foo('bar');", { ast: false }).then(function (
669 result,
670 ) {
671 expect(result.ast).toBeFalsy();
672 });
673 });
674
675 it("ast option true", function () {
676 return transformAsync("foo('bar');", { ast: true }).then(function (result) {
677 expect(result.ast).toBeTruthy();
678 });
679 });
680
681 it("ast option default", function () {
682 return transformAsync("foo('bar');").then(function (result) {
683 expect(result.ast).toBeFalsy();
684 });
685 });
686
687 it("auxiliaryComment option", function () {
688 return transformAsync("class Foo {}", {
689 auxiliaryCommentBefore: "before",
690 auxiliaryCommentAfter: "after",
691 plugins: [
692 function (babel) {
693 const t = babel.types;
694 return {
695 visitor: {
696 Program: function (path) {
697 path.unshiftContainer(
698 "body",
699 t.expressionStatement(t.identifier("start")),
700 );
701 path.pushContainer(
702 "body",
703 t.expressionStatement(t.identifier("end")),
704 );
705 },
706 },
707 };
708 },
709 ],
710 }).then(function (result) {
711 expect(result.code).toBe(
712 "/*before*/\nstart;\n/*after*/\nclass Foo {}\n/*before*/\nend;\n/*after*/",
713 );
714 });
715 });
716
717 it("ignore option", function () {
718 return Promise.all([
719 transformAsync("", {
720 ignore: ["/foo"],
721 filename: "/foo/node_modules/bar",
722 }).then(assertIgnored),
723
724 transformAsync("", {
725 ignore: ["/foo/node_modules"],
726 filename: "/foo/node_modules/bar",
727 }).then(assertIgnored),
728
729 transformAsync("", {
730 ignore: ["/foo/node_modules/*"],
731 filename: "/foo/node_modules/bar",
732 }).then(assertIgnored),
733
734 transformAsync("", {
735 ignore: ["/foo/**/*"],
736 filename: "/foo/node_modules/bar",
737 }).then(assertIgnored),
738
739 transformAsync("", {
740 ignore: ["/foo/node_modules/*.bar"],
741 filename: "/foo/node_modules/foo.bar",
742 }).then(assertIgnored),
743
744 transformAsync("", {
745 ignore: ["/foo/node_modules/*.foo"],
746 filename: "/foo/node_modules/foo.bar",
747 }).then(assertNotIgnored),
748
749 transformAsync("", {
750 ignore: ["/bar/**/*"],
751 filename: "/foo/node_modules/foo.bar",
752 }).then(assertNotIgnored),
753 ]);
754 });
755
756 it("only option", function () {
757 return Promise.all([
758 transformAsync("", {
759 only: ["/foo"],
760 filename: "/foo/node_modules/bar",
761 }).then(assertNotIgnored),
762
763 transformAsync("", {
764 only: ["/foo/*"],
765 filename: "/foo/node_modules/bar",
766 }).then(assertNotIgnored),
767
768 transformAsync("", {
769 only: ["/foo/node_modules"],
770 filename: "/foo/node_modules/bar",
771 }).then(assertNotIgnored),
772
773 transformAsync("", {
774 only: ["/foo/node_modules/*.bar"],
775 filename: "/foo/node_modules/foo.bar",
776 }).then(assertNotIgnored),
777
778 transformAsync("", {
779 only: ["/foo/node_modules"],
780 filename: "/foo/node_module/bar",
781 }).then(assertIgnored),
782
783 transformAsync("", {
784 only: ["/foo/node_modules"],
785 filename: "/bar/node_modules/foo",
786 }).then(assertIgnored),
787
788 transformAsync("", {
789 only: ["/foo/node_modules/*.bar"],
790 filename: "/foo/node_modules/bar.foo",
791 }).then(assertIgnored),
792 ]);
793 });
794
795 describe("env option", function () {
796 const oldBabelEnv = process.env.BABEL_ENV;
797 const oldNodeEnv = process.env.NODE_ENV;
798
799 beforeEach(function () {
800 // Tests need to run with the default and specific values for these. They
801 // need to be cleared for each test.
802 delete process.env.BABEL_ENV;
803 delete process.env.NODE_ENV;
804 });
805
806 afterAll(function () {
807 process.env.BABEL_ENV = oldBabelEnv;
808 process.env.NODE_ENV = oldNodeEnv;
809 });
810
811 it("default", function () {
812 const result = transformSync("foo;", {
813 env: {
814 development: { comments: false },
815 },
816 });
817
818 expect(result.options.comments).toBe(false);
819 });
820
821 it("BABEL_ENV", function () {
822 process.env.BABEL_ENV = "foo";
823 const result = transformSync("foo;", {
824 env: {
825 foo: { comments: false },
826 },
827 });
828 expect(result.options.comments).toBe(false);
829 });
830
831 it("NODE_ENV", function () {
832 process.env.NODE_ENV = "foo";
833 const result = transformSync("foo;", {
834 env: {
835 foo: { comments: false },
836 },
837 });
838 expect(result.options.comments).toBe(false);
839 });
840 });
841
842 describe("buildExternalHelpers", function () {
843 describe("smoke tests", function () {
844 it("builds external helpers in global output type", function () {
845 babel.buildExternalHelpers(null, "global");
846 });
847
848 it("builds external helpers in module output type", function () {
849 babel.buildExternalHelpers(null, "module");
850 });
851
852 it("builds external helpers in umd output type", function () {
853 babel.buildExternalHelpers(null, "umd");
854 });
855
856 it("builds external helpers in var output type", function () {
857 babel.buildExternalHelpers(null, "var");
858 });
859 });
860
861 it("all", function () {
862 const script = babel.buildExternalHelpers();
863 expect(script).toEqual(expect.stringContaining("classCallCheck"));
864 expect(script).toEqual(expect.stringContaining("inherits"));
865 });
866
867 it("allowlist", function () {
868 const script = babel.buildExternalHelpers(["inherits"]);
869 expect(script).not.toEqual(expect.stringContaining("classCallCheck"));
870 expect(script).toEqual(expect.stringContaining("inherits"));
871 });
872
873 it("empty allowlist", function () {
874 const script = babel.buildExternalHelpers([]);
875 expect(script).not.toEqual(expect.stringContaining("classCallCheck"));
876 expect(script).not.toEqual(expect.stringContaining("inherits"));
877 });
878
879 it("underscored", function () {
880 const script = babel.buildExternalHelpers(["typeof"]);
881 expect(script).toEqual(expect.stringContaining("typeof"));
882 });
883 });
884
885 describe("handle parsing errors", function () {
886 const options = {
887 babelrc: false,
888 };
889
890 it("only syntax plugin available", function () {
891 return new Promise(resolve => {
892 transformFile(
893 cwd + "/fixtures/api/parsing-errors/only-syntax/file.js",
894 options,
895 function (err) {
896 expect(err.message).toMatch(
897 "Support for the experimental syntax 'pipelineOperator' isn't currently enabled (1:3):",
898 );
899 expect(err.message).toMatch(
900 "Add @babel/plugin-proposal-pipeline-operator (https://github.com/babel/babel/tree/main/packages/babel-plugin-proposal-pipeline-operator) to the " +
901 "'plugins' section of your Babel config to enable transformation.",
902 );
903 resolve();
904 },
905 );
906 });
907 });
908
909 it("both syntax and transform plugin available", function () {
910 return new Promise(resolve => {
911 transformFile(
912 cwd + "/fixtures/api/parsing-errors/syntax-and-transform/file.js",
913 options,
914 function (err) {
915 expect(err.message).toMatch(
916 "Support for the experimental syntax 'doExpressions' isn't currently enabled (1:2):",
917 );
918 expect(err.message).toMatch(
919 "Add @babel/plugin-proposal-do-expressions (https://github.com/babel/babel/tree/main/packages/babel-plugin-proposal-do-expressions) to the " +
920 "'plugins' section of your Babel config to enable transformation.",
921 );
922 resolve();
923 },
924 );
925 });
926 });
927 });
928
929 describe("missing helpers", function () {
930 it("should always throw", function () {
931 expect(() =>
932 babel.transformSync(``, {
933 configFile: false,
934 plugins: [
935 function () {
936 return {
937 visitor: {
938 Program(path) {
939 try {
940 path.pushContainer("body", this.addHelper("fooBar"));
941 } catch {}
942 path.pushContainer("body", this.addHelper("fooBar"));
943 },
944 },
945 };
946 },
947 ],
948 }),
949 ).toThrowErrorMatchingInlineSnapshot(
950 `"unknown file: Unknown helper fooBar"`,
951 );
952 });
953 });
954 });