diff --git a/packages/babel-core/src/config/validation/options.js b/packages/babel-core/src/config/validation/options.js index b48ad22a941b..dbfaa1b4856f 100644 --- a/packages/babel-core/src/config/validation/options.js +++ b/packages/babel-core/src/config/validation/options.js @@ -332,6 +332,7 @@ type EnvPath = $ReadOnly<{ export type NestingPath = RootPath | OverridesPath | EnvPath; export const assumptionsNames = new Set([ + "ignoreFunctionLength", "ignoreToPrimitiveHint", "mutableTemplateObject", "setClassMethods", diff --git a/packages/babel-plugin-proposal-object-rest-spread/src/index.js b/packages/babel-plugin-proposal-object-rest-spread/src/index.js index e1f9b7d51016..5fdc599b655f 100644 --- a/packages/babel-plugin-proposal-object-rest-spread/src/index.js +++ b/packages/babel-plugin-proposal-object-rest-spread/src/index.js @@ -23,6 +23,9 @@ export default declare((api, opts) => { throw new Error(".loose must be a boolean, or undefined"); } + const ignoreFunctionLength = + api.assumption("ignoreFunctionLength") ?? opts.loose; + function getExtendsHelper(file) { return useBuiltIns ? t.memberExpression(t.identifier("Object"), t.identifier("assign")) @@ -275,7 +278,7 @@ export default declare((api, opts) => { idx >= i - 1 || paramsWithRestElement.has(idx); convertFunctionParams( path, - loose, + ignoreFunctionLength, shouldTransformParam, replaceRestElement, ); diff --git a/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/assumption-ignoreFunctionLength/options.json b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/assumption-ignoreFunctionLength/options.json new file mode 100644 index 000000000000..ec966b0b43ba --- /dev/null +++ b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/assumption-ignoreFunctionLength/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + "proposal-object-rest-spread" + ], + "assumptions": { + "ignoreFunctionLength": true + } +} diff --git a/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/assumption-ignoreFunctionLength/parameters-object-rest-used-in-default/input.js b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/assumption-ignoreFunctionLength/parameters-object-rest-used-in-default/input.js new file mode 100644 index 000000000000..000e99bcf250 --- /dev/null +++ b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/assumption-ignoreFunctionLength/parameters-object-rest-used-in-default/input.js @@ -0,0 +1,3 @@ +({...R}, a = R) => {} +({...R}, e, c = 2, a = R, f = q) => { let q; } +({...R}, a = f(R)) => {} diff --git a/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/assumption-ignoreFunctionLength/parameters-object-rest-used-in-default/output.js b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/assumption-ignoreFunctionLength/parameters-object-rest-used-in-default/output.js new file mode 100644 index 000000000000..84e28e863e5b --- /dev/null +++ b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/assumption-ignoreFunctionLength/parameters-object-rest-used-in-default/output.js @@ -0,0 +1,33 @@ +function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } + +(_ref, a) => { + let R = _extends({}, _ref); + + if (a === void 0) { + a = R; + } +}; + +(_ref2, e, c = 2, a, f) => { + let R = _extends({}, _ref2); + + if (a === void 0) { + a = R; + } + + if (f === void 0) { + f = q; + } + + return function () { + let q; + }(); +}; + +(_ref3, a) => { + let R = _extends({}, _ref3); + + if (a === void 0) { + a = f(R); + } +}; diff --git a/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/parameters-extra/input.js b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/parameters-object-rest-used-in-default/input.js similarity index 100% rename from packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/parameters-extra/input.js rename to packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/parameters-object-rest-used-in-default/input.js diff --git a/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/parameters-extra/output.js b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/parameters-object-rest-used-in-default/output.js similarity index 100% rename from packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/parameters-extra/output.js rename to packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/parameters-object-rest-used-in-default/output.js diff --git a/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-spread-loose/parameters-object-rest-used-in-default/input.js b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-spread-loose/parameters-object-rest-used-in-default/input.js new file mode 100644 index 000000000000..000e99bcf250 --- /dev/null +++ b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-spread-loose/parameters-object-rest-used-in-default/input.js @@ -0,0 +1,3 @@ +({...R}, a = R) => {} +({...R}, e, c = 2, a = R, f = q) => { let q; } +({...R}, a = f(R)) => {} diff --git a/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-spread-loose/parameters-object-rest-used-in-default/output.js b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-spread-loose/parameters-object-rest-used-in-default/output.js new file mode 100644 index 000000000000..84e28e863e5b --- /dev/null +++ b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-spread-loose/parameters-object-rest-used-in-default/output.js @@ -0,0 +1,33 @@ +function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } + +(_ref, a) => { + let R = _extends({}, _ref); + + if (a === void 0) { + a = R; + } +}; + +(_ref2, e, c = 2, a, f) => { + let R = _extends({}, _ref2); + + if (a === void 0) { + a = R; + } + + if (f === void 0) { + f = q; + } + + return function () { + let q; + }(); +}; + +(_ref3, a) => { + let R = _extends({}, _ref3); + + if (a === void 0) { + a = f(R); + } +}; diff --git a/packages/babel-plugin-transform-parameters/src/index.js b/packages/babel-plugin-transform-parameters/src/index.js index 1f5cb7184b04..91816d56e908 100644 --- a/packages/babel-plugin-transform-parameters/src/index.js +++ b/packages/babel-plugin-transform-parameters/src/index.js @@ -6,7 +6,9 @@ export { convertFunctionParams }; export default declare((api, options) => { api.assertVersion(7); - const { loose } = options; + const ignoreFunctionLength = + api.assumption("ignoreFunctionLength") ?? options.loose; + return { name: "transform-parameters", @@ -23,7 +25,10 @@ export default declare((api, options) => { } const convertedRest = convertFunctionRest(path); - const convertedParams = convertFunctionParams(path, loose); + const convertedParams = convertFunctionParams( + path, + ignoreFunctionLength, + ); if (convertedRest || convertedParams) { // Manually reprocess this scope to ensure that the moved params are updated. diff --git a/packages/babel-plugin-transform-parameters/src/params.js b/packages/babel-plugin-transform-parameters/src/params.js index 4608a225af45..58629183c35f 100644 --- a/packages/babel-plugin-transform-parameters/src/params.js +++ b/packages/babel-plugin-transform-parameters/src/params.js @@ -44,7 +44,7 @@ const iifeVisitor = { // last 2 parameters are optional -- they are used by proposal-object-rest-spread/src/index.js export default function convertFunctionParams( path, - loose, + ignoreFunctionLength, shouldTransformParam, replaceRestElement, ) { @@ -123,7 +123,10 @@ export default function convertFunctionParams( } const paramIsAssignmentPattern = param.isAssignmentPattern(); - if (paramIsAssignmentPattern && (loose || node.kind === "set")) { + if ( + paramIsAssignmentPattern && + (ignoreFunctionLength || node.kind === "set") + ) { const left = param.get("left"); const right = param.get("right"); diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/complex-assignment/input.js b/packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/complex-assignment/input.js similarity index 100% rename from packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/complex-assignment/input.js rename to packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/complex-assignment/input.js diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/complex-assignment/output.js b/packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/complex-assignment/output.js similarity index 100% rename from packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/complex-assignment/output.js rename to packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/complex-assignment/output.js diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-array-destructuring/exec.js b/packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-array-destructuring/exec.js similarity index 100% rename from packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-array-destructuring/exec.js rename to packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-array-destructuring/exec.js diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-array-destructuring/input.js b/packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-array-destructuring/input.js similarity index 100% rename from packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-array-destructuring/input.js rename to packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-array-destructuring/input.js diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-array-destructuring/output.js b/packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-array-destructuring/output.js similarity index 100% rename from packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-array-destructuring/output.js rename to packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-array-destructuring/output.js diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-assignment-with-types/input.mjs b/packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-assignment-with-types/input.mjs similarity index 100% rename from packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-assignment-with-types/input.mjs rename to packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-assignment-with-types/input.mjs diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-assignment-with-types/options.json b/packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-assignment-with-types/options.json similarity index 100% rename from packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-assignment-with-types/options.json rename to packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-assignment-with-types/options.json diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-assignment-with-types/output.mjs b/packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-assignment-with-types/output.mjs similarity index 100% rename from packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-assignment-with-types/output.mjs rename to packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-assignment-with-types/output.mjs diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-earlier-params/exec.js b/packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-earlier-params/exec.js similarity index 100% rename from packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-earlier-params/exec.js rename to packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-earlier-params/exec.js diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-iife-1128/exec.js b/packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-iife-1128/exec.js similarity index 100% rename from packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-iife-1128/exec.js rename to packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-iife-1128/exec.js diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-iife-4253/exec.js b/packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-iife-4253/exec.js similarity index 100% rename from packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-iife-4253/exec.js rename to packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-iife-4253/exec.js diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-iife-self/exec.js b/packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-iife-self/exec.js similarity index 100% rename from packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-iife-self/exec.js rename to packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-iife-self/exec.js diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-multiple/input.js b/packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-multiple/input.js similarity index 100% rename from packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-multiple/input.js rename to packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-multiple/input.js diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-multiple/output.js b/packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-multiple/output.js similarity index 100% rename from packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-multiple/output.js rename to packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-multiple/output.js diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-object-destructuring/exec.js b/packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-object-destructuring/exec.js similarity index 100% rename from packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-object-destructuring/exec.js rename to packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-object-destructuring/exec.js diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-rest-mix/input.js b/packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-rest-mix/input.js similarity index 100% rename from packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-rest-mix/input.js rename to packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-rest-mix/input.js diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-rest-mix/output.js b/packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-rest-mix/output.js similarity index 100% rename from packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-rest-mix/output.js rename to packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-rest-mix/output.js diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-rest/exec.js b/packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-rest/exec.js similarity index 100% rename from packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-rest/exec.js rename to packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-rest/exec.js diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-single/input.js b/packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-single/input.js similarity index 100% rename from packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-single/input.js rename to packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-single/input.js diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-single/output.js b/packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-single/output.js similarity index 100% rename from packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/default-single/output.js rename to packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/default-single/output.js diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/destructuring-rest/input.js b/packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/destructuring-rest/input.js similarity index 100% rename from packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/destructuring-rest/input.js rename to packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/destructuring-rest/input.js diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/destructuring-rest/output.js b/packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/destructuring-rest/output.js similarity index 100% rename from packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/destructuring-rest/output.js rename to packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/destructuring-rest/output.js diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/options.json b/packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/options.json similarity index 62% rename from packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/options.json rename to packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/options.json index aa8f6cf317dd..f8c2c7a9fafe 100644 --- a/packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/options.json +++ b/packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/options.json @@ -3,12 +3,14 @@ "proposal-class-properties", "external-helpers", "syntax-flow", - ["transform-parameters", { "loose": true }], + "transform-parameters", "transform-block-scoping", "transform-spread", "transform-classes", "transform-destructuring", - "transform-arrow-functions", - "transform-for-of" - ] + "transform-arrow-functions" + ], + "assumptions": { + "ignoreFunctionLength": true + } } diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/overwrite-undefined/exec.js b/packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/overwrite-undefined/exec.js similarity index 100% rename from packages/babel-plugin-transform-parameters/test/fixtures/use-loose-option/overwrite-undefined/exec.js rename to packages/babel-plugin-transform-parameters/test/fixtures/assumption-ignoreFunctionLength/overwrite-undefined/exec.js diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/loose/ignoreFunctionLength/input.js b/packages/babel-plugin-transform-parameters/test/fixtures/loose/ignoreFunctionLength/input.js new file mode 100644 index 000000000000..b91cfb244914 --- /dev/null +++ b/packages/babel-plugin-transform-parameters/test/fixtures/loose/ignoreFunctionLength/input.js @@ -0,0 +1,3 @@ +function fn(a, b = c()) { + return b; +} diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/loose/ignoreFunctionLength/output.js b/packages/babel-plugin-transform-parameters/test/fixtures/loose/ignoreFunctionLength/output.js new file mode 100644 index 000000000000..6c1471d59a4f --- /dev/null +++ b/packages/babel-plugin-transform-parameters/test/fixtures/loose/ignoreFunctionLength/output.js @@ -0,0 +1,7 @@ +function fn(a, b) { + if (b === void 0) { + b = c(); + } + + return b; +} diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/loose/options.json b/packages/babel-plugin-transform-parameters/test/fixtures/loose/options.json new file mode 100644 index 000000000000..0d49296cfac0 --- /dev/null +++ b/packages/babel-plugin-transform-parameters/test/fixtures/loose/options.json @@ -0,0 +1,5 @@ +{ + "plugins": [ + ["transform-parameters", { "loose": true }] + ] +}