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

[babel 8] Other Babel 8 misc changes #15576

Merged
merged 10 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
27 changes: 21 additions & 6 deletions packages/babel-core/src/config/files/module-types.ts
Expand Up @@ -29,19 +29,33 @@ export const supportsESM = semver.satisfies(
export default function* loadCodeDefault(
filepath: string,
asyncError: string,
// TODO(Babel 8): Remove this
fallbackToTranspiledModule: boolean = false,
): Handler<unknown> {
switch (path.extname(filepath)) {
case ".cjs":
return loadCjsDefault(filepath, fallbackToTranspiledModule);
if (process.env.BABEL_8_BREAKING) {
return loadCjsDefault(filepath);
} else {
return loadCjsDefault(
filepath,
// @ts-ignore(Babel 7 vs Babel 8) Removed in Babel 8
/* fallbackToTranspiledModule */ arguments[2],
);
}
case ".mjs":
break;
case ".cts":
return loadCtsDefault(filepath);
default:
try {
return loadCjsDefault(filepath, fallbackToTranspiledModule);
if (process.env.BABEL_8_BREAKING) {
return loadCjsDefault(filepath);
} else {
return loadCjsDefault(
filepath,
// @ts-ignore(Babel 7 vs Babel 8) Removed in Babel 8
/* fallbackToTranspiledModule */ arguments[2],
);
}
} catch (e) {
if (e.code !== "ERR_REQUIRE_ESM") throw e;
}
Expand Down Expand Up @@ -122,13 +136,14 @@ function loadCtsDefault(filepath: string) {
}
}

function loadCjsDefault(filepath: string, fallbackToTranspiledModule: boolean) {
function loadCjsDefault(filepath: string) {
const module = endHiddenCallStack(require)(filepath);
if (process.env.BABEL_8_BREAKING) {
return module?.__esModule ? module.default : module;
} else {
return module?.__esModule
? module.default || (fallbackToTranspiledModule ? module : undefined)
? module.default ||
/* fallbackToTranspiledModule */ (arguments[1] ? module : undefined)
: module;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-core/src/config/item.ts
Expand Up @@ -64,7 +64,7 @@ class ConfigItem {
*/
_descriptor: UnloadedDescriptor;

// TODO(Babel 8): Check if this symbol needs to be updated
// TODO(Babel 9): Check if this symbol needs to be updated
/**
* Used to detect ConfigItem instances from other Babel instances.
*/
Expand Down
Expand Up @@ -38,10 +38,12 @@ export function createClassFeaturePlugin({
feature,
loose,
manipulateOptions,
// @ts-ignore(Babel 7 vs Babel 8) TODO(Babel 8): Remove the default value
api = { assumption: () => void 0 },
api,
inherits,
}: Options): PluginObject {
if (!process.env.BABEL_8_BREAKING) {
api ??= { assumption: () => void 0 as any } as any;
}
const setPublicClassFields = api.assumption("setPublicClassFields");
const privateFieldsAsSymbols = api.assumption("privateFieldsAsSymbols");
const privateFieldsAsProperties = api.assumption("privateFieldsAsProperties");
Expand Down
24 changes: 16 additions & 8 deletions packages/babel-plugin-transform-arrow-functions/src/index.ts
Expand Up @@ -18,15 +18,23 @@ export default declare((api, options: Options) => {
// was queued up.
if (!path.isArrowFunctionExpression()) return;

path.arrowFunctionToExpression({
// While other utils may be fine inserting other arrows to make more transforms possible,
// the arrow transform itself absolutely cannot insert new arrow functions.
allowInsertArrow: false,
noNewArrows,
if (process.env.BABEL_8_BREAKING) {
path.arrowFunctionToExpression({
// While other utils may be fine inserting other arrows to make more transforms possible,
// the arrow transform itself absolutely cannot insert new arrow functions.
allowInsertArrow: false,
noNewArrows,
});
} else {
path.arrowFunctionToExpression({
allowInsertArrow: false,
noNewArrows,

// TODO(Babel 8): This is only needed for backward compat with @babel/traverse <7.13.0
specCompliant: !noNewArrows,
});
// This is only needed for backward compat with @babel/traverse <7.13.0
// @ts-ignore(Babel 7 vs Babel 8) Removed in Babel 8
specCompliant: !noNewArrows,
});
}
},
},
};
Expand Down
63 changes: 37 additions & 26 deletions packages/babel-plugin-transform-computed-properties/src/index.ts
Expand Up @@ -17,15 +17,18 @@ type PropertyInfo = {
state: PluginPass;
};

// TODO(Babel 8): Remove this
const DefineAccessorHelper = template.expression.ast`
function (type, obj, key, fn) {
var desc = { configurable: true, enumerable: true };
desc[type] = fn;
return Object.defineProperty(obj, key, desc);
}`;
// @ts-expect-error undocumented _compact node property
DefineAccessorHelper._compact = true;
if (!process.env.BABEL_8_BREAKING) {
// eslint-disable-next-line no-var
var DefineAccessorHelper = template.expression.ast`
function (type, obj, key, fn) {
var desc = { configurable: true, enumerable: true };
desc[type] = fn;
return Object.defineProperty(obj, key, desc);
}
`;
// @ts-expect-error undocumented _compact node property
DefineAccessorHelper._compact = true;
}

export default declare((api, options: Options) => {
api.assertVersion(7);
Expand All @@ -44,26 +47,34 @@ export default declare((api, options: Options) => {
key: t.Expression,
fn: t.Expression,
) {
let helper: t.Identifier;
if (state.availableHelper("defineAccessor")) {
helper = state.addHelper("defineAccessor");
if (process.env.BABEL_8_BREAKING) {
return t.callExpression(state.addHelper("defineAccessor"), [
t.stringLiteral(type),
obj,
key,
fn,
]);
} else {
// Fallback for @babel/helpers <= 7.20.6, manually add helper function
// TODO(Babel 8): Remove this
const file = state.file;
helper = file.get("fallbackDefineAccessorHelper");
if (!helper) {
const id = file.scope.generateUidIdentifier("defineAccessor");
file.scope.push({
id,
init: DefineAccessorHelper,
});
file.set("fallbackDefineAccessorHelper", (helper = id));
let helper: t.Identifier;
if (state.availableHelper("defineAccessor")) {
helper = state.addHelper("defineAccessor");
} else {
// Fallback for @babel/helpers <= 7.20.6, manually add helper function
const file = state.file;
helper = file.get("fallbackDefineAccessorHelper");
if (!helper) {
const id = file.scope.generateUidIdentifier("defineAccessor");
file.scope.push({
id,
init: DefineAccessorHelper,
});
file.set("fallbackDefineAccessorHelper", (helper = id));
}
helper = t.cloneNode(helper);
}
helper = t.cloneNode(helper);
}

return t.callExpression(helper, [t.stringLiteral(type), obj, key, fn]);
return t.callExpression(helper, [t.stringLiteral(type), obj, key, fn]);
}
}

/**
Expand Down
10 changes: 6 additions & 4 deletions packages/babel-plugin-transform-for-of/src/index.ts
Expand Up @@ -217,10 +217,12 @@ export default declare((api, options: Options) => {
return;
}

if (!state.availableHelper(builder.helper)) {
// Babel <7.9.0 doesn't support this helper
transformWithoutHelper(skipIteratorClosing, path, state);
return;
if (!process.env.BABEL_8_BREAKING) {
if (!state.availableHelper(builder.helper)) {
// Babel <7.9.0 doesn't support this helper
transformWithoutHelper(skipIteratorClosing, path, state);
return;
}
}

const { node, parent, scope } = path;
Expand Down
Expand Up @@ -3,7 +3,7 @@ import type { NodePath } from "@babel/traverse";

// This is the legacy implementation, which inlines all the code.
// It must be kept for compatibility reasons.
// TODO(Babel 8): Remove this code.
// TODO(Babel 8): Remove this file.

export default function transformWithoutHelper(
loose: boolean | void,
Expand Down
16 changes: 8 additions & 8 deletions packages/babel-preset-env/src/normalize-options.ts
Expand Up @@ -82,14 +82,14 @@ const expandIncludesAndExcludes = (
} else {
re = filter;
}
const items = filterableItems.filter(
item =>
re.test(item) ||
// For backwards compatibility, we also support matching against the
// proposal- name.
// TODO(Babel 8): Remove this.
re.test(item.replace(/^transform-/, "proposal-")),
);
const items = filterableItems.filter(item => {
return process.env.BABEL_8_BREAKING
? re.test(item)
: re.test(item) ||
// For backwards compatibility, we also support matching against the
// proposal- name.
re.test(item.replace(/^transform-/, "proposal-"));
});
if (items.length === 0) invalidFilters.push(filter);
return items;
});
Expand Down
Expand Up @@ -6,7 +6,7 @@
"useBuiltIns": "usage",
"corejs": 3,
"modules": false,
"exclude": ["proposal-object-rest-spread"]
"exclude": ["transform-object-rest-spread"]
}
]
]
Expand Down
Expand Up @@ -6,6 +6,8 @@ import _normalizeOptions, {
} from "../lib/normalize-options.js";
const normalizeOptions = _normalizeOptions.default || _normalizeOptions;

const itBabel7 = process.env.BABEL_8_BREAKING ? it.skip : it;

describe("normalize-options", () => {
describe("normalizeOptions", () => {
it("should return normalized `include` and `exclude`", () => {
Expand Down Expand Up @@ -83,7 +85,7 @@ describe("normalize-options", () => {

it("throws when including module plugins", () => {
expect(() =>
normalizeOptions({ include: ["proposal-dynamic-import"] }),
normalizeOptions({ include: ["transform-dynamic-import"] }),
).toThrow();
expect(() =>
normalizeOptions({ include: ["transform-modules-amd"] }),
Expand All @@ -92,7 +94,7 @@ describe("normalize-options", () => {

it("allows exclusion of module plugins", () => {
expect(() =>
normalizeOptions({ exclude: ["proposal-dynamic-import"] }),
normalizeOptions({ exclude: ["transform-dynamic-import"] }),
).not.toThrow();
expect(() =>
normalizeOptions({ exclude: ["transform-modules-commonjs"] }),
Expand Down Expand Up @@ -142,7 +144,7 @@ describe("normalize-options", () => {
]);
});

it("should work both with proposal-* and transform-*", () => {
itBabel7("should work both with proposal-* and transform-*", () => {
expect(
normalizeOptions({ include: ["proposal-.*-regex"] }).include,
).toEqual([
Expand Down
31 changes: 18 additions & 13 deletions packages/babel-traverse/src/path/conversion.ts
Expand Up @@ -105,14 +105,20 @@ export function ensureBlock(
return this.node;
}

/**
* Keeping this for backward-compatibility. You should use arrowFunctionToExpression() for >=7.x.
*/
// TODO(Babel 8): Remove this
export function arrowFunctionToShadowed(this: NodePath) {
if (!this.isArrowFunctionExpression()) return;

this.arrowFunctionToExpression();
declare const USE_ESM: boolean;

if (!process.env.BABEL_8_BREAKING) {
if (!USE_ESM) {
/**
* Keeping this for backward-compatibility. You should use arrowFunctionToExpression() for >=7.x.
*/
// eslint-disable-next-line no-restricted-globals
exports.arrowFunctionToShadowed = function (this: NodePath) {
if (!this.isArrowFunctionExpression()) return;

this.arrowFunctionToExpression();
};
}
}

/**
Expand Down Expand Up @@ -150,14 +156,13 @@ export function arrowFunctionToExpression(
{
allowInsertArrow = true,
allowInsertArrowWithRest = allowInsertArrow,
/** @deprecated Use `noNewArrows` instead */
specCompliant = false,
// TODO(Babel 8): Consider defaulting to `false` for spec compliancy
noNewArrows = !specCompliant,
noNewArrows = process.env.BABEL_8_BREAKING
? // TODO(Babel 8): Consider defaulting to `false` for spec compliancy
true
: !arguments[0]?.specCompliant,
}: {
allowInsertArrow?: boolean | void;
allowInsertArrowWithRest?: boolean | void;
specCompliant?: boolean | void;
noNewArrows?: boolean;
} = {},
): NodePath<
Expand Down