From 2ce56e832c2dd7a7ed92c89028ba929f874c2f5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Sun, 5 Jun 2022 15:26:11 -0400 Subject: [PATCH] [babel 8] Disallow synchronous usage of `babel.*` callback methods (#12695) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [babel 8] Disallow synchronous usage of `babel.{parse,transform{,FromAst,File}}` Disallow babel.transformFromAst synchronously Disallow using babel.transform, babel.transformFile, babel.parse synchronously chore: throw error behind BABEL_8_BREAKING flag fix: use transformFromAstSync fix: use transformSync refactor: use Babel.transformSync in standalone fix: use parseSync feat: emit deprecation message on callback-less usage chore: add tests on thrown error call sync method on undefined callback review comments address review comments chore: use loadOptionsSync for OptionManager Co-authored-by: Huáng Jùnliàng Co-authored-by: Nicolò Ribaudo * Comment out warning for now Co-authored-by: Pranav Co-authored-by: Nicolò Ribaudo --- packages/babel-core/src/config/full.ts | 2 +- packages/babel-core/src/index.ts | 4 +- packages/babel-core/src/parse.ts | 15 ++- packages/babel-core/src/transform-ast.ts | 13 ++- packages/babel-core/src/transform.ts | 15 ++- packages/babel-core/test/api.js | 104 ++++++++++++++---- packages/babel-core/test/parse.js | 8 +- packages/babel-core/test/path.js | 16 +-- packages/babel-core/test/resolution.js | 80 +++++++------- .../babel-helper-module-imports/test/index.js | 2 +- .../src/index.ts | 6 +- packages/babel-node/src/_babel-node.ts | 2 +- .../test/plugin-ordering.test.js | 2 +- .../test/index.js | 4 +- .../test/copied-nodes.js | 2 +- .../test/esmodule-flag.js | 2 +- .../scripts/build-dist.js | 2 +- .../babel-standalone/examples/example.htm | 76 ++++++------- packages/babel-standalone/src/index.ts | 8 +- 19 files changed, 225 insertions(+), 138 deletions(-) diff --git a/packages/babel-core/src/config/full.ts b/packages/babel-core/src/config/full.ts index d5f5a165ad62..7121d4892902 100644 --- a/packages/babel-core/src/config/full.ts +++ b/packages/babel-core/src/config/full.ts @@ -430,7 +430,7 @@ const validateIfOptionNeedsFilename = ( [ `Preset ${formattedPresetName} requires a filename to be set when babel is called directly,`, `\`\`\``, - `babel.transform(code, { filename: 'file.ts', presets: [${formattedPresetName}] });`, + `babel.transformSync(code, { filename: 'file.ts', presets: [${formattedPresetName}] });`, `\`\`\``, `See https://babeljs.io/docs/en/options#filename for more information.`, ].join("\n"), diff --git a/packages/babel-core/src/index.ts b/packages/babel-core/src/index.ts index cdbc2e6998bb..676662749ec5 100644 --- a/packages/babel-core/src/index.ts +++ b/packages/babel-core/src/index.ts @@ -63,10 +63,10 @@ export const DEFAULT_EXTENSIONS = Object.freeze([ ] as const); // For easier backward-compatibility, provide an API like the one we exposed in Babel 6. -import { loadOptions } from "./config"; +import { loadOptionsSync } from "./config"; export class OptionManager { init(opts: {}) { - return loadOptions(opts); + return loadOptionsSync(opts); } } diff --git a/packages/babel-core/src/parse.ts b/packages/babel-core/src/parse.ts index 79fd13ffe209..38765f06eebf 100644 --- a/packages/babel-core/src/parse.ts +++ b/packages/babel-core/src/parse.ts @@ -39,9 +39,18 @@ export const parse: Parse = function parse(code, opts?, callback?) { opts = undefined; } - // For backward-compat with Babel 7's early betas, we allow sync parsing when - // no callback is given. Will be dropped in some future Babel major version. - if (callback === undefined) return parseRunner.sync(code, opts); + if (callback === undefined) { + if (process.env.BABEL_8_BREAKING) { + throw new Error( + "Starting from Babel 8.0.0, the 'parse' function expects a callback. If you need to call it synchronously, please use 'parseSync'.", + ); + } else { + // console.warn( + // "Starting from Babel 8.0.0, the 'parse' function will expect a callback. If you need to call it synchronously, please use 'parseSync'.", + // ); + return parseRunner.sync(code, opts); + } + } parseRunner.errback(code, opts, callback); }; diff --git a/packages/babel-core/src/transform-ast.ts b/packages/babel-core/src/transform-ast.ts index 5a482dead628..d42dbb79913f 100644 --- a/packages/babel-core/src/transform-ast.ts +++ b/packages/babel-core/src/transform-ast.ts @@ -45,10 +45,17 @@ export const transformFromAst: TransformFromAst = function transformFromAst( opts = undefined; } - // For backward-compat with Babel 6, we allow sync transformation when - // no callback is given. Will be dropped in some future Babel major version. if (callback === undefined) { - return transformFromAstRunner.sync(ast, code, opts); + if (process.env.BABEL_8_BREAKING) { + throw new Error( + "Starting from Babel 8.0.0, the 'transformFromAst' function expects a callback. If you need to call it synchronously, please use 'transformFromAstSync'.", + ); + } else { + // console.warn( + // "Starting from Babel 8.0.0, the 'transformFromAst' function will expect a callback. If you need to call it synchronously, please use 'transformFromAstSync'.", + // ); + return transformFromAstRunner.sync(ast, code, opts); + } } transformFromAstRunner.errback(ast, code, opts, callback); diff --git a/packages/babel-core/src/transform.ts b/packages/babel-core/src/transform.ts index 5bc4fca5ab4a..668b20de5ab7 100644 --- a/packages/babel-core/src/transform.ts +++ b/packages/babel-core/src/transform.ts @@ -31,9 +31,18 @@ export const transform: Transform = function transform(code, opts?, callback?) { opts = undefined; } - // For backward-compat with Babel 6, we allow sync transformation when - // no callback is given. Will be dropped in some future Babel major version. - if (callback === undefined) return transformRunner.sync(code, opts); + if (callback === undefined) { + if (process.env.BABEL_8_BREAKING) { + throw new Error( + "Starting from Babel 8.0.0, the 'transform' function expects a callback. If you need to call it synchronously, please use 'transformSync'.", + ); + } else { + // console.warn( + // "Starting from Babel 8.0.0, the 'transform' function will expect a callback. If you need to call it synchronously, please use 'transformSync'.", + // ); + return transformRunner.sync(code, opts); + } + } transformRunner.errback(code, opts, callback); }; diff --git a/packages/babel-core/test/api.js b/packages/babel-core/test/api.js index c714a7516df2..39ec12d2740e 100644 --- a/packages/babel-core/test/api.js +++ b/packages/babel-core/test/api.js @@ -25,10 +25,18 @@ function parse(code, opts) { return babel.parse(code, { cwd, configFile: false, ...opts }); } +function parseSync(code, opts) { + return babel.parseSync(code, { cwd, configFile: false, ...opts }); +} + function transform(code, opts) { return babel.transform(code, { cwd, configFile: false, ...opts }); } +function transformSync(code, opts) { + return babel.transformSync(code, { cwd, configFile: false, ...opts }); +} + function transformFile(filename, opts, cb) { return babel.transformFile(filename, { cwd, configFile: false, ...opts }, cb); } @@ -51,6 +59,14 @@ function transformFromAst(ast, code, opts) { return babel.transformFromAst(ast, code, { cwd, configFile: false, ...opts }); } +function transformFromAstSync(ast, code, opts) { + return babel.transformFromAstSync(ast, code, { + cwd, + configFile: false, + ...opts, + }); +} + describe("parser and generator options", function () { const recast = { parse: function (code, opts) { @@ -62,7 +78,7 @@ describe("parser and generator options", function () { }; function newTransform(string) { - return transform(string, { + return transformSync(string, { ast: true, parserOpts: { parser: recast.parse, @@ -78,7 +94,7 @@ describe("parser and generator options", function () { it("options", function () { const string = "original;"; expect(newTransform(string).ast).toEqual( - transform(string, { ast: true }).ast, + transformSync(string, { ast: true }).ast, ); expect(newTransform(string).code).toBe(string); }); @@ -87,7 +103,7 @@ describe("parser and generator options", function () { const experimental = "var a: number = 1;"; expect(newTransform(experimental).ast).toEqual( - transform(experimental, { + transformSync(experimental, { ast: true, parserOpts: { plugins: ["flow"], @@ -97,7 +113,7 @@ describe("parser and generator options", function () { expect(newTransform(experimental).code).toBe(experimental); function newTransformWithPlugins(string) { - return transform(string, { + return transformSync(string, { ast: true, plugins: [cwd + "/../../babel-plugin-syntax-flow"], parserOpts: { @@ -110,7 +126,7 @@ describe("parser and generator options", function () { } expect(newTransformWithPlugins(experimental).ast).toEqual( - transform(experimental, { + transformSync(experimental, { ast: true, parserOpts: { plugins: ["flow"], @@ -124,7 +140,7 @@ describe("parser and generator options", function () { const experimental = "if (true) {\n import a from 'a';\n}"; expect(newTransform(experimental).ast).not.toBe( - transform(experimental, { + transformSync(experimental, { ast: true, parserOpts: { allowImportExportEverywhere: true, @@ -156,6 +172,27 @@ describe("api", function () { expect(babel.tokTypes).toBeDefined(); }); + (process.env.BABEL_8_BREAKING ? it : it.skip)( + "parse throws on undefined callback", + () => { + expect(() => parse("", {})).toThrowErrorMatchingInlineSnapshot( + `"Starting from Babel 8.0.0, the 'parse' function expects a callback. If you need to call it synchronously, please use 'parseSync'."`, + ); + }, + ); + + (process.env.BABEL_8_BREAKING ? it : it.skip)( + "transform throws on undefined callback", + () => { + const options = { + filename: "example.js", + }; + expect(() => transform("", options)).toThrowErrorMatchingInlineSnapshot( + `"Starting from Babel 8.0.0, the 'transform' function expects a callback. If you need to call it synchronously, please use 'transformSync'."`, + ); + }, + ); + it("transformFile", function () { const options = { babelrc: false, @@ -189,6 +226,16 @@ describe("api", function () { // keep user options untouched expect(options).toEqual({ babelrc: false }); }); + it("transformFile throws on undefined callback", () => { + const options = { + babelrc: false, + }; + expect(() => + transformFile(cwd + "/fixtures/api/file.js", options), + ).toThrowErrorMatchingInlineSnapshot( + `"Asynchronous function called without callback"`, + ); + }); it("transformFileSync", function () { const options = { @@ -201,10 +248,23 @@ describe("api", function () { expect(options).toEqual({ babelrc: false }); }); - it("transformFromAst should not mutate the AST", function () { + (process.env.BABEL_8_BREAKING ? it : it.skip)( + "transformFromAst throws on undefined callback", + () => { + const program = "const identifier = 1"; + const node = parseSync(program); + expect(() => + transformFromAst(node, program), + ).toThrowErrorMatchingInlineSnapshot( + `"Starting from Babel 8.0.0, the 'transformFromAst' function expects a callback. If you need to call it synchronously, please use 'transformFromAstSync'."`, + ); + }, + ); + + it("transformFromAstSync should not mutate the AST", function () { const program = "const identifier = 1"; - const node = parse(program); - const { code } = transformFromAst(node, program, { + const node = parseSync(program); + const { code } = transformFromAstSync(node, program, { plugins: [ function () { return { @@ -225,10 +285,10 @@ describe("api", function () { ); }); - it("transformFromAst should mutate the AST when cloneInputAst is false", function () { + it("transformFromAstSync should mutate the AST when cloneInputAst is false", function () { const program = "const identifier = 1"; - const node = parse(program); - const { code } = transformFromAst(node, program, { + const node = parseSync(program); + const { code } = transformFromAstSync(node, program, { cloneInputAst: false, plugins: [ function () { @@ -252,7 +312,7 @@ describe("api", function () { it("options throw on falsy true", function () { return expect(function () { - transform("", { + transformSync("", { plugins: [cwd + "/../../babel-plugin-syntax-jsx", false], }); }).toThrow(/.plugins\[1\] must be a string, object, function/); @@ -273,7 +333,7 @@ describe("api", function () { let calledRaw = 0; let calledIntercept = 0; - transform("function foo() { bar(foobar); }", { + transformSync("function foo() { bar(foobar); }", { wrapPluginVisitorMethod: function (pluginAlias, visitorType, callback) { if (pluginAlias !== "foobar") { return callback; @@ -307,7 +367,7 @@ describe("api", function () { let aliasBaseType = null; function execTest(passPerPreset) { - return transform("type Foo = number; let x = (y): Foo => y;", { + return transformSync("type Foo = number; let x = (y): Foo => y;", { sourceType: "script", passPerPreset: passPerPreset, presets: [ @@ -390,7 +450,7 @@ describe("api", function () { const oldEnv = process.env.BABEL_ENV; process.env.BABEL_ENV = "development"; - const result = transform("", { + const result = transformSync("", { cwd: path.join(cwd, "fixtures", "config", "complex-plugin-config"), filename: path.join( cwd, @@ -448,7 +508,7 @@ describe("api", function () { it("interpreter directive backward-compat", function () { function doTransform(code, preHandler) { - return transform(code, { + return transformSync(code, { plugins: [ { pre: preHandler, @@ -501,7 +561,7 @@ describe("api", function () { }); it("source map merging", function () { - const result = transform( + const result = transformSync( [ /* eslint-disable max-len */ 'function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }', @@ -710,7 +770,7 @@ describe("api", function () { }); it("default", function () { - const result = transform("foo;", { + const result = transformSync("foo;", { env: { development: { comments: false }, }, @@ -721,7 +781,7 @@ describe("api", function () { it("BABEL_ENV", function () { process.env.BABEL_ENV = "foo"; - const result = transform("foo;", { + const result = transformSync("foo;", { env: { foo: { comments: false }, }, @@ -731,7 +791,7 @@ describe("api", function () { it("NODE_ENV", function () { process.env.NODE_ENV = "foo"; - const result = transform("foo;", { + const result = transformSync("foo;", { env: { foo: { comments: false }, }, @@ -847,7 +907,7 @@ describe("api", function () { }, ], }), - ).toThrow(); + ).toThrowErrorMatchingInlineSnapshot(`"unknown: Unknown helper fooBar"`); }); }); }); diff --git a/packages/babel-core/test/parse.js b/packages/babel-core/test/parse.js index 244b380a339e..eeec4ce7869d 100644 --- a/packages/babel-core/test/parse.js +++ b/packages/babel-core/test/parse.js @@ -1,6 +1,6 @@ import fs from "fs"; import path from "path"; -import { parse } from "../lib/index.js"; +import { parseSync } from "../lib/index.js"; import { fileURLToPath } from "url"; import { createRequire } from "module"; @@ -15,12 +15,12 @@ function fixture(...args) { ); } -describe("parse", function () { +describe("parseSync", function () { it("should parse using configuration from .babelrc when a filename is provided", function () { const input = fs.readFileSync(fixture("input.js"), "utf8"); const output = require(fixture("output")); - const result = parse(input, { + const result = parseSync(input, { filename: fixture("input.js"), cwd: fixture(), }); @@ -31,7 +31,7 @@ describe("parse", function () { const input = fs.readFileSync(fixture("input.js"), "utf8"); const output = require(fixture("output.json")); - const result = parse(input, { + const result = parseSync(input, { parserOpts: { plugins: [["decorators", { decoratorsBeforeExport: false }]], }, diff --git a/packages/babel-core/test/path.js b/packages/babel-core/test/path.js index 1ef571ae1464..a70d5dac6b1d 100644 --- a/packages/babel-core/test/path.js +++ b/packages/babel-core/test/path.js @@ -1,4 +1,4 @@ -import { transform } from "../lib/index.js"; +import { transformSync } from "../lib/index.js"; import { fileURLToPath } from "url"; import path from "path"; @@ -11,7 +11,7 @@ describe("traversal path", function () { it("replaceWithSourceString", function () { const expectCode = "function foo() {}"; - const actualCode = transform(expectCode, { + const actualCode = transformSync(expectCode, { cwd, plugins: [ new Plugin({ @@ -30,7 +30,7 @@ describe("traversal path", function () { it("replaceWith (arrow expression body to block statement body)", function () { const expectCode = "var fn = () => true;"; - const actualCode = transform(expectCode, { + const actualCode = transformSync(expectCode, { cwd, plugins: [ new Plugin({ @@ -60,7 +60,7 @@ describe("traversal path", function () { it("replaceWith (arrow block statement body to expression body)", function () { const expectCode = "var fn = () => { return true; }"; - const actualCode = transform(expectCode, { + const actualCode = transformSync(expectCode, { cwd, plugins: [ new Plugin({ @@ -82,7 +82,7 @@ describe("traversal path", function () { it("replaceWith (for-in left expression to variable declaration)", function () { const expectCode = "for (KEY in right);"; - const actualCode = transform(expectCode, { + const actualCode = transformSync(expectCode, { cwd, plugins: [ new Plugin({ @@ -113,7 +113,7 @@ describe("traversal path", function () { it("replaceWith (for-in left variable declaration to expression)", function () { const expectCode = "for (var KEY in right);"; - const actualCode = transform(expectCode, { + const actualCode = transformSync(expectCode, { cwd, plugins: [ new Plugin({ @@ -135,7 +135,7 @@ describe("traversal path", function () { it("replaceWith (for-loop left expression to variable declaration)", function () { const expectCode = "for (KEY;;);"; - const actualCode = transform(expectCode, { + const actualCode = transformSync(expectCode, { cwd, plugins: [ new Plugin({ @@ -166,7 +166,7 @@ describe("traversal path", function () { it("replaceWith (for-loop left variable declaration to expression)", function () { const expectCode = "for (var KEY;;);"; - const actualCode = transform(expectCode, { + const actualCode = transformSync(expectCode, { cwd, plugins: [ new Plugin({ diff --git a/packages/babel-core/test/resolution.js b/packages/babel-core/test/resolution.js index a719cd92e1ec..614ed63c6e38 100644 --- a/packages/babel-core/test/resolution.js +++ b/packages/babel-core/test/resolution.js @@ -22,7 +22,7 @@ describe("addon resolution", function () { it("should find module: presets", function () { process.chdir("module-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, presets: ["module:preset"], @@ -32,7 +32,7 @@ describe("addon resolution", function () { it("should find module: plugins", function () { process.chdir("module-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, plugins: ["module:plugin"], @@ -42,7 +42,7 @@ describe("addon resolution", function () { it("should find standard presets", function () { process.chdir("standard-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, presets: ["mod"], @@ -52,7 +52,7 @@ describe("addon resolution", function () { it("should find standard plugins", function () { process.chdir("standard-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, plugins: ["mod"], @@ -62,7 +62,7 @@ describe("addon resolution", function () { it("should find standard presets with an existing prefix", function () { process.chdir("standard-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, presets: ["babel-preset-mod"], @@ -72,7 +72,7 @@ describe("addon resolution", function () { it("should find standard plugins with an existing prefix", function () { process.chdir("standard-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, plugins: ["babel-plugin-mod"], @@ -82,7 +82,7 @@ describe("addon resolution", function () { it("should find @babel scoped presets", function () { process.chdir("babel-org-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, presets: ["@babel/foo"], @@ -92,7 +92,7 @@ describe("addon resolution", function () { it("should find @babel scoped plugins", function () { process.chdir("babel-org-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, plugins: ["@babel/foo"], @@ -102,7 +102,7 @@ describe("addon resolution", function () { it("should find @babel scoped presets with an existing prefix", function () { process.chdir("babel-org-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, presets: ["@babel/preset-foo"], @@ -112,7 +112,7 @@ describe("addon resolution", function () { it("should find @babel scoped plugins", function () { process.chdir("babel-org-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, plugins: ["@babel/plugin-foo"], @@ -122,7 +122,7 @@ describe("addon resolution", function () { it("should find @foo scoped presets", function () { process.chdir("foo-org-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, presets: ["@foo/mod"], @@ -132,7 +132,7 @@ describe("addon resolution", function () { it("should find @foo scoped plugins", function () { process.chdir("foo-org-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, plugins: ["@foo/mod"], @@ -142,7 +142,7 @@ describe("addon resolution", function () { it("should find @foo scoped presets with an inner babel-preset", function () { process.chdir("foo-org-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, presets: ["@foo/thing.babel-preset-convert"], @@ -152,7 +152,7 @@ describe("addon resolution", function () { it("should find @foo scoped plugins with an inner babel-plugin", function () { process.chdir("foo-org-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, plugins: ["@foo/thing.babel-plugin-convert"], @@ -162,7 +162,7 @@ describe("addon resolution", function () { it("should find @foo scoped presets with an babel-preset suffix", function () { process.chdir("foo-org-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, presets: ["@foo/thing-babel-preset"], @@ -172,7 +172,7 @@ describe("addon resolution", function () { it("should find @foo scoped plugins with an babel-plugin suffix", function () { process.chdir("foo-org-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, plugins: ["@foo/thing-babel-plugin"], @@ -182,7 +182,7 @@ describe("addon resolution", function () { it("should find @foo scoped presets with an existing prefix", function () { process.chdir("foo-org-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, presets: ["@foo/babel-preset-mod"], @@ -192,7 +192,7 @@ describe("addon resolution", function () { it("should find @foo scoped plugins with an existing prefix", function () { process.chdir("foo-org-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, plugins: ["@foo/babel-plugin-mod"], @@ -202,7 +202,7 @@ describe("addon resolution", function () { it("should find @foo/babel-plugin when specified", function () { process.chdir("foo-org-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, plugins: ["@foo/babel-plugin"], @@ -212,7 +212,7 @@ describe("addon resolution", function () { it("should find @foo/babel-preset when specified", function () { process.chdir("foo-org-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, presets: ["@foo/babel-preset"], @@ -222,7 +222,7 @@ describe("addon resolution", function () { it("should find @foo/babel-plugin/index when specified", function () { process.chdir("foo-org-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, plugins: ["@foo/babel-plugin/index"], @@ -232,7 +232,7 @@ describe("addon resolution", function () { it("should find @foo/babel-preset/index when specified", function () { process.chdir("foo-org-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, presets: ["@foo/babel-preset/index"], @@ -242,7 +242,7 @@ describe("addon resolution", function () { it("should find @foo/babel-plugin when just scope given", function () { process.chdir("foo-org-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, plugins: ["@foo"], @@ -252,7 +252,7 @@ describe("addon resolution", function () { it("should find @foo/babel-preset when just scope given", function () { process.chdir("foo-org-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, presets: ["@foo"], @@ -262,7 +262,7 @@ describe("addon resolution", function () { it("should find relative path presets", function () { process.chdir("relative-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, presets: ["./dir/preset.js"], @@ -272,7 +272,7 @@ describe("addon resolution", function () { it("should find relative path plugins", function () { process.chdir("relative-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, plugins: ["./dir/plugin.js"], @@ -282,7 +282,7 @@ describe("addon resolution", function () { it("should find module file presets", function () { process.chdir("nested-module-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, presets: ["mod/preset"], @@ -292,7 +292,7 @@ describe("addon resolution", function () { it("should find module file plugins", function () { process.chdir("nested-module-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, plugins: ["mod/plugin"], @@ -302,7 +302,7 @@ describe("addon resolution", function () { it("should find @foo scoped module file presets", function () { process.chdir("scoped-nested-module-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, presets: ["@foo/mod/preset"], @@ -312,7 +312,7 @@ describe("addon resolution", function () { it("should find @foo scoped module file plugins", function () { process.chdir("scoped-nested-module-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, plugins: ["@foo/mod/plugin"], @@ -322,7 +322,7 @@ describe("addon resolution", function () { it("should find @babel scoped module file presets", function () { process.chdir("babel-scoped-nested-module-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, presets: ["@babel/mod/preset"], @@ -332,7 +332,7 @@ describe("addon resolution", function () { it("should find @babel scoped module file plugins", function () { process.chdir("babel-scoped-nested-module-paths"); - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, plugins: ["@babel/mod/plugin"], @@ -344,7 +344,7 @@ describe("addon resolution", function () { process.chdir("throw-module-paths"); expect(() => { - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, presets: ["foo"], @@ -364,7 +364,7 @@ describe("addon resolution", function () { process.chdir("throw-module-paths"); expect(() => { - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, plugins: ["foo"], @@ -380,7 +380,7 @@ describe("addon resolution", function () { process.chdir("throw-babel-paths"); expect(() => { - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, presets: ["foo"], @@ -396,7 +396,7 @@ describe("addon resolution", function () { process.chdir("throw-babel-paths"); expect(() => { - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, plugins: ["foo"], @@ -412,7 +412,7 @@ describe("addon resolution", function () { process.chdir("throw-opposite-paths"); expect(() => { - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, presets: ["testplugin"], @@ -428,7 +428,7 @@ describe("addon resolution", function () { process.chdir("throw-opposite-paths"); expect(() => { - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, plugins: ["testpreset"], @@ -444,7 +444,7 @@ describe("addon resolution", function () { process.chdir("throw-missing-paths"); expect(() => { - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, presets: ["foo"], @@ -456,7 +456,7 @@ describe("addon resolution", function () { process.chdir("throw-missing-paths"); expect(() => { - babel.transform("", { + babel.transformSync("", { filename: "filename.js", babelrc: false, plugins: ["foo"], diff --git a/packages/babel-helper-module-imports/test/index.js b/packages/babel-helper-module-imports/test/index.js index e322262303c3..2c81447354a4 100644 --- a/packages/babel-helper-module-imports/test/index.js +++ b/packages/babel-helper-module-imports/test/index.js @@ -18,7 +18,7 @@ function test(sourceType, opts, initializer, inputCode, expectedCode) { inputCode = ""; } - const result = babel.transform(inputCode, { + const result = babel.transformSync(inputCode, { cwd, sourceType, filename: "example" + (sourceType === "module" ? ".mjs" : ".js"), diff --git a/packages/babel-helper-transform-fixture-test-runner/src/index.ts b/packages/babel-helper-transform-fixture-test-runner/src/index.ts index b3274a737fc9..be7fa4c40960 100644 --- a/packages/babel-helper-transform-fixture-test-runner/src/index.ts +++ b/packages/babel-helper-transform-fixture-test-runner/src/index.ts @@ -34,7 +34,7 @@ const sharedTestContext = createContext(); // default. Tests can still set `configFile: true | string` // to re-enable config loading. function transformWithoutConfigFile(code, opts) { - return babel.transform(code, { + return babel.transformSync(code, { configFile: false, babelrc: false, ...opts, @@ -257,7 +257,7 @@ function run(task) { // Ignore Babel logs of exec.js files. // They will be validated in input/output files. ({ result } = maybeMockConsole(validateLogs, () => - babel.transform(execCode, execOpts), + babel.transformSync(execCode, execOpts), )); checkDuplicateNodes(result.ast); @@ -279,7 +279,7 @@ function run(task) { let actualLogs; ({ result, actualLogs } = maybeMockConsole(validateLogs, () => - babel.transform(inputCode, getOpts(actual)), + babel.transformSync(inputCode, getOpts(actual)), )); const outputCode = normalizeOutput(result.code); diff --git a/packages/babel-node/src/_babel-node.ts b/packages/babel-node/src/_babel-node.ts index 3e2fee2b3e9e..979ec4433816 100644 --- a/packages/babel-node/src/_babel-node.ts +++ b/packages/babel-node/src/_babel-node.ts @@ -130,7 +130,7 @@ const _eval = function (code, filename) { code = code.trim(); if (!code) return undefined; - code = babel.transform(code, { + code = babel.transformSync(code, { filename: filename, presets: program.presets, plugins: (program.plugins || []).concat([replPlugin]), diff --git a/packages/babel-plugin-proposal-class-static-block/test/plugin-ordering.test.js b/packages/babel-plugin-proposal-class-static-block/test/plugin-ordering.test.js index 9a3da5cc687e..72505b9d639d 100644 --- a/packages/babel-plugin-proposal-class-static-block/test/plugin-ordering.test.js +++ b/packages/babel-plugin-proposal-class-static-block/test/plugin-ordering.test.js @@ -11,7 +11,7 @@ describe("plugin ordering", () => { } `; expect( - babel.transform(source, { + babel.transformSync(source, { filename: "example.js", highlightCode: false, configFile: false, diff --git a/packages/babel-plugin-syntax-decorators/test/index.js b/packages/babel-plugin-syntax-decorators/test/index.js index afe726ab985c..286cb4dbefb6 100644 --- a/packages/babel-plugin-syntax-decorators/test/index.js +++ b/packages/babel-plugin-syntax-decorators/test/index.js @@ -1,9 +1,9 @@ -import { parse } from "@babel/core"; +import { parseSync } from "@babel/core"; import syntaxDecorators from "../lib/index.js"; function makeParser(code, options) { return () => - parse(code, { + parseSync(code, { babelrc: false, configFile: false, plugins: [[syntaxDecorators, options]], diff --git a/packages/babel-plugin-transform-modules-commonjs/test/copied-nodes.js b/packages/babel-plugin-transform-modules-commonjs/test/copied-nodes.js index ac37d47669b1..3810c6ff1c0a 100644 --- a/packages/babel-plugin-transform-modules-commonjs/test/copied-nodes.js +++ b/packages/babel-plugin-transform-modules-commonjs/test/copied-nodes.js @@ -7,7 +7,7 @@ import transformCommonJS from "../lib/index.js"; test("Doesn't use the same object for two different nodes in the AST", function () { const code = 'import Foo from "bar"; Foo; Foo;'; - const ast = babel.transform(code, { + const ast = babel.transformSync(code, { cwd: path.dirname(fileURLToPath(import.meta.url)), ast: true, plugins: [[transformCommonJS, { loose: true }]], diff --git a/packages/babel-plugin-transform-modules-commonjs/test/esmodule-flag.js b/packages/babel-plugin-transform-modules-commonjs/test/esmodule-flag.js index 7d84cbe73559..8b01fec4b6ed 100644 --- a/packages/babel-plugin-transform-modules-commonjs/test/esmodule-flag.js +++ b/packages/babel-plugin-transform-modules-commonjs/test/esmodule-flag.js @@ -22,7 +22,7 @@ test("Re-export doesn't overwrite __esModule flag", function () { }; context.exports = context.module.exports; - code = babel.transform(code, { + code = babel.transformSync(code, { cwd: path.dirname(fileURLToPath(import.meta.url)), plugins: [[transformCommonJS, { loose: true }]], ast: false, diff --git a/packages/babel-plugin-transform-runtime/scripts/build-dist.js b/packages/babel-plugin-transform-runtime/scripts/build-dist.js index 3f9dd2da3bda..61570a0ab7ff 100644 --- a/packages/babel-plugin-transform-runtime/scripts/build-dist.js +++ b/packages/babel-plugin-transform-runtime/scripts/build-dist.js @@ -235,7 +235,7 @@ function buildHelper( ); tree.body.push(...helper.nodes); - return babel.transformFromAst(tree, null, { + return babel.transformFromAstSync(tree, null, { filename: helperFilename, presets: [["@babel/preset-env", { modules: false }]], plugins: [ diff --git a/packages/babel-standalone/examples/example.htm b/packages/babel-standalone/examples/example.htm index cdbda4cde166..157161c2d60d 100644 --- a/packages/babel-standalone/examples/example.htm +++ b/packages/babel-standalone/examples/example.htm @@ -1,48 +1,50 @@ - - - babel-standalone example - - - Input: - + - Transformed code using Babel : -
Loading...
+ Transformed code using Babel : +
Loading...
- - + + - inputEl.addEventListener("keyup", transform, false); - transform(); - - diff --git a/packages/babel-standalone/src/index.ts b/packages/babel-standalone/src/index.ts index 11074f3422ef..1b2ae5ee9b17 100644 --- a/packages/babel-standalone/src/index.ts +++ b/packages/babel-standalone/src/index.ts @@ -12,8 +12,8 @@ /// import { - transformFromAst as babelTransformFromAst, - transform as babelTransform, + transformFromAstSync as babelTransformFromAstSync, + transformSync as babelTransformSync, buildExternalHelpers as babelBuildExternalHelpers, } from "@babel/core"; import { all } from "./generated/plugins"; @@ -101,11 +101,11 @@ function processOptions(options) { } export function transform(code: string, options: any) { - return babelTransform(code, processOptions(options)); + return babelTransformSync(code, processOptions(options)); } export function transformFromAst(ast: any, code: string, options: any) { - return babelTransformFromAst(ast, code, processOptions(options)); + return babelTransformFromAstSync(ast, code, processOptions(options)); } export const availablePlugins = {}; export const availablePresets = {};