Skip to content

Commit

Permalink
polish: allow comments when printing single-binding arrow
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Oct 3, 2022
1 parent 11b8b5f commit 93b3596
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 35 deletions.
20 changes: 9 additions & 11 deletions packages/babel-generator/src/generators/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,26 +170,26 @@ export function ArrowFunctionExpression(
this: Printer,
node: t.ArrowFunctionExpression,
) {
const { _noLineTerminator } = this;
if (node.async) {
this._noLineTerminator = true;
this.word("async");
this.space();
}

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.
let firstParam;
if (
!this.format.retainLines &&
// Auxiliary comments can introduce unexpected newlines
!this.format.auxiliaryCommentBefore &&
!this.format.auxiliaryCommentAfter &&
node.params.length === 1 &&
isIdentifier(firstParam) &&
!hasTypesOrComments(node, firstParam)
isIdentifier((firstParam = node.params[0])) &&
!hasTypes(node, firstParam)
) {
this.print(firstParam, node);
this._noLineTerminator = _noLineTerminator;
} else {
this._noLineTerminator = _noLineTerminator;
this._params(node);
}

Expand All @@ -205,7 +205,7 @@ export function ArrowFunctionExpression(
this.print(node.body, node);
}

function hasTypesOrComments(
function hasTypes(
node: t.ArrowFunctionExpression,
param: t.Identifier,
): boolean {
Expand All @@ -214,8 +214,6 @@ function hasTypesOrComments(
node.returnType ||
node.predicate ||
param.typeAnnotation ||
param.optional ||
param.leadingComments?.length ||
param.trailingComments?.length
param.optional
);
}
15 changes: 4 additions & 11 deletions packages/babel-generator/test/arrow-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ describe("parameter parentheses", () => {
expect(output).toMatchInlineSnapshot(`
"() => {};
(
/*BEFORE*/
a) => {};
a => {};
(
/*BEFORE*/
Expand All @@ -41,9 +40,7 @@ describe("parameter parentheses", () => {
async () => {};
async (
/*BEFORE*/
a) => {};
async /*BEFORE*/a => {};
async (
/*BEFORE*/
Expand All @@ -59,9 +56,7 @@ describe("parameter parentheses", () => {
expect(output).toMatchInlineSnapshot(`
"() => {};
(a
/*AFTER*/
) => {};
a /*AFTER*/=> {};
(a
/*AFTER*/
Expand All @@ -71,9 +66,7 @@ describe("parameter parentheses", () => {
async () => {};
async (a
/*AFTER*/
) => {};
async a /*AFTER*/=> {};
async (a
/*AFTER*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
async (
/** @type {any} */
arg) => {};
async /** @type {any} */arg => {};

async (arg
/* trailing */
) => {};
async arg /* trailing */ => {};

async (
/** @type {any} */
arg
/* trailing */
) => {};
async /** @type {any} */arg /* trailing */ => {};
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const x = async ( // some comment
a) => {
const x = async /* some comment*/a => {
return foo(await a);
};

Expand Down

0 comments on commit 93b3596

Please sign in to comment.