Files
940b15aa3c [babel 8] Disallow synchronous usage of babel.* callback methods (#12695)
* [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 <[email protected]>
Co-authored-by: Nicolò Ribaudo <[email protected]>

* Comment out warning for now

Co-authored-by: Pranav <[email protected]>
Co-authored-by: Nicolò Ribaudo <[email protected]>
2022-06-05 21:26:11 +02:00

33 lines
1.1 KiB
JavaScript

import * as babel from "@babel/core";
import { fileURLToPath } from "url";
import path from "path";
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.transformSync(code, {
cwd: path.dirname(fileURLToPath(import.meta.url)),
ast: true,
plugins: [[transformCommonJS, { loose: true }]],
}).ast;
expect(ast.program.body[0].declarations[0].id.type).toBe("Identifier");
expect(ast.program.body[2].expression.type).toBe("MemberExpression");
expect(ast.program.body[2].expression.object.type).toBe("Identifier");
expect(ast.program.body[3].expression.type).toBe("MemberExpression");
expect(ast.program.body[3].expression.object.type).toBe("Identifier");
expect(ast.program.body[2].expression.object).not.toBe(
ast.program.body[3].expression.object,
);
expect(ast.program.body[0].declarations[0].id).not.toBe(
ast.program.body[3].expression.object,
);
expect(ast.program.body[0].declarations[0].id).not.toBe(
ast.program.body[2].expression.object,
);
});