From e5d9860e5702c6227b7b3d1bf30a98b90965c84b Mon Sep 17 00:00:00 2001 From: Joshua Ohlman Date: Mon, 13 Jul 2020 10:20:39 -0400 Subject: [PATCH 1/5] Always print params with parentheses for async arrow functions (#11830) --- packages/babel-generator/src/generators/methods.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/babel-generator/src/generators/methods.js b/packages/babel-generator/src/generators/methods.js index 1019aba04c7c..25506cea0566 100644 --- a/packages/babel-generator/src/generators/methods.js +++ b/packages/babel-generator/src/generators/methods.js @@ -109,7 +109,8 @@ export function ArrowFunctionExpression(node: Object) { if ( node.params.length === 1 && t.isIdentifier(firstParam) && - !hasTypes(node, firstParam) + !hasTypes(node, firstParam) && + !node.async ) { if ( this.format.retainLines && From 3c886bc48c205b80df1cf8f639ea7db6c5d2417b Mon Sep 17 00:00:00 2001 From: Joshua Ohlman Date: Tue, 14 Jul 2020 10:10:14 -0400 Subject: [PATCH 2/5] Add a failing test for async arrow functions with comments --- .../fixtures/parentheses/async-arrow-function/input.js | 10 ++++++++++ .../parentheses/async-arrow-function/options.json | 1 + .../parentheses/async-arrow-function/output.js | 10 ++++++++++ 3 files changed, 21 insertions(+) create mode 100644 packages/babel-generator/test/fixtures/parentheses/async-arrow-function/input.js create mode 100644 packages/babel-generator/test/fixtures/parentheses/async-arrow-function/options.json create mode 100644 packages/babel-generator/test/fixtures/parentheses/async-arrow-function/output.js diff --git a/packages/babel-generator/test/fixtures/parentheses/async-arrow-function/input.js b/packages/babel-generator/test/fixtures/parentheses/async-arrow-function/input.js new file mode 100644 index 000000000000..e47e7f048eb5 --- /dev/null +++ b/packages/babel-generator/test/fixtures/parentheses/async-arrow-function/input.js @@ -0,0 +1,10 @@ +const x = async ( + // some comment + a +) => { + return foo(await a); +}; + +function foo(a) { + return a; +} diff --git a/packages/babel-generator/test/fixtures/parentheses/async-arrow-function/options.json b/packages/babel-generator/test/fixtures/parentheses/async-arrow-function/options.json new file mode 100644 index 000000000000..0967ef424bce --- /dev/null +++ b/packages/babel-generator/test/fixtures/parentheses/async-arrow-function/options.json @@ -0,0 +1 @@ +{} 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 new file mode 100644 index 000000000000..e47e7f048eb5 --- /dev/null +++ b/packages/babel-generator/test/fixtures/parentheses/async-arrow-function/output.js @@ -0,0 +1,10 @@ +const x = async ( + // some comment + a +) => { + return foo(await a); +}; + +function foo(a) { + return a; +} From 63fadfad08b22f722ae337737b1033237ab966b5 Mon Sep 17 00:00:00 2001 From: Joshua Ohlman Date: Tue, 14 Jul 2020 10:20:55 -0400 Subject: [PATCH 3/5] Match test output to actual output --- .../fixtures/parentheses/async-arrow-function/output.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) 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 e47e7f048eb5..c25cb880ec3c 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,7 +1,5 @@ -const x = async ( - // some comment - a -) => { +const x = async ( // some comment +a) => { return foo(await a); }; From eda47a8420ef74163b41588c8034920adc6f99eb Mon Sep 17 00:00:00 2001 From: Joshua Ohlman Date: Wed, 15 Jul 2020 09:45:21 -0400 Subject: [PATCH 4/5] Move check for node.async to retain lines instead of always print parens --- packages/babel-generator/src/generators/methods.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/babel-generator/src/generators/methods.js b/packages/babel-generator/src/generators/methods.js index 25506cea0566..cac8f023aa61 100644 --- a/packages/babel-generator/src/generators/methods.js +++ b/packages/babel-generator/src/generators/methods.js @@ -109,11 +109,13 @@ export function ArrowFunctionExpression(node: Object) { if ( node.params.length === 1 && t.isIdentifier(firstParam) && - !hasTypes(node, firstParam) && - !node.async + !hasTypes(node, firstParam) ) { if ( - this.format.retainLines && + ( + this.format.retainLines || + node.async + ) && node.loc && node.body.loc && node.loc.start.line < node.body.loc.start.line From 8cae946514c6706a586e5a9dc77b9dba37fcf39d Mon Sep 17 00:00:00 2001 From: Joshua Ohlman Date: Wed, 15 Jul 2020 09:58:15 -0400 Subject: [PATCH 5/5] Fix linting & tests for parentheses/async-arrow-function --- packages/babel-generator/src/generators/methods.js | 5 +---- .../test/fixtures/parentheses/async-arrow-function/output.js | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/babel-generator/src/generators/methods.js b/packages/babel-generator/src/generators/methods.js index cac8f023aa61..e9d0c67bded7 100644 --- a/packages/babel-generator/src/generators/methods.js +++ b/packages/babel-generator/src/generators/methods.js @@ -112,10 +112,7 @@ export function ArrowFunctionExpression(node: Object) { !hasTypes(node, firstParam) ) { if ( - ( - this.format.retainLines || - node.async - ) && + (this.format.retainLines || node.async) && node.loc && node.body.loc && node.loc.start.line < node.body.loc.start.line 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 c25cb880ec3c..eaf8a9076d5a 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,5 +1,5 @@ const x = async ( // some comment -a) => { + a) => { return foo(await a); };