Skip to content

Commit

Permalink
[babel 8] Remove babel 7-specific imports
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Feb 5, 2025
1 parent e02b0ff commit d4a3dff
Show file tree
Hide file tree
Showing 21 changed files with 132 additions and 81 deletions.
89 changes: 80 additions & 9 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,16 +244,20 @@ module.exports = function (api) {
name: "process.env.IS_PUBLISH",
value: bool(process.env.IS_PUBLISH),
},
"flag-IS_PUBLISH",
],

process.env.STRIP_BABEL_8_FLAG && [
pluginToggleBooleanFlag,
{
name: "process.env.BABEL_8_BREAKING",
value: bool(process.env.BABEL_8_BREAKING),
},
"flag-BABEL_8_BREAKING",
],
process.env.STRIP_BABEL_8_FLAG
? [
pluginToggleBooleanFlag,
{
name: "process.env.BABEL_8_BREAKING",
attributeName: "BABEL_8_BREAKING",
value: bool(process.env.BABEL_8_BREAKING),
},
"flag-BABEL_8_BREAKING",
]
: [pluginDropBooleanImportAttribute, { name: "BABEL_8_BREAKING" }],

pluginPackageJsonMacro,

Expand Down Expand Up @@ -575,7 +579,10 @@ function pluginPolyfillsOldNode({ template, types: t }) {
* @param {import("@babel/core")} pluginAPI
* @returns {import("@babel/core").PluginObject}
*/
function pluginToggleBooleanFlag({ types: t }, { name, value }) {
function pluginToggleBooleanFlag(
{ types: t },
{ name, attributeName = name, value }
) {
if (typeof value !== "boolean") throw new Error(`.value must be a boolean`);

function evaluate(test) {
Expand Down Expand Up @@ -684,6 +691,64 @@ function pluginToggleBooleanFlag({ types: t }, { name, value }) {
throw path.buildCodeFrameError("This check could not be stripped.");
}
},
ImportDeclaration(path) {
if (!path.node.attributes?.length) return;

/** @type {null | import("@babel/core").NodePath<import("@babel/core").types.ImportAttribute>} */
const attribute = path
.get("attributes")
.find(attr => attr.node.key.name === attributeName);
if (attribute == null) {
return;
}

const attributeValue = attribute.node.value.value;
if (attributeValue !== "true" && attributeValue !== "false") {
throw new path.buildCodeFrameError(
`${attributeName} attribute must be "true" or "false"`
);
}

if (attributeValue !== String(value)) {
path.remove();
} else {
attribute.remove();
if (path.node.attributes.length === 0) {
path.node.attributes = null;
}
}
},
},
};
}

/**
* @param {import("@babel/core")} pluginAPI
* @returns {import("@babel/core").PluginObject}
*/
function pluginDropBooleanImportAttribute(_babel, { name }) {
return {
visitor: {
ImportDeclaration(path) {
if (!path.node.attributes?.length) return;

const attribute = path
.get("attributes")
.find(attr => attr.node.key.name === name);
if (attribute == null) return;

const attributeValue = attribute.node.value.value;
if (attributeValue !== "true" && attributeValue !== "false") {
throw new path.buildCodeFrameError(
`${name} attribute must be "true" or "false"`
);
}

attribute.remove();
if (path.node.attributes.length === 0) {
path.node.attributes = null;
}
},
},
};
}
Expand Down Expand Up @@ -916,6 +981,12 @@ function pluginImportMetaUrl({ types: t, template }) {

// Let's just remove this declaration to unshadow the "global" cjs require.
path.remove();
path.scope.crawl();

const createRequireBinding = path.scope.getBinding("createRequire");
if (!createRequireBinding.referenced) {
createRequireBinding.path.remove();
}
},

// import.meta.url
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ export const DEFAULT_EXTENSIONS = Object.freeze([
".cjs",
] as const);

import Module from "module";
import * as thisFile from "./index.ts";
import Module from "module" with { USE_ESM: "true", IS_STANDALONE: "false" };
import * as thisFile from "./index.ts" with { USE_ESM: "true", IS_STANDALONE: "false" };
if (USE_ESM && !IS_STANDALONE) {
// Pass this module to the CJS proxy, so that it can be synchronously accessed.
const cjsProxy = Module.createRequire(import.meta.url)("../cjs-proxy.cjs");
Expand Down
10 changes: 8 additions & 2 deletions packages/babel-core/src/transformation/file/babel-7-helpers.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
// TODO(Babel 8): Remove this file

exports.getModuleName = () =>
require("@babel/helper-module-transforms").getModuleName;
if (!process.env.BABEL_8_BREAKING) {
exports.getModuleName = () =>
require("@babel/helper-module-transforms").getModuleName;
} else if (process.env.IS_PUBLISH) {
throw new Error(
"Internal Babel error: This file should only be loaded in Babel 7",
);
}
2 changes: 1 addition & 1 deletion packages/babel-core/src/transformation/file/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import semver from "semver";
import type { NormalizedFile } from "../normalize-file.ts";

// @ts-expect-error This file is `any`
import * as babel7 from "./babel-7-helpers.cjs";
import babel7 from "./babel-7-helpers.cjs" with { BABEL_8_BREAKING: "false" };

const errorVisitor: Visitor<{ loc: t.SourceLocation | null }> = {
enter(path, state) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
// TODO(Babel 8): Remove this file
if (process.env.BABEL_8_BREAKING && process.env.IS_PUBLISH) {
throw new Error(
"Internal Babel error: This file should only be loaded in Babel 7",
);
}

import { types as t, template } from "@babel/core";
import type { File, NodePath } from "@babel/core";
import ReplaceSupers from "@babel/helper-replace-supers";

type Decoratable = Extract<t.Node, { decorators?: t.Decorator[] | null }>;

export function hasOwnDecorators(node: t.Class | t.ClassBody["body"][number]) {
// @ts-expect-error: 'decorators' not in TSIndexSignature
return !!node.decorators?.length;
}

export function hasDecorators(node: t.Class) {
return hasOwnDecorators(node) || node.body.body.some(hasOwnDecorators);
}

function prop(key: string, value?: t.Expression) {
if (!value) return null;
return t.objectProperty(t.identifier(key), value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import {
} from "./fields.ts";
import { memoiseComputedKey } from "./misc.ts";

export function hasOwnDecorators(node: t.Class | t.ClassBody["body"][number]) {
// @ts-expect-error: 'decorators' not in TSIndexSignature
return !!node.decorators?.length;
}

export function hasDecorators(node: t.Class) {
return hasOwnDecorators(node) || node.body.body.some(hasOwnDecorators);
}

// We inline this package
// eslint-disable-next-line import/no-extraneous-dependencies
import * as charCodes from "charcodes";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { File, types as t } from "@babel/core";
import type { NodePath } from "@babel/core";
import { hasOwnDecorators } from "./decorators-2018-09.ts";
import { hasOwnDecorators } from "./decorators.ts";

export const FEATURES = Object.freeze({
//classes: 1 << 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { types as t } from "@babel/core";
import type { PluginAPI, PluginObject, NodePath } from "@babel/core";
import createDecoratorTransform from "./decorators.ts";
import type { DecoratorVersionKind } from "./decorators.ts";

import semver from "semver";

Expand All @@ -13,7 +11,9 @@ import {
buildCheckInRHS,
} from "./fields.ts";
import type { PropPath } from "./fields.ts";
import { buildDecoratedClass, hasDecorators } from "./decorators-2018-09.ts";
import createDecoratorTransform, { hasDecorators } from "./decorators.ts";
import type { DecoratorVersionKind } from "./decorators.ts";
import { buildDecoratedClass } from "./decorators-2018-09.ts" with { BABEL_8_BREAKING: "false" };
import { injectInitialization, extractComputedKeys } from "./misc.ts";
import {
enableFeature,
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-plugin-transform-for-of/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { declare } from "@babel/helper-plugin-utils";
import { template, types as t, type NodePath } from "@babel/core";

import transformWithoutHelper from "./no-helper-implementation.ts";
import transformWithoutHelper from "./no-helper-implementation.ts" with { BABEL_8_BREAKING: "false" };
import { skipTransparentExprWrapperNodes } from "@babel/helper-skip-transparent-expression-wrappers";

export interface Options {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
if (process.env.BABEL_8_BREAKING && process.env.IS_PUBLISH) {
throw new Error(
"Internal Babel error: This file should only be loaded in Babel 7",
);
}

import { template, types as t } from "@babel/core";
import type { PluginPass, NodePath } from "@babel/core";

Expand Down
2 changes: 1 addition & 1 deletion packages/babel-plugin-transform-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { hasMinVersion } from "./helpers.ts";
import getRuntimePath, { resolveFSPath } from "./get-runtime-path/index.ts";

// TODO(Babel 8): Remove this
import babel7 from "./babel-7/index.cjs";
import babel7 from "./babel-7/index.cjs" with { BABEL_8_BREAKING: "false" };

export interface Options {
absoluteRuntime?: boolean;
Expand Down
26 changes: 7 additions & 19 deletions packages/babel-preset-env/src/available-plugins.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint sort-keys: "error" */

import otherBabel7Plugins from "./babel-7-available-plugins.cjs";
import syntaxImportAssertions from "@babel/plugin-syntax-import-assertions" with { BABEL_8_BREAKING: "false" };
import syntaxImportAttributes from "@babel/plugin-syntax-import-assertions" with { BABEL_8_BREAKING: "false" };

import transformAsyncGeneratorFunctions from "@babel/plugin-transform-async-generator-functions";
import transformAsyncToGenerator from "@babel/plugin-transform-async-to-generator";
Expand Down Expand Up @@ -185,6 +186,11 @@ if (!process.env.BABEL_8_BREAKING) {
"syntax-private-property-in-object": syntax("privateIn"),
"syntax-top-level-await": syntax("topLevelAwait"),

// These plugins have more logic than just enabling/disabling a feature
// eslint-disable-next-line sort-keys
"syntax-import-assertions": () => syntaxImportAssertions,
"syntax-import-attributes": () => syntaxImportAttributes,

// These are CJS plugins that depend on a package from the monorepo, so it
// breaks using ESM. Given that ESM builds are new enough to have this
// syntax enabled by default, we can safely skip enabling it.
Expand All @@ -193,24 +199,6 @@ if (!process.env.BABEL_8_BREAKING) {
USE_ESM || IS_STANDALONE
? () => () => ({})
: () => require("@babel/plugin-syntax-unicode-sets-regex"),

// We need to keep these plugins because they do not simply enable a
// feature, but can affect the AST shape (.attributes vs .assertions).
// TLA is only used for local development with ESM, since we cannot
// require() monorepo files in that case.
// eslint-disable-next-line sort-keys
"syntax-import-assertions":
USE_ESM && !IS_STANDALONE
? await import("@babel/plugin-syntax-import-assertions").then(
m => () => m.default,
)
: otherBabel7Plugins["syntax-import-assertions"],
"syntax-import-attributes":
USE_ESM && !IS_STANDALONE
? await import("@babel/plugin-syntax-import-attributes").then(
m => () => m.default,
)
: otherBabel7Plugins["syntax-import-attributes"],
};

Object.assign(availablePlugins, legacyBabel7SyntaxPluginsLoaders);
Expand Down
15 changes: 0 additions & 15 deletions packages/babel-preset-env/src/babel-7-available-plugins.cjs

This file was deleted.

7 changes: 0 additions & 7 deletions packages/babel-preset-env/src/babel-7-available-plugins.d.cts

This file was deleted.

3 changes: 1 addition & 2 deletions packages/babel-preset-env/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import _pluginCoreJS3 from "babel-plugin-polyfill-corejs3";
// TODO(Babel 8): Just use the default import
const pluginCoreJS3 = _pluginCoreJS3.default || _pluginCoreJS3;

// TODO(Babel 8): Remove this
import babel7 from "./polyfills/babel-7-plugins.cjs";
import babel7 from "./polyfills/babel-7-plugins.cjs" with { BABEL_8_BREAKING: "false" };

import getTargets, {
prettifyTargets,
Expand Down
3 changes: 1 addition & 2 deletions packages/babel-preset-env/src/normalize-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import {
} from "./options.ts";
import { OptionValidator } from "@babel/helper-validator-option";

// TODO(Babel 8): Remove this
import babel7 from "./polyfills/babel-7-plugins.cjs";
import babel7 from "./polyfills/babel-7-plugins.cjs" with { BABEL_8_BREAKING: "false" };

import type {
BuiltInsOption,
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-traverse/src/path/lib/hoister.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Remove this file in Babel 8
// TODO: Remove this file in Babel 8

import { react } from "@babel/types";
import {
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-traverse/src/path/modification.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// This file contains methods that modify the path/node in some ways.

import { getCachedPaths } from "../cache.ts";
import PathHoister from "./lib/hoister.ts";
import NodePath from "./index.ts";
import { _getQueueContexts, pushContext, setScope } from "./context.ts";
import { _assertUnremoved } from "./removal.ts";
Expand Down Expand Up @@ -403,6 +402,7 @@ export function pushContainer<
return path.replaceWithMultiple(verifiedNodes);
}

import PathHoister from "./lib/hoister.ts" with { BABEL_8_BREAKING: "false", USE_ESM: "false" };
if (!process.env.BABEL_8_BREAKING && !USE_ESM) {
/**
* Hoist the current node to the highest scope possible and return a UID
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// TODO(Babel 8) Remove this file
if (process.env.BABEL_8_BREAKING) {
if (process.env.BABEL_8_BREAKING && process.env.IS_PUBLISH) {
throw new Error(
"Internal Babel error: This file should only be loaded in Babel 7",
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// TODO(Babel 8) Remove this file
if (process.env.BABEL_8_BREAKING) {
if (process.env.BABEL_8_BREAKING && process.env.IS_PUBLISH) {
throw new Error(
"Internal Babel error: This file should only be loaded in Babel 7",
);
Expand Down
5 changes: 2 additions & 3 deletions packages/babel-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,10 @@ export type * from "./ast-types/generated/index.ts";
// this is used by @babel/traverse to warn about deprecated visitors
export { default as __internal__deprecationWarning } from "./utils/deprecationWarning.ts";

import toSequenceExpression from "./converters/toSequenceExpression.ts" with { BABEL_8_BREAKING: "false", USE_ESM: "false", IS_STANDALONE: "false" };
if (!process.env.BABEL_8_BREAKING && !USE_ESM && !IS_STANDALONE) {
// eslint-disable-next-line no-restricted-globals
exports.toSequenceExpression =
// eslint-disable-next-line no-restricted-globals
require("./converters/toSequenceExpression.js").default;
exports.toSequenceExpression = toSequenceExpression;
}

if (!process.env.BABEL_8_BREAKING && process.env.BABEL_TYPES_8_BREAKING) {
Expand Down

0 comments on commit d4a3dff

Please sign in to comment.