Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cloneInputAst option to babel.transformFromAst #10241

Merged
merged 1 commit into from Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/babel-core/src/config/partial.js
Expand Up @@ -81,6 +81,7 @@ export default function* loadPrivatePartialConfig(
root: rootDir = ".",
rootMode = "root",
caller,
cloneInputAst = true,
} = args;
const absoluteCwd = path.resolve(cwd);
const absoluteRootDir = yield* resolveRootMode(
Expand Down Expand Up @@ -110,6 +111,7 @@ export default function* loadPrivatePartialConfig(
// Tack the passes onto the object itself so that, if this object is
// passed back to Babel a second time, it will be in the right structure
// to not change behavior.
options.cloneInputAst = cloneInputAst;
options.babelrc = false;
options.configFile = false;
options.passPerPreset = false;
Expand Down
5 changes: 5 additions & 0 deletions packages/babel-core/src/config/validation/options.js
Expand Up @@ -51,6 +51,10 @@ const ROOT_VALIDATORS: ValidatorSet = {
code: (assertBoolean: Validator<$PropertyType<ValidatedOptions, "code">>),
ast: (assertBoolean: Validator<$PropertyType<ValidatedOptions, "ast">>),

cloneInputAst: (assertBoolean: Validator<
$PropertyType<ValidatedOptions, "cloneInputAst">,
>),

envName: (assertString: Validator<
$PropertyType<ValidatedOptions, "envName">,
>),
Expand Down Expand Up @@ -184,6 +188,7 @@ export type ValidatedOptions = {
rootMode?: RootMode,
code?: boolean,
ast?: boolean,
cloneInputAst?: boolean,
inputSourceMap?: RootInputSourceMapOption,
envName?: string,
caller?: CallerMetadata,
Expand Down
6 changes: 5 additions & 1 deletion packages/babel-core/src/transformation/normalize-file.js
Expand Up @@ -34,7 +34,11 @@ export default function* normalizeFile(
} else if (ast.type !== "File") {
throw new Error("AST root must be a Program or File node");
}
ast = cloneDeep(ast);

const { cloneInputAst } = options;
if (cloneInputAst) {
ast = cloneDeep(ast);
}
} else {
ast = yield* parser(pluginPasses, options, code);
}
Expand Down
25 changes: 25 additions & 0 deletions packages/babel-core/test/api.js
Expand Up @@ -214,6 +214,31 @@ describe("api", function () {
);
});

it("transformFromAst should mutate the AST when cloneInputAst is false", function () {
const program = "const identifier = 1";
const node = parse(program);
const { code } = transformFromAst(node, program, {
cloneInputAst: false,
plugins: [
function () {
return {
visitor: {
Identifier: function (path) {
path.node.name = "replaced";
},
},
};
},
],
});

expect(code).toBe("const replaced = 1;");
expect(node.program.body[0].declarations[0].id.name).toBe(
"replaced",
"original ast should have been mutated",
);
});

it("options throw on falsy true", function () {
return expect(function () {
transform("", {
Expand Down
1 change: 1 addition & 0 deletions packages/babel-core/test/config-chain.js
Expand Up @@ -984,6 +984,7 @@ describe("buildConfigChain", function () {
passPerPreset: false,
plugins: [],
presets: [],
cloneInputAst: true,
});
const realEnv = process.env.NODE_ENV;
const realBabelEnv = process.env.BABEL_ENV;
Expand Down