From 41449c00a096e5c1773ee4a6a3ed6fddeef7b927 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Wed, 28 Sep 2022 17:36:30 -0400 Subject: [PATCH 01/11] fix: ensure no linebreak between async and do --- packages/babel-generator/src/generators/expressions.ts | 5 ++++- .../fixtures/async-do-expressions/inner-comments/input.js | 1 + .../fixtures/async-do-expressions/inner-comments/output.js | 5 +++++ 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 packages/babel-generator/test/fixtures/async-do-expressions/inner-comments/input.js create mode 100644 packages/babel-generator/test/fixtures/async-do-expressions/inner-comments/output.js diff --git a/packages/babel-generator/src/generators/expressions.ts b/packages/babel-generator/src/generators/expressions.ts index 82680bca7868..5a7f690efc63 100644 --- a/packages/babel-generator/src/generators/expressions.ts +++ b/packages/babel-generator/src/generators/expressions.ts @@ -28,7 +28,10 @@ export function UnaryExpression(this: Printer, node: t.UnaryExpression) { export function DoExpression(this: Printer, node: t.DoExpression) { if (node.async) { this.word("async"); - this.space(); + this.ensureNoLineTerminator(() => { + this.printInnerComments(node); + this.space(); + }); } this.word("do"); this.space(); diff --git a/packages/babel-generator/test/fixtures/async-do-expressions/inner-comments/input.js b/packages/babel-generator/test/fixtures/async-do-expressions/inner-comments/input.js new file mode 100644 index 000000000000..6f070e80c4a6 --- /dev/null +++ b/packages/babel-generator/test/fixtures/async-do-expressions/inner-comments/input.js @@ -0,0 +1 @@ +async /* 1 */ do /* 2 */ { /* 3 */ } diff --git a/packages/babel-generator/test/fixtures/async-do-expressions/inner-comments/output.js b/packages/babel-generator/test/fixtures/async-do-expressions/inner-comments/output.js new file mode 100644 index 000000000000..10ced6cf6aac --- /dev/null +++ b/packages/babel-generator/test/fixtures/async-do-expressions/inner-comments/output.js @@ -0,0 +1,5 @@ +async /* 1 */ do +/* 2 */ +{ + /* 3 */ +}; \ No newline at end of file From 390dadf9f10afd6cf95054e35f08a9e898ed3f37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Thu, 29 Sep 2022 15:04:28 -0400 Subject: [PATCH 02/11] fix: ensure no linebreak before import assertion --- .../babel-generator/src/generators/modules.ts | 39 +++++++++++++++---- packages/babel-generator/src/printer.ts | 18 ++++----- .../import-assertion-inner-comment/input.js | 1 + .../options.json | 3 ++ .../import-assertion-inner-comment/output.js | 3 ++ 5 files changed, 46 insertions(+), 18 deletions(-) create mode 100644 packages/babel-generator/test/fixtures/comments/import-assertion-inner-comment/input.js create mode 100644 packages/babel-generator/test/fixtures/comments/import-assertion-inner-comment/options.json create mode 100644 packages/babel-generator/test/fixtures/comments/import-assertion-inner-comment/output.js diff --git a/packages/babel-generator/src/generators/modules.ts b/packages/babel-generator/src/generators/modules.ts index 08c47bc17d9f..aed9cf13b593 100644 --- a/packages/babel-generator/src/generators/modules.ts +++ b/packages/babel-generator/src/generators/modules.ts @@ -80,9 +80,19 @@ export function ExportAllDeclaration( this.space(); this.word("from"); this.space(); - this.print(node.source, node); // @ts-expect-error Fixme: assertions is not defined in DeclareExportAllDeclaration - this.printAssertions(node); + if (node.assertions?.length) { + this.ensureNoLineTerminator(() => { + this.print(node.source, node); + this.space(); + this.word("assert"); + }); + // @ts-expect-error Fixme: assertions is not defined in DeclareExportAllDeclaration + this.printAssertions(node); + } else { + this.print(node.source, node); + } + this.semicolon(); } @@ -146,8 +156,16 @@ export function ExportNamedDeclaration( this.space(); this.word("from"); this.space(); - this.print(node.source, node); - this.printAssertions(node); + if (node.assertions?.length) { + this.ensureNoLineTerminator(() => { + this.print(node.source, node); + this.space(); + this.word("assert"); + }); + this.printAssertions(node); + } else { + this.print(node.source, node); + } } this.semicolon(); @@ -220,9 +238,16 @@ export function ImportDeclaration(this: Printer, node: t.ImportDeclaration) { this.space(); } - this.print(node.source, node); - - this.printAssertions(node); + if (node.assertions?.length) { + this.ensureNoLineTerminator(() => { + this.print(node.source, node); + this.space(); + this.word("assert"); + }); + this.printAssertions(node); + } else { + this.print(node.source, node); + } if (!process.env.BABEL_8_BREAKING) { // @ts-ignore(Babel 7 vs Babel 8) Babel 7 supports module attributes if (node.attributes?.length) { diff --git a/packages/babel-generator/src/printer.ts b/packages/babel-generator/src/printer.ts index 1d1c30c33712..7d39d5c7e7c3 100644 --- a/packages/babel-generator/src/printer.ts +++ b/packages/babel-generator/src/printer.ts @@ -1070,18 +1070,14 @@ class Printer { } } } - // todo(flow->ts): was Node printAssertions(node: Extract) { - if (node.assertions?.length) { - this.space(); - this.word("assert"); - this.space(); - this.token("{"); - this.space(); - this.printList(node.assertions, node); - this.space(); - this.token("}"); - } + this.space(); + this.printInnerComments(node); + this.token("{"); + this.space(); + this.printList(node.assertions, node); + this.space(); + this.token("}"); } } diff --git a/packages/babel-generator/test/fixtures/comments/import-assertion-inner-comment/input.js b/packages/babel-generator/test/fixtures/comments/import-assertion-inner-comment/input.js new file mode 100644 index 000000000000..a682899803dd --- /dev/null +++ b/packages/babel-generator/test/fixtures/comments/import-assertion-inner-comment/input.js @@ -0,0 +1 @@ +import "foo" /* 1 */ assert /* 2 */ { type: "json" } diff --git a/packages/babel-generator/test/fixtures/comments/import-assertion-inner-comment/options.json b/packages/babel-generator/test/fixtures/comments/import-assertion-inner-comment/options.json new file mode 100644 index 000000000000..a3a80a8b1511 --- /dev/null +++ b/packages/babel-generator/test/fixtures/comments/import-assertion-inner-comment/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["importAssertions"] +} diff --git a/packages/babel-generator/test/fixtures/comments/import-assertion-inner-comment/output.js b/packages/babel-generator/test/fixtures/comments/import-assertion-inner-comment/output.js new file mode 100644 index 000000000000..aa93403ae9c2 --- /dev/null +++ b/packages/babel-generator/test/fixtures/comments/import-assertion-inner-comment/output.js @@ -0,0 +1,3 @@ +import "foo" /* 1 */ assert + /* 2 */ +{ type: "json" }; \ No newline at end of file From c3b1abb03d886ab4adec1fb6261df91b5f3c0cf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Thu, 29 Sep 2022 15:15:24 -0400 Subject: [PATCH 03/11] fix: ensure no linebreak between yield and * --- .../src/generators/expressions.ts | 18 +++++++++++++----- .../comments/yield-star-inner-comment/input.js | 3 +++ .../yield-star-inner-comment/output.js | 5 +++++ 3 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 packages/babel-generator/test/fixtures/comments/yield-star-inner-comment/input.js create mode 100644 packages/babel-generator/test/fixtures/comments/yield-star-inner-comment/output.js diff --git a/packages/babel-generator/src/generators/expressions.ts b/packages/babel-generator/src/generators/expressions.ts index 5a7f690efc63..be0779be5f7d 100644 --- a/packages/babel-generator/src/generators/expressions.ts +++ b/packages/babel-generator/src/generators/expressions.ts @@ -233,12 +233,20 @@ export function YieldExpression(this: Printer, node: t.YieldExpression) { this.word("yield"); if (node.delegate) { + this.ensureNoLineTerminator(() => { + this.printInnerComments(node); + }); this.token("*"); - } - - if (node.argument) { - this.space(); - this.printTerminatorless(node.argument, node, false); + if (node.argument) { + this.space(); + // line terminators are allowed after yield* + this.print(node.argument, node); + } + } else { + if (node.argument) { + this.space(); + this.printTerminatorless(node.argument, node, false); + } } } diff --git a/packages/babel-generator/test/fixtures/comments/yield-star-inner-comment/input.js b/packages/babel-generator/test/fixtures/comments/yield-star-inner-comment/input.js new file mode 100644 index 000000000000..2da4cbddc5c2 --- /dev/null +++ b/packages/babel-generator/test/fixtures/comments/yield-star-inner-comment/input.js @@ -0,0 +1,3 @@ +function * loop() { + yield /* 1 */ * /* 2 */ loop(); +} diff --git a/packages/babel-generator/test/fixtures/comments/yield-star-inner-comment/output.js b/packages/babel-generator/test/fixtures/comments/yield-star-inner-comment/output.js new file mode 100644 index 000000000000..127b2ad57717 --- /dev/null +++ b/packages/babel-generator/test/fixtures/comments/yield-star-inner-comment/output.js @@ -0,0 +1,5 @@ +function* loop() { + yield /* 1 */* + /* 2 */ + loop(); +} \ No newline at end of file From 1d1ad27d572b914318ea03e1d3f932a9cf0eaba9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 30 Sep 2022 09:13:49 -0400 Subject: [PATCH 04/11] fixup import assertion --- packages/babel-generator/src/generators/modules.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-generator/src/generators/modules.ts b/packages/babel-generator/src/generators/modules.ts index aed9cf13b593..abffa0c1a1fc 100644 --- a/packages/babel-generator/src/generators/modules.ts +++ b/packages/babel-generator/src/generators/modules.ts @@ -239,8 +239,8 @@ export function ImportDeclaration(this: Printer, node: t.ImportDeclaration) { } if (node.assertions?.length) { + this.print(node.source, node, true); this.ensureNoLineTerminator(() => { - this.print(node.source, node); this.space(); this.word("assert"); }); From 1b3149f8c9a521e43a449b376bf0f35326bb628d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 30 Sep 2022 09:36:14 -0400 Subject: [PATCH 05/11] ensure no linebreak between async and */class key --- .../babel-generator/src/generators/methods.ts | 18 +++++++++++++++--- .../async-class-method-comment/input.js | 3 +++ .../async-class-method-comment/output.js | 8 ++++++++ .../input.js | 3 +++ .../output.js | 14 ++++++++++++++ .../input.js | 3 +++ .../output.js | 8 ++++++++ .../input.js | 3 +++ .../output.js | 8 ++++++++ 9 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 packages/babel-generator/test/fixtures/comments/async-class-method-comment/input.js create mode 100644 packages/babel-generator/test/fixtures/comments/async-class-method-comment/output.js create mode 100644 packages/babel-generator/test/fixtures/comments/async-class-method-computed-key-comment/input.js create mode 100644 packages/babel-generator/test/fixtures/comments/async-class-method-computed-key-comment/output.js create mode 100644 packages/babel-generator/test/fixtures/comments/async-generator-class-method-comment/input.js create mode 100644 packages/babel-generator/test/fixtures/comments/async-generator-class-method-comment/output.js create mode 100644 packages/babel-generator/test/fixtures/comments/async-generator-class-method-computed-key-comment/input.js create mode 100644 packages/babel-generator/test/fixtures/comments/async-generator-class-method-computed-key-comment/output.js diff --git a/packages/babel-generator/src/generators/methods.ts b/packages/babel-generator/src/generators/methods.ts index 51bcdadcfae5..2015be425950 100644 --- a/packages/babel-generator/src/generators/methods.ts +++ b/packages/babel-generator/src/generators/methods.ts @@ -25,7 +25,8 @@ export function _parameters( | t.TSFunctionType | t.TSConstructorType, ) { - for (let i = 0; i < parameters.length; i++) { + const paramLength = parameters.length; + for (let i = 0; i < paramLength; i++) { this._param(parameters[i], parent); if (i < parameters.length - 1) { @@ -33,6 +34,9 @@ export function _parameters( this.space(); } } + if (paramLength === 0) { + this.printInnerComments(parent); + } } export function _param( @@ -71,9 +75,10 @@ export function _methodHead(this: Printer, node: t.Method | t.TSDeclareMethod) { this.space(); } + const { _noLineTerminator } = this; if (node.async) { - // ensure `async` is in the same line with property name - this._catchUp("start", key.loc); + // ensure no line terminator between async and class element name / * + this._noLineTerminator = true; this.word("async"); this.space(); } @@ -84,16 +89,23 @@ export function _methodHead(this: Printer, node: t.Method | t.TSDeclareMethod) { kind === "init" ) { if (node.generator) { + if (node.async) { + this.printInnerComments(node); + } this.token("*"); + this._noLineTerminator = _noLineTerminator; } } if (node.computed) { this.token("["); + this._noLineTerminator = _noLineTerminator; this.print(key, node); this.token("]"); + this.printInnerComments(node); } else { this.print(key, node); + this._noLineTerminator = _noLineTerminator; } if ( diff --git a/packages/babel-generator/test/fixtures/comments/async-class-method-comment/input.js b/packages/babel-generator/test/fixtures/comments/async-class-method-comment/input.js new file mode 100644 index 000000000000..41537f60553c --- /dev/null +++ b/packages/babel-generator/test/fixtures/comments/async-class-method-comment/input.js @@ -0,0 +1,3 @@ +class C { + async /* 1 */ foo(/* 2 */) /* 3 */ {} +} diff --git a/packages/babel-generator/test/fixtures/comments/async-class-method-comment/output.js b/packages/babel-generator/test/fixtures/comments/async-class-method-comment/output.js new file mode 100644 index 000000000000..63a2f2a565d0 --- /dev/null +++ b/packages/babel-generator/test/fixtures/comments/async-class-method-comment/output.js @@ -0,0 +1,8 @@ +class C { + async /* 1 */foo( + /* 2 */ + ) + /* 3 */ + {} + +} \ No newline at end of file diff --git a/packages/babel-generator/test/fixtures/comments/async-class-method-computed-key-comment/input.js b/packages/babel-generator/test/fixtures/comments/async-class-method-computed-key-comment/input.js new file mode 100644 index 000000000000..08504fa88eb9 --- /dev/null +++ b/packages/babel-generator/test/fixtures/comments/async-class-method-computed-key-comment/input.js @@ -0,0 +1,3 @@ +class C { + async /* 1 */ [ /* 2 */ foo /* 3 */] /* 4 */ (/* 5 */) {} +} diff --git a/packages/babel-generator/test/fixtures/comments/async-class-method-computed-key-comment/output.js b/packages/babel-generator/test/fixtures/comments/async-class-method-computed-key-comment/output.js new file mode 100644 index 000000000000..0cbc3e2a3adc --- /dev/null +++ b/packages/babel-generator/test/fixtures/comments/async-class-method-computed-key-comment/output.js @@ -0,0 +1,14 @@ +class C { + async [ + /* 2 */ + foo + /* 3 */ + ] + /* 1 */ + + /* 4 */ + + /* 5 */ + () {} + +} \ No newline at end of file diff --git a/packages/babel-generator/test/fixtures/comments/async-generator-class-method-comment/input.js b/packages/babel-generator/test/fixtures/comments/async-generator-class-method-comment/input.js new file mode 100644 index 000000000000..3ff717f026b7 --- /dev/null +++ b/packages/babel-generator/test/fixtures/comments/async-generator-class-method-comment/input.js @@ -0,0 +1,3 @@ +class C { + async /* 1 */ * /* 2 */ foo(/* 3 */) /* 4 */ {} +} diff --git a/packages/babel-generator/test/fixtures/comments/async-generator-class-method-comment/output.js b/packages/babel-generator/test/fixtures/comments/async-generator-class-method-comment/output.js new file mode 100644 index 000000000000..53d406e37190 --- /dev/null +++ b/packages/babel-generator/test/fixtures/comments/async-generator-class-method-comment/output.js @@ -0,0 +1,8 @@ +class C { + async /* 1 */ /* 3 */* + /* 2 */ + foo() + /* 4 */ + {} + +} \ No newline at end of file diff --git a/packages/babel-generator/test/fixtures/comments/async-generator-class-method-computed-key-comment/input.js b/packages/babel-generator/test/fixtures/comments/async-generator-class-method-computed-key-comment/input.js new file mode 100644 index 000000000000..8d4ee08883c1 --- /dev/null +++ b/packages/babel-generator/test/fixtures/comments/async-generator-class-method-computed-key-comment/input.js @@ -0,0 +1,3 @@ +class C { + async /* 1 */ * /* 2 */ [ /* 3 */ foo /* 4 */] /* 5 */ (/* 6 */) {} +} diff --git a/packages/babel-generator/test/fixtures/comments/async-generator-class-method-computed-key-comment/output.js b/packages/babel-generator/test/fixtures/comments/async-generator-class-method-computed-key-comment/output.js new file mode 100644 index 000000000000..a883c47fcb24 --- /dev/null +++ b/packages/babel-generator/test/fixtures/comments/async-generator-class-method-computed-key-comment/output.js @@ -0,0 +1,8 @@ +class C { + async /* 1 */ /* 2 */ /* 5 */ /* 6 */*[ + /* 3 */ + foo + /* 4 */ + ]() {} + +} \ No newline at end of file From 94ec62492103f4512b8b216ea3081509748f41fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 30 Sep 2022 10:20:59 -0400 Subject: [PATCH 06/11] ensure no line terminator before => --- packages/babel-generator/src/generators/methods.ts | 7 +++++-- .../fixtures/comments/arrow-single-param-comment/input.js | 1 + .../fixtures/comments/arrow-single-param-comment/output.js | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 packages/babel-generator/test/fixtures/comments/arrow-single-param-comment/input.js create mode 100644 packages/babel-generator/test/fixtures/comments/arrow-single-param-comment/output.js diff --git a/packages/babel-generator/src/generators/methods.ts b/packages/babel-generator/src/generators/methods.ts index 2015be425950..2556aa1c425b 100644 --- a/packages/babel-generator/src/generators/methods.ts +++ b/packages/babel-generator/src/generators/methods.ts @@ -194,9 +194,12 @@ export function ArrowFunctionExpression( } this._predicate(node); + this.ensureNoLineTerminator(() => { + this.space(); + this.printInnerComments(node); + this.token("=>"); + }); - this.space(); - this.token("=>"); this.space(); this.print(node.body, node); diff --git a/packages/babel-generator/test/fixtures/comments/arrow-single-param-comment/input.js b/packages/babel-generator/test/fixtures/comments/arrow-single-param-comment/input.js new file mode 100644 index 000000000000..3429c0b66900 --- /dev/null +++ b/packages/babel-generator/test/fixtures/comments/arrow-single-param-comment/input.js @@ -0,0 +1 @@ +(x)/* 1 */=>{} diff --git a/packages/babel-generator/test/fixtures/comments/arrow-single-param-comment/output.js b/packages/babel-generator/test/fixtures/comments/arrow-single-param-comment/output.js new file mode 100644 index 000000000000..286aeba5d144 --- /dev/null +++ b/packages/babel-generator/test/fixtures/comments/arrow-single-param-comment/output.js @@ -0,0 +1 @@ +x /* 1 */=> {}; \ No newline at end of file From d47d8ad9745970972772c412e7ba2fd5c3ce4b7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 30 Sep 2022 11:07:55 -0400 Subject: [PATCH 07/11] polish: allow comments when printing single-binding arrow --- .../babel-generator/src/generators/methods.ts | 20 +++++++++---------- .../babel-generator/test/arrow-functions.js | 15 ++++---------- .../output.js | 6 +++--- .../async-arrow-function/output.js | 6 +++--- 4 files changed, 19 insertions(+), 28 deletions(-) diff --git a/packages/babel-generator/src/generators/methods.ts b/packages/babel-generator/src/generators/methods.ts index 2556aa1c425b..5efd17a53582 100644 --- a/packages/babel-generator/src/generators/methods.ts +++ b/packages/babel-generator/src/generators/methods.ts @@ -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); } @@ -205,7 +205,7 @@ export function ArrowFunctionExpression( this.print(node.body, node); } -function hasTypesOrComments( +function hasTypes( node: t.ArrowFunctionExpression, param: t.Identifier, ): boolean { @@ -214,8 +214,6 @@ function hasTypesOrComments( node.returnType || node.predicate || param.typeAnnotation || - param.optional || - param.leadingComments?.length || - param.trailingComments?.length + param.optional ); } diff --git a/packages/babel-generator/test/arrow-functions.js b/packages/babel-generator/test/arrow-functions.js index ec9cdc3c4b2b..537ca0c8da59 100644 --- a/packages/babel-generator/test/arrow-functions.js +++ b/packages/babel-generator/test/arrow-functions.js @@ -28,18 +28,15 @@ describe("parameter parentheses", () => { const output = generate(ast, { auxiliaryCommentBefore: "BEFORE" }).code; expect(output).toMatchInlineSnapshot(` "() => {}; - ( /*BEFORE*/ - a) => {}; + a => {}; ( /*BEFORE*/ a, /*BEFORE*/ b) => {}; async () => {}; - async ( - /*BEFORE*/ - a) => {}; + async /*BEFORE*/a => {}; async ( /*BEFORE*/ a, @@ -53,18 +50,14 @@ describe("parameter parentheses", () => { const output = generate(ast, { auxiliaryCommentAfter: "AFTER" }).code; expect(output).toMatchInlineSnapshot(` "() => {}; - (a - /*AFTER*/ - ) => {}; + a /*AFTER*/=> {}; (a /*AFTER*/ , b /*AFTER*/ ) => {}; async () => {}; - async (a - /*AFTER*/ - ) => {}; + async a /*AFTER*/=> {}; async (a /*AFTER*/ , b 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 addc834ee0ed..a1e62b4e986f 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 bf33d2ca48fc..f718e264aefc 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) { From 1306527a2720867a9912f969f498c33555f1a22b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 3 Oct 2022 20:31:26 -0400 Subject: [PATCH 08/11] review comment --- .../async-generator-class-method-comment/input.js | 2 +- .../async-generator-class-method-comment/output.js | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/babel-generator/test/fixtures/comments/async-generator-class-method-comment/input.js b/packages/babel-generator/test/fixtures/comments/async-generator-class-method-comment/input.js index 3ff717f026b7..0dd6805e1e51 100644 --- a/packages/babel-generator/test/fixtures/comments/async-generator-class-method-comment/input.js +++ b/packages/babel-generator/test/fixtures/comments/async-generator-class-method-comment/input.js @@ -1,3 +1,3 @@ class C { - async /* 1 */ * /* 2 */ foo(/* 3 */) /* 4 */ {} + async /* 1 */ * /* 2 */ foo /* 3 */ (/* 4 */) /* 5 */ {} } diff --git a/packages/babel-generator/test/fixtures/comments/async-generator-class-method-comment/output.js b/packages/babel-generator/test/fixtures/comments/async-generator-class-method-comment/output.js index 53d406e37190..2d9af81a4609 100644 --- a/packages/babel-generator/test/fixtures/comments/async-generator-class-method-comment/output.js +++ b/packages/babel-generator/test/fixtures/comments/async-generator-class-method-comment/output.js @@ -1,8 +1,10 @@ class C { - async /* 1 */ /* 3 */* + async /* 1 */ /* 4 */* /* 2 */ - foo() - /* 4 */ + foo + /* 3 */ + () + /* 5 */ {} } \ No newline at end of file From 43f826fc0ed0f34dcd6e41002a3d502933df7490 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Thu, 6 Oct 2022 17:48:27 +0200 Subject: [PATCH 09/11] Update fixtures --- .../async-do-expressions/inner-comments/output.js | 6 +----- .../comments/async-class-method-comment/output.js | 7 +------ .../output.js | 13 +------------ .../async-generator-class-method-comment/output.js | 9 +-------- .../output.js | 7 +------ .../import-assertion-inner-comment/output.js | 4 +--- .../comments/yield-star-inner-comment/output.js | 4 +--- .../arrow-single-optional-param/output.js | 2 +- .../flow-comments/arrow-single-param-type/output.js | 2 +- 9 files changed, 9 insertions(+), 45 deletions(-) diff --git a/packages/babel-generator/test/fixtures/async-do-expressions/inner-comments/output.js b/packages/babel-generator/test/fixtures/async-do-expressions/inner-comments/output.js index 10ced6cf6aac..a9654126b38b 100644 --- a/packages/babel-generator/test/fixtures/async-do-expressions/inner-comments/output.js +++ b/packages/babel-generator/test/fixtures/async-do-expressions/inner-comments/output.js @@ -1,5 +1 @@ -async /* 1 */ do -/* 2 */ -{ - /* 3 */ -}; \ No newline at end of file +async /* 1 */ do /* 2 */{/* 3 */}; \ No newline at end of file diff --git a/packages/babel-generator/test/fixtures/comments/async-class-method-comment/output.js b/packages/babel-generator/test/fixtures/comments/async-class-method-comment/output.js index 63a2f2a565d0..8302e190cd10 100644 --- a/packages/babel-generator/test/fixtures/comments/async-class-method-comment/output.js +++ b/packages/babel-generator/test/fixtures/comments/async-class-method-comment/output.js @@ -1,8 +1,3 @@ class C { - async /* 1 */foo( - /* 2 */ - ) - /* 3 */ - {} - + async /* 1 */foo( /* 2 */) /* 3 */{} } \ No newline at end of file diff --git a/packages/babel-generator/test/fixtures/comments/async-class-method-computed-key-comment/output.js b/packages/babel-generator/test/fixtures/comments/async-class-method-computed-key-comment/output.js index 0cbc3e2a3adc..dbca0b9748d5 100644 --- a/packages/babel-generator/test/fixtures/comments/async-class-method-computed-key-comment/output.js +++ b/packages/babel-generator/test/fixtures/comments/async-class-method-computed-key-comment/output.js @@ -1,14 +1,3 @@ class C { - async [ - /* 2 */ - foo - /* 3 */ - ] - /* 1 */ - - /* 4 */ - - /* 5 */ - () {} - + async [/* 2 */foo /* 3 */] /* 1 */ /* 4 */ /* 5 */() {} } \ No newline at end of file diff --git a/packages/babel-generator/test/fixtures/comments/async-generator-class-method-comment/output.js b/packages/babel-generator/test/fixtures/comments/async-generator-class-method-comment/output.js index 2d9af81a4609..89f3e529357b 100644 --- a/packages/babel-generator/test/fixtures/comments/async-generator-class-method-comment/output.js +++ b/packages/babel-generator/test/fixtures/comments/async-generator-class-method-comment/output.js @@ -1,10 +1,3 @@ class C { - async /* 1 */ /* 4 */* - /* 2 */ - foo - /* 3 */ - () - /* 5 */ - {} - + async /* 1 */ /* 4 */* /* 2 */foo /* 3 */() /* 5 */{} } \ No newline at end of file diff --git a/packages/babel-generator/test/fixtures/comments/async-generator-class-method-computed-key-comment/output.js b/packages/babel-generator/test/fixtures/comments/async-generator-class-method-computed-key-comment/output.js index a883c47fcb24..46783d555037 100644 --- a/packages/babel-generator/test/fixtures/comments/async-generator-class-method-computed-key-comment/output.js +++ b/packages/babel-generator/test/fixtures/comments/async-generator-class-method-computed-key-comment/output.js @@ -1,8 +1,3 @@ class C { - async /* 1 */ /* 2 */ /* 5 */ /* 6 */*[ - /* 3 */ - foo - /* 4 */ - ]() {} - + async /* 1 */ /* 2 */ /* 5 */ /* 6 */*[/* 3 */foo /* 4 */]() {} } \ No newline at end of file diff --git a/packages/babel-generator/test/fixtures/comments/import-assertion-inner-comment/output.js b/packages/babel-generator/test/fixtures/comments/import-assertion-inner-comment/output.js index aa93403ae9c2..15516625752a 100644 --- a/packages/babel-generator/test/fixtures/comments/import-assertion-inner-comment/output.js +++ b/packages/babel-generator/test/fixtures/comments/import-assertion-inner-comment/output.js @@ -1,3 +1 @@ -import "foo" /* 1 */ assert - /* 2 */ -{ type: "json" }; \ No newline at end of file +import "foo" /* 1 */ assert /* 2 */{ type: "json" }; \ No newline at end of file diff --git a/packages/babel-generator/test/fixtures/comments/yield-star-inner-comment/output.js b/packages/babel-generator/test/fixtures/comments/yield-star-inner-comment/output.js index 127b2ad57717..81c6fdfccdcf 100644 --- a/packages/babel-generator/test/fixtures/comments/yield-star-inner-comment/output.js +++ b/packages/babel-generator/test/fixtures/comments/yield-star-inner-comment/output.js @@ -1,5 +1,3 @@ function* loop() { - yield /* 1 */* - /* 2 */ - loop(); + yield /* 1 */* /* 2 */loop(); } \ No newline at end of file diff --git a/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/arrow-single-optional-param/output.js b/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/arrow-single-optional-param/output.js index b4c55a034047..97404c5ed653 100644 --- a/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/arrow-single-optional-param/output.js +++ b/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/arrow-single-optional-param/output.js @@ -1 +1 @@ -const x = (foo /*:: ?*/) => {}; +const x = foo /*:: ?*/ => {}; diff --git a/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/arrow-single-param-type/output.js b/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/arrow-single-param-type/output.js index e29c6196f0b9..fd89f9501670 100644 --- a/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/arrow-single-param-type/output.js +++ b/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/arrow-single-param-type/output.js @@ -1 +1 @@ -const x = (foo /*: string*/) => {}; +const x = foo /*: string*/ => {}; From bf8294441dd3f7834e08ef1a5742726a9d0de2c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 24 Oct 2022 10:40:18 -0400 Subject: [PATCH 10/11] address review comments --- packages/babel-generator/src/generators/methods.ts | 9 ++++++--- .../async-arrow-single-param-with-comments/output.js | 6 +++--- .../fixtures/parentheses/async-arrow-function/output.js | 6 +++--- 3 files changed, 12 insertions(+), 9 deletions(-) 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) { From b5b0132fb65a87aa7ebdcc52827cb1daca36a321 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 24 Oct 2022 10:54:05 -0400 Subject: [PATCH 11/11] update test fixtures --- .../flow-comments/arrow-single-optional-param/output.js | 2 +- .../fixtures/flow-comments/arrow-single-param-type/output.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/arrow-single-optional-param/output.js b/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/arrow-single-optional-param/output.js index 97404c5ed653..b4c55a034047 100644 --- a/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/arrow-single-optional-param/output.js +++ b/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/arrow-single-optional-param/output.js @@ -1 +1 @@ -const x = foo /*:: ?*/ => {}; +const x = (foo /*:: ?*/) => {}; diff --git a/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/arrow-single-param-type/output.js b/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/arrow-single-param-type/output.js index fd89f9501670..e29c6196f0b9 100644 --- a/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/arrow-single-param-type/output.js +++ b/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/arrow-single-param-type/output.js @@ -1 +1 @@ -const x = foo /*: string*/ => {}; +const x = (foo /*: string*/) => {};