Skip to content

Commit

Permalink
[babel 8] Remove loose and spec options from preset-env (#16043)
Browse files Browse the repository at this point in the history
* [babel 8] Remove `loose` and `spec` options from `preset-env`

* Add tests for good error messages

* [babel 8] Remove env's loose special-handling in class features plugins
  • Loading branch information
nicolo-ribaudo authored Dec 10, 2023
1 parent cce807f commit 8fa2a91
Show file tree
Hide file tree
Showing 39 changed files with 110 additions and 115 deletions.
62 changes: 39 additions & 23 deletions packages/babel-helper-create-class-features-plugin/src/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,26 @@ const featuresSameLoose = new Map([
const featuresKey = "@babel/plugin-class-features/featuresKey";
const looseKey = "@babel/plugin-class-features/looseKey";

// See /~https://github.com/babel/babel/issues/11622.
// Since preset-env sets loose for the fields and private methods plugins, it can
// cause conflicts with the loose mode set by an explicit plugin in the config.
// To solve this problem, we ignore preset-env's loose mode if another plugin
// explicitly sets it
// The code to handle this logic doesn't check that "low priority loose" is always
// the same. However, it is only set by the preset and not directly by users:
// unless someone _wants_ to break it, it shouldn't be a problem.
const looseLowPriorityKey =
"@babel/plugin-class-features/looseLowPriorityKey/#__internal__@babel/preset-env__please-overwrite-loose-instead-of-throwing";
if (!process.env.BABEL_8_BREAKING) {
// See /~https://github.com/babel/babel/issues/11622.
// Since preset-env sets loose for the fields and private methods plugins, it can
// cause conflicts with the loose mode set by an explicit plugin in the config.
// To solve this problem, we ignore preset-env's loose mode if another plugin
// explicitly sets it
// The code to handle this logic doesn't check that "low priority loose" is always
// the same. However, it is only set by the preset and not directly by users:
// unless someone _wants_ to break it, it shouldn't be a problem.
// eslint-disable-next-line no-var
var looseLowPriorityKey =
"@babel/plugin-class-features/looseLowPriorityKey/#__internal__@babel/preset-env__please-overwrite-loose-instead-of-throwing";
}

if (!process.env.BABEL_8_BREAKING) {
// eslint-disable-next-line no-var
var canIgnoreLoose = function (file: File, feature: number) {
return !!(file.get(looseLowPriorityKey) & feature);
};
}

export function enableFeature(file: File, feature: number, loose: boolean) {
// We can't blindly enable the feature because, if it was already set,
Expand All @@ -55,7 +65,12 @@ export function enableFeature(file: File, feature: number, loose: boolean) {
// @babel/plugin-class-properties { loose: false }
// is transformed in loose mode.
// We only enabled the feature if it was previously disabled.
if (!hasFeature(file, feature) || canIgnoreLoose(file, feature)) {
if (process.env.BABEL_8_BREAKING) {
if (!hasFeature(file, feature)) {
file.set(featuresKey, file.get(featuresKey) | feature);
setLoose(file, feature, loose);
}
} else if (!hasFeature(file, feature) || canIgnoreLoose(file, feature)) {
file.set(featuresKey, file.get(featuresKey) | feature);
if (
// @ts-expect-error comparing loose with internal private magic string
Expand All @@ -77,28 +92,31 @@ export function enableFeature(file: File, feature: number, loose: boolean) {
}

let resolvedLoose: boolean | undefined;
let higherPriorityPluginName: string | undefined;

for (const [mask, name] of featuresSameLoose) {
if (!hasFeature(file, mask)) continue;
if (!process.env.BABEL_8_BREAKING) {
if (canIgnoreLoose(file, mask)) continue;
}

const loose = isLoose(file, mask);

if (canIgnoreLoose(file, mask)) {
continue;
} else if (resolvedLoose === !loose) {
if (resolvedLoose === !loose) {
throw new Error(
"'loose' mode configuration must be the same for @babel/plugin-transform-class-properties, " +
"@babel/plugin-transform-private-methods and " +
"@babel/plugin-transform-private-property-in-object (when they are enabled).",
);
} else {
resolvedLoose = loose;
higherPriorityPluginName = name;

if (!process.env.BABEL_8_BREAKING) {
// eslint-disable-next-line no-var
var higherPriorityPluginName = name;
}
}
}

if (resolvedLoose !== undefined) {
if (!process.env.BABEL_8_BREAKING && resolvedLoose !== undefined) {
for (const [mask, name] of featuresSameLoose) {
if (hasFeature(file, mask) && isLoose(file, mask) !== resolvedLoose) {
setLoose(file, mask, resolvedLoose);
Expand Down Expand Up @@ -129,11 +147,9 @@ function setLoose(file: File, feature: number, loose: boolean) {
if (loose) file.set(looseKey, file.get(looseKey) | feature);
else file.set(looseKey, file.get(looseKey) & ~feature);

file.set(looseLowPriorityKey, file.get(looseLowPriorityKey) & ~feature);
}

function canIgnoreLoose(file: File, feature: number) {
return !!(file.get(looseLowPriorityKey) & feature);
if (!process.env.BABEL_8_BREAKING) {
file.set(looseLowPriorityKey, file.get(looseLowPriorityKey) & ~feature);
}
}

export function shouldTransform(path: NodePath<t.Class>, file: File): boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"BABEL_8_BREAKING": false,
"validateLogs": true,
"presets": [["env", { "loose": true, "shippedProposals": true }]],
"assumptions": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"presets": [["env", { "loose": true, "targets": { "browsers": "ie 6" } }]],
"plugins": ["transform-flow-comments"]
"plugins": [
"transform-flow-comments",
["transform-classes", { "loose": true }]
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @flow
var C = /*#__PURE__*/function () {
let C = /*#__PURE__*/function () {
"use strict";

function C() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"presets": [["env", { "loose": true, "targets": { "browsers": "ie 6" } }]],
"plugins": ["transform-flow-strip-types"]
"plugins": [
"transform-flow-strip-types",
["transform-classes", { "loose": true }]
]
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var C = /*#__PURE__*/function () {
let C = /*#__PURE__*/function () {
"use strict";

function C() {}
Expand Down
5 changes: 0 additions & 5 deletions packages/babel-preset-env/src/get-option-specific-excludes.ts

This file was deleted.

31 changes: 23 additions & 8 deletions packages/babel-preset-env/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import semver, { type SemVer } from "semver";
import { logPlugin } from "./debug.ts";
import getOptionSpecificExcludesFor from "./get-option-specific-excludes.ts";
import {
addProposalSyntaxPlugins,
removeUnnecessaryItems,
Expand Down Expand Up @@ -318,6 +317,14 @@ export default declarePreset((api, opts: Options) => {

const babelTargets = api.targets();

if (process.env.BABEL_8_BREAKING && ("loose" in opts || "spec" in opts)) {
throw new Error(
"@babel/preset-env: The 'loose' and 'spec' options have been removed, " +
"and you should configure granular compiler assumptions instead. See " +
"https://babeljs.io/assumptions for more information.",
);
}

const {
bugfixes,
configPath,
Expand All @@ -326,16 +333,19 @@ export default declarePreset((api, opts: Options) => {
forceAllTransforms,
ignoreBrowserslistConfig,
include: optionsInclude,
loose,
modules: optionsModules,
shippedProposals,
spec,
targets: optionsTargets,
useBuiltIns,
corejs: { version: corejs, proposals },
browserslistEnv,
} = normalizeOptions(opts);

if (!process.env.BABEL_8_BREAKING) {
// eslint-disable-next-line no-var
var { loose, spec = false } = opts;
}

let targets = babelTargets;

if (
Expand Down Expand Up @@ -413,7 +423,9 @@ option \`forceAllTransforms: true\` instead.
exclude.plugins,
transformTargets,
getSpecialModulesPluginNames(modules, shouldTransformDynamicImport),
getOptionSpecificExcludesFor({ loose }),
process.env.BABEL_8_BREAKING || !loose
? undefined
: ["transform-typeof-symbol"],
pluginSyntaxMap,
);
if (shippedProposals) {
Expand Down Expand Up @@ -456,9 +468,10 @@ option \`forceAllTransforms: true\` instead.
const plugins = Array.from(pluginNames)
.map(pluginName => {
if (
pluginName === "transform-class-properties" ||
pluginName === "transform-private-methods" ||
pluginName === "transform-private-property-in-object"
!process.env.BABEL_8_BREAKING &&
(pluginName === "transform-class-properties" ||
pluginName === "transform-private-methods" ||
pluginName === "transform-private-property-in-object")
) {
return [
getPlugin(pluginName),
Expand All @@ -477,7 +490,9 @@ option \`forceAllTransforms: true\` instead.
}
return [
getPlugin(pluginName),
{ spec, loose, useBuiltIns: pluginUseBuiltIns },
process.env.BABEL_8_BREAKING
? { useBuiltIns: pluginUseBuiltIns }
: { spec, loose, useBuiltIns: pluginUseBuiltIns },
];
})
.concat(polyfillPlugins);
Expand Down
7 changes: 5 additions & 2 deletions packages/babel-preset-env/src/normalize-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ export default function normalizeOptions(opts: Options) {

checkDuplicateIncludeExcludes(include, exclude);

if (!process.env.BABEL_8_BREAKING) {
v.validateBooleanOption("loose", opts.loose);
v.validateBooleanOption("spec", opts.spec);
}

return {
bugfixes: v.validateBooleanOption(
TopLevelOptions.bugfixes,
Expand All @@ -296,14 +301,12 @@ export default function normalizeOptions(opts: Options) {
opts.ignoreBrowserslistConfig,
false,
),
loose: v.validateBooleanOption(TopLevelOptions.loose, opts.loose),
modules: validateModulesOption(opts.modules),
shippedProposals: v.validateBooleanOption(
TopLevelOptions.shippedProposals,
opts.shippedProposals,
false,
),
spec: v.validateBooleanOption(TopLevelOptions.spec, opts.spec, false),
targets: normalizeTargets(opts.targets),
useBuiltIns: useBuiltIns,
browserslistEnv: v.validateStringOption(
Expand Down
9 changes: 7 additions & 2 deletions packages/babel-preset-env/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ export const TopLevelOptions = {
forceAllTransforms: "forceAllTransforms",
ignoreBrowserslistConfig: "ignoreBrowserslistConfig",
include: "include",
loose: "loose",
modules: "modules",
shippedProposals: "shippedProposals",
spec: "spec",
targets: "targets",
useBuiltIns: "useBuiltIns",
browserslistEnv: "browserslistEnv",
} as const;

if (!process.env.BABEL_8_BREAKING) {
Object.assign(TopLevelOptions, {
loose: "loose",
spec: "spec",
});
}

export const ModulesOption = {
false: false,
auto: "auto",
Expand Down
6 changes: 4 additions & 2 deletions packages/babel-preset-env/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,18 @@ export type Options = {
forceAllTransforms: boolean;
ignoreBrowserslistConfig: boolean;
include: PluginListOption;
loose: boolean;
modules: ModuleOption;
shippedProposals: boolean;
spec: boolean;
targets: {
uglify?: boolean;
esmodules?: boolean;
} & InputTargets;
useBuiltIns: BuiltInsOption;
browserslistEnv: string;

// TODO(Babel 8): Remove these options
loose: boolean;
spec: boolean;
};

// Babel
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"BABEL_8_BREAKING": false,
"validateLogs": true,
"presets": [
["env", {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"BABEL_8_BREAKING": false,
"validateLogs": true,
"presets": [
["env", {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"BABEL_8_BREAKING": false,
"validateLogs": true,
"presets": [
["env", {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"BABEL_8_BREAKING": false,
"validateLogs": true,
"presets": [
["env", {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"BABEL_8_BREAKING": false,
"validateLogs": true,
"presets": [
["env", {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"BABEL_8_BREAKING": false,
"validateLogs": true,
"presets": [
["env", {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"BABEL_8_BREAKING": false,
"validateLogs": true,
"presets": [
["env", {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"BABEL_8_BREAKING": false,
"validateLogs": true,
"presets": [
["env", {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"BABEL_8_BREAKING": false,
"presets": [
[
"env",
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"BABEL_8_BREAKING": true,
"presets": [["env", { "loose": true }]],
"throws": "@babel/preset-env: The 'loose' and 'spec' options have been removed, and you should configure granular compiler assumptions instead. See https://babeljs.io/assumptions for more information."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
;
Loading

0 comments on commit 8fa2a91

Please sign in to comment.