Skip to content

Commit

Permalink
Update: no-extra-parens supports async/await (refs #7101) (#7178)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea authored and nzakas committed Sep 20, 2016
1 parent 8e1fee1 commit 745343f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/ast-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ module.exports = {
/* falls through */

case "UnaryExpression":
case "AwaitExpression":
return 14;

case "UpdateExpression":
Expand Down
1 change: 1 addition & 0 deletions lib/rules/no-extra-parens.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ module.exports = {

UnaryExpression: dryUnaryUpdate,
UpdateExpression: dryUnaryUpdate,
AwaitExpression: dryUnaryUpdate,

VariableDeclarator(node) {
if (node.init && hasExcessParens(node.init) &&
Expand Down
31 changes: 29 additions & 2 deletions tests/lib/rules/no-extra-parens.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,13 @@ ruleTester.run("no-extra-parens", rule, {
"}"
].join("\n"),
parserOptions: { ecmaVersion: 6 }
}
},

// async/await
{ code: "async function a() { await (a + b) }", parserOptions: { ecmaVersion: 8 } },
{ code: "async function a() { await (a + await b) }", parserOptions: { ecmaVersion: 8 } },
{ code: "async function a() { (await a)() }", parserOptions: { ecmaVersion: 8 } },
{ code: "async function a() { new (await a) }", parserOptions: { ecmaVersion: 8 } },
],

invalid: [
Expand Down Expand Up @@ -662,6 +668,27 @@ ruleTester.run("no-extra-parens", rule, {
}
],
output: "b => { return c ? d = b : e = b; }"
}
},

// async/await
{
code: "async function a() { (await a) + (await b); }",
parserOptions: { ecmaVersion: 8 },
errors: [
{
message: "Gratuitous parentheses around expression.",
type: "AwaitExpression"
},
{
message: "Gratuitous parentheses around expression.",
type: "AwaitExpression"
},
],
output: "async function a() { await a + await b; }"
},
invalid("async function a() { await (a); }", "async function a() { await a; }", "Identifier", null, {parserOptions: { ecmaVersion: 8 }}),
invalid("async function a() { await (a()); }", "async function a() { await a(); }", "CallExpression", null, {parserOptions: { ecmaVersion: 8 }}),
invalid("async function a() { await (+a); }", "async function a() { await +a; }", "UnaryExpression", null, {parserOptions: { ecmaVersion: 8 }}),
invalid("async function a() { +(await a); }", "async function a() { +await a; }", "AwaitExpression", null, {parserOptions: { ecmaVersion: 8 }}),
]
});

0 comments on commit 745343f

Please sign in to comment.