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

chore: Helper changes for Babel 8 #16209

Merged
merged 2 commits into from Jan 31, 2024
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
106 changes: 47 additions & 59 deletions packages/babel-core/src/transformation/file/file.ts
Expand Up @@ -10,7 +10,7 @@ import semver from "semver";

import type { NormalizedFile } from "../normalize-file.ts";

const errorVisitor: Visitor<{ loc: NodeLocation["loc"] | null }> = {
const errorVisitor: Visitor<{ loc: t.SourceLocation | null }> = {
enter(path, state) {
const loc = path.node.loc;
if (loc) {
Expand All @@ -20,29 +20,6 @@ const errorVisitor: Visitor<{ loc: NodeLocation["loc"] | null }> = {
},
};

export type NodeLocation = {
loc?: {
end?: {
line: number;
column: number;
};
start: {
line: number;
column: number;
};
};
_loc?: {
end?: {
line: number;
column: number;
};
start: {
line: number;
column: number;
};
};
};

export default class File {
_map: Map<unknown, unknown> = new Map();
opts: { [key: string]: any };
Expand Down Expand Up @@ -97,14 +74,16 @@ export default class File {
}

set(key: unknown, val: unknown) {
if (key === "helpersNamespace") {
throw new Error(
"Babel 7.0.0-beta.56 has dropped support for the 'helpersNamespace' utility." +
"If you are using @babel/plugin-external-helpers you will need to use a newer " +
"version than the one you currently have installed. " +
"If you have your own implementation, you'll want to explore using 'helperGenerator' " +
"alongside 'file.availableHelper()'.",
);
if (!process.env.BABEL_8_BREAKING) {
if (key === "helpersNamespace") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reminding!
This is a special case. :)

function pluginToggleBooleanFlag({ types: t }, { name, value }) {

We use a custom plugin to remove it on release.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, since #15961 the custom plugin should support doing

if (!process.env.BABEL_8_BREAKING && key === "helpersNamespace") {

:)

throw new Error(
"Babel 7.0.0-beta.56 has dropped support for the 'helpersNamespace' utility." +
"If you are using @babel/plugin-external-helpers you will need to use a newer " +
"version than the one you currently have installed. " +
"If you have your own implementation, you'll want to explore using 'helperGenerator' " +
"alongside 'file.availableHelper()'.",
);
}
}

this._map.set(key, val);
Expand All @@ -122,15 +101,6 @@ export default class File {
return getModuleName(this.opts, this.opts);
}

addImport() {
throw new Error(
"This API has been removed. If you're looking for this " +
"functionality in Babel 7, you should import the " +
"'@babel/helper-module-imports' module and use the functions exposed " +
" from that module, such as 'addNamed' or 'addDefault'.",
);
}

/**
* Check if a given helper is available in @babel/core's helper list.
*
Expand Down Expand Up @@ -169,10 +139,17 @@ export default class File {
// transform-runtime's definitions.js file.
if (semver.valid(versionRange)) versionRange = `^${versionRange}`;

return (
!semver.intersects(`<${minVersion}`, versionRange) &&
!semver.intersects(`>=8.0.0`, versionRange)
);
if (process.env.BABEL_8_BREAKING) {
return (
!semver.intersects(`<${minVersion}`, versionRange) &&
!semver.intersects(`>=9.0.0`, versionRange)
);
} else {
return (
!semver.intersects(`<${minVersion}`, versionRange) &&
!semver.intersects(`>=8.0.0`, versionRange)
);
}
}

addHelper(name: string): t.Identifier {
Expand Down Expand Up @@ -214,35 +191,28 @@ export default class File {
node._compact = true;
});

this.path.unshiftContainer("body", nodes);
const added = this.path.unshiftContainer("body", nodes);
// TODO: NodePath#unshiftContainer should automatically register new
// bindings.
this.path.get("body").forEach(path => {
if (nodes.indexOf(path.node) === -1) return;
for (const path of added) {
if (path.isVariableDeclaration()) this.scope.registerDeclaration(path);
});
}

return uid;
}

addTemplateObject() {
throw new Error(
"This function has been moved into the template literal transform itself.",
);
}

buildCodeFrameError(
node: NodeLocation | undefined | null,
node: t.Node | undefined | null,
msg: string,
_Error: typeof Error = SyntaxError,
): Error {
let loc = node && (node.loc || node._loc);
let loc = node?.loc;

if (!loc && node) {
const state: { loc?: NodeLocation["loc"] | null } = {
const state: { loc?: t.SourceLocation | null } = {
loc: null,
};
traverse(node as t.Node, errorVisitor, this.scope, state);
traverse(node, errorVisitor, this.scope, state);
loc = state.loc;

let txt =
Expand Down Expand Up @@ -279,3 +249,21 @@ export default class File {
return new _Error(msg);
}
}

if (!process.env.BABEL_8_BREAKING) {
// @ts-expect-error Babel 7
File.prototype.addImport = function addImport() {
throw new Error(
"This API has been removed. If you're looking for this " +
"functionality in Babel 7, you should import the " +
"'@babel/helper-module-imports' module and use the functions exposed " +
" from that module, such as 'addNamed' or 'addDefault'.",
);
};
// @ts-expect-error Babel 7
File.prototype.addTemplateObject = function addTemplateObject() {
throw new Error(
"This function has been moved into the template literal transform itself.",
);
};
}
5 changes: 3 additions & 2 deletions packages/babel-core/src/transformation/plugin-pass.ts
@@ -1,5 +1,5 @@
import type * as t from "@babel/types";
import type File from "./file/file.ts";
import type { NodeLocation } from "./file/file.ts";

export default class PluginPass<Options = {}> {
_map: Map<unknown, unknown> = new Map();
Expand Down Expand Up @@ -39,7 +39,7 @@ export default class PluginPass<Options = {}> {
}

buildCodeFrameError(
node: NodeLocation | undefined | null,
node: t.Node | undefined | null,
msg: string,
_Error?: typeof Error,
) {
Expand All @@ -56,6 +56,7 @@ if (!process.env.BABEL_8_BREAKING) {
(PluginPass as any).prototype.addImport = function addImport(
this: PluginPass,
): void {
// @ts-expect-error only exists in Babel 7
this.file.addImport();
};
}
24 changes: 14 additions & 10 deletions packages/babel-plugin-transform-object-rest-spread/src/index.ts
Expand Up @@ -642,17 +642,21 @@ export default declare((api, opts: Options) => {
if (setSpreadProperties) {
helper = getExtendsHelper(file);
} else {
try {
if (process.env.BABEL_8_BREAKING) {
helper = file.addHelper("objectSpread2");
} catch {
// TODO: This is needed to workaround https://github.com/babel/babel/issues/10187
// and https://github.com/babel/babel/issues/10179 for older @babel/core versions
// where #10187 isn't fixed.
this.file.declarations["objectSpread2"] = null;

// objectSpread2 has been introduced in v7.5.0
// We have to maintain backward compatibility.
helper = file.addHelper("objectSpread");
} else {
try {
helper = file.addHelper("objectSpread2");
} catch {
// TODO: This is needed to workaround https://github.com/babel/babel/issues/10187
// and https://github.com/babel/babel/issues/10179 for older @babel/core versions
// where #10187 isn't fixed.
this.file.declarations["objectSpread2"] = null;

// objectSpread2 has been introduced in v7.5.0
// We have to maintain backward compatibility.
helper = file.addHelper("objectSpread");
}
}
}

Expand Down