Skip to content

Commit

Permalink
Implement ignoreFunctionLength assumption (babel#12491)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Feb 10, 2021
1 parent ecc8fa0 commit f12b834
Show file tree
Hide file tree
Showing 38 changed files with 118 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/babel-core/src/config/validation/options.js
Expand Up @@ -332,6 +332,7 @@ type EnvPath = $ReadOnly<{
export type NestingPath = RootPath | OverridesPath | EnvPath;

export const assumptionsNames = new Set<string>([
"ignoreFunctionLength",
"ignoreToPrimitiveHint",
"mutableTemplateObject",
"setClassMethods",
Expand Down
Expand Up @@ -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"))
Expand Down Expand Up @@ -275,7 +278,7 @@ export default declare((api, opts) => {
idx >= i - 1 || paramsWithRestElement.has(idx);
convertFunctionParams(
path,
loose,
ignoreFunctionLength,
shouldTransformParam,
replaceRestElement,
);
Expand Down
@@ -0,0 +1,8 @@
{
"plugins": [
"proposal-object-rest-spread"
],
"assumptions": {
"ignoreFunctionLength": true
}
}
@@ -0,0 +1,3 @@
({...R}, a = R) => {}
({...R}, e, c = 2, a = R, f = q) => { let q; }
({...R}, a = f(R)) => {}
@@ -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);
}
};
@@ -0,0 +1,3 @@
({...R}, a = R) => {}
({...R}, e, c = 2, a = R, f = q) => { let q; }
({...R}, a = f(R)) => {}
@@ -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);
}
};
9 changes: 7 additions & 2 deletions packages/babel-plugin-transform-parameters/src/index.js
Expand Up @@ -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",

Expand All @@ -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.
Expand Down
7 changes: 5 additions & 2 deletions packages/babel-plugin-transform-parameters/src/params.js
Expand Up @@ -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,
) {
Expand Down Expand Up @@ -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");

Expand Down
Expand Up @@ -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
}
}
@@ -0,0 +1,3 @@
function fn(a, b = c()) {
return b;
}
@@ -0,0 +1,7 @@
function fn(a, b) {
if (b === void 0) {
b = c();
}

return b;
}
@@ -0,0 +1,5 @@
{
"plugins": [
["transform-parameters", { "loose": true }]
]
}

0 comments on commit f12b834

Please sign in to comment.