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

Simplify the special-case printing of single-param arrow functions #13204

Merged
merged 3 commits into from Apr 26, 2021
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
42 changes: 17 additions & 25 deletions packages/babel-generator/src/generators/methods.ts
Expand Up @@ -112,32 +112,18 @@ export function ArrowFunctionExpression(

const firstParam = node.params[0];

// Try to avoid printing parens in simple cases, but only if we're pretty
// sure that they aren't needed by type annotations or potential newlines.
if (
!this.format.retainLines &&
// Auxiliary comments can introduce unexpected newlines
!this.format.auxiliaryCommentBefore &&
!this.format.auxiliaryCommentAfter &&
node.params.length === 1 &&
t.isIdentifier(firstParam) &&
!hasTypes(node, firstParam)
!hasTypesOrComments(node, firstParam)
) {
if (
(this.format.retainLines || node.async) &&
((node.loc &&
node.body.loc &&
node.loc.start.line < node.body.loc.start.line) ||
firstParam.leadingComments?.length ||
firstParam.trailingComments?.length)
) {
this.token("(");
if (firstParam.loc && firstParam.loc.start.line > node.loc.start.line) {
this.indent();
this.print(firstParam, node);
this.dedent();
this._catchUp("start", node.body.loc);
} else {
this.print(firstParam, node);
}
this.token(")");
} else {
this.print(firstParam, node);
}
this.print(firstParam, node);
} else {
this._params(node);
}
Expand All @@ -151,12 +137,18 @@ export function ArrowFunctionExpression(
this.print(node.body, node);
}

function hasTypes(node, param) {
return (
function hasTypesOrComments(
node: t.ArrowFunctionExpression,
param: t.Identifier,
): boolean {
return !!(
node.typeParameters ||
node.returnType ||
// @ts-expect-error
node.predicate ||
Comment on lines +147 to +148
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't type-check because predicate isn't declared on ArrowFunctionExpression.

param.typeAnnotation ||
param.optional ||
param.trailingComments
param.leadingComments?.length ||
param.trailingComments?.length
);
}
@@ -1,11 +1,11 @@
var fn = async (
arg
) => {};
arg) =>
{};

async (x) =>
{};

async x => {};
async (x) => {};

async (x) =>
{};
Expand Up @@ -14,4 +14,4 @@ var f = (x: mixed): %checks => typeof x === "string";

const foo2 = (x: mixed): boolean %checks => typeof x === "string";

x: %checks => x !== null;
(x): %checks => x !== null;
@@ -1,8 +1,8 @@
const x = async ( // some comment
a) => {
a) => {
return foo(await a);
};

function foo(a) {
return a;
}
}
@@ -1,7 +1,7 @@
var _ref2;

const {
[(_ref) => {
[_ref => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked into why some of these arrow functions are losing their parens.

It turns out that the previous implementation of hasTypes was checking param.trailingComments, which is truthy when trailingComments is an empty array.

Switching to param.trailingComments?.length makes the generator conclude that this arrow function doesn't need parentheses after all.

let rest = babelHelpers.extends({}, _ref);
let b = babelHelpers.extends({}, {});
}]: a,
Expand Down
@@ -1,7 +1,7 @@
var _ref2;

const {
a = (_ref) => {
a = _ref => {
let rest = babelHelpers.extends({}, _ref);
let b = babelHelpers.extends({}, {});
},
Expand Down
@@ -1,4 +1,4 @@
(_ref) => {
_ref => {
let R = babelHelpers.extends({}, _ref);
let a = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : R;
};
Expand Down Expand Up @@ -32,7 +32,7 @@
}();
};

(_ref6) => {
_ref6 => {
let R = babelHelpers.extends({}, _ref6);
let a = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : f(R);
};
Expand Down
Expand Up @@ -15,7 +15,7 @@ const _bar = bar(),

const {
a
} = foo((_ref) => {
} = foo(_ref => {
let {
b
} = _ref,
Expand Down
Expand Up @@ -3,7 +3,7 @@ const get = () => {
return 3;
};

const f = (_ref) => {
const f = _ref => {
let {
a = get(),
b
Expand Down
@@ -1,5 +1,5 @@
// Edge
(_ref) => {
_ref => {
var {
x = 2
} = _ref;
Expand Down
@@ -1,4 +1,4 @@
(_ref) => {
_ref => {
var {
x = 2
} = _ref;
Expand Down
@@ -1,4 +1,4 @@
(_ref) => {
_ref => {
let _ref$x = _ref.x,
x = _ref$x === void 0 ? 2 : _ref$x;
};
@@ -1,4 +1,4 @@
(_ref) => {
_ref => {
let {
x = 2
} = _ref;
Expand Down