diff --git a/packages/babel-generator/src/generators/methods.ts b/packages/babel-generator/src/generators/methods.ts index 5efd17a53582..4cfeaf151c16 100644 --- a/packages/babel-generator/src/generators/methods.ts +++ b/packages/babel-generator/src/generators/methods.ts @@ -184,7 +184,7 @@ export function ArrowFunctionExpression( !this.format.retainLines && node.params.length === 1 && isIdentifier((firstParam = node.params[0])) && - !hasTypes(node, firstParam) + !hasTypesOrComments(node, firstParam) ) { this.print(firstParam, node); this._noLineTerminator = _noLineTerminator; @@ -205,7 +205,7 @@ export function ArrowFunctionExpression( this.print(node.body, node); } -function hasTypes( +function hasTypesOrComments( node: t.ArrowFunctionExpression, param: t.Identifier, ): boolean { @@ -214,6 +214,9 @@ function hasTypes( node.returnType || node.predicate || param.typeAnnotation || - param.optional + param.optional || + // Flow does not support `foo /*: string*/ => {};` + param.leadingComments?.length || + param.trailingComments?.length ); } diff --git a/packages/babel-generator/test/fixtures/comments/async-arrow-single-param-with-comments/output.js b/packages/babel-generator/test/fixtures/comments/async-arrow-single-param-with-comments/output.js index a1e62b4e986f..addc834ee0ed 100644 --- a/packages/babel-generator/test/fixtures/comments/async-arrow-single-param-with-comments/output.js +++ b/packages/babel-generator/test/fixtures/comments/async-arrow-single-param-with-comments/output.js @@ -1,3 +1,3 @@ -async /** @type {any} */arg => {}; -async arg /* trailing */ => {}; -async /** @type {any} */arg /* trailing */ => {}; \ No newline at end of file +async ( /** @type {any} */arg) => {}; +async (arg /* trailing */) => {}; +async ( /** @type {any} */arg /* trailing */) => {}; \ No newline at end of file diff --git a/packages/babel-generator/test/fixtures/parentheses/async-arrow-function/output.js b/packages/babel-generator/test/fixtures/parentheses/async-arrow-function/output.js index f718e264aefc..bf33d2ca48fc 100644 --- a/packages/babel-generator/test/fixtures/parentheses/async-arrow-function/output.js +++ b/packages/babel-generator/test/fixtures/parentheses/async-arrow-function/output.js @@ -1,6 +1,6 @@ -const x = async -/* some comment*/ -a => { +const x = async ( +// some comment +a) => { return foo(await a); }; function foo(a) {