Skip to content

Commit

Permalink
Update: Ensure arrow-parens handles async arrow functions correctly (
Browse files Browse the repository at this point in the history
…#7176)

(refs #7101)
  • Loading branch information
not-an-aardvark authored and nzakas committed Sep 19, 2016
1 parent 2d10657 commit 16bb802
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/rules/arrow-parens.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module.exports = {
* @returns {void}
*/
function parens(node) {
const token = sourceCode.getFirstToken(node);
const token = sourceCode.getFirstToken(node, node.async ? 1 : 0);

// "as-needed", { "requireForBlockBody": true }: x => x
if (
Expand Down
56 changes: 54 additions & 2 deletions tests/lib/rules/arrow-parens.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const valid = [
{ code: "(a) => {\n}", parserOptions: { ecmaVersion: 6 } },
{ code: "a.then((foo) => {});", parserOptions: { ecmaVersion: 6 } },
{ code: "a.then((foo) => { if (true) {}; });", parserOptions: { ecmaVersion: 6 } },
{ code: "a.then(async (foo) => { if (true) {}; });", parserOptions: { ecmaVersion: 8 } },

// "always" (explicit)
{ code: "() => {}", options: ["always"], parserOptions: { ecmaVersion: 6 } },
Expand All @@ -34,6 +35,7 @@ const valid = [
{ code: "(a) => {\n}", options: ["always"], parserOptions: { ecmaVersion: 6 } },
{ code: "a.then((foo) => {});", options: ["always"], parserOptions: { ecmaVersion: 6 } },
{ code: "a.then((foo) => { if (true) {}; });", options: ["always"], parserOptions: { ecmaVersion: 6 } },
{ code: "a.then(async (foo) => { if (true) {}; });", options: ["always"], parserOptions: { ecmaVersion: 8 } },

// "as-needed"
{ code: "() => {}", options: ["as-needed"], parserOptions: { ecmaVersion: 6 } },
Expand All @@ -44,6 +46,8 @@ const valid = [
{ code: "(a = 10) => {}", options: ["as-needed"], parserOptions: { ecmaVersion: 6 } },
{ code: "(...a) => a[0]", options: ["as-needed"], parserOptions: { ecmaVersion: 6 } },
{ code: "(a, b) => {}", options: ["as-needed"], parserOptions: { ecmaVersion: 6 } },
{ code: "async ([a, b]) => {}", options: ["as-needed"], parserOptions: { ecmaVersion: 8 } },
{ code: "async (a, b) => {}", options: ["as-needed"], parserOptions: { ecmaVersion: 8 } },

// "as-needed", { "requireForBlockBody": true }
{ code: "() => {}", options: ["as-needed", {requireForBlockBody: true}], parserOptions: { ecmaVersion: 6 } },
Expand All @@ -55,8 +59,9 @@ const valid = [
{ code: "(a = 10) => {}", options: ["as-needed", {requireForBlockBody: true}], parserOptions: { ecmaVersion: 6 } },
{ code: "(...a) => a[0]", options: ["as-needed", {requireForBlockBody: true}], parserOptions: { ecmaVersion: 6 } },
{ code: "(a, b) => {}", options: ["as-needed", {requireForBlockBody: true}], parserOptions: { ecmaVersion: 6 } },
{ code: "a => ({})", options: ["as-needed", {requireForBlockBody: true}], parserOptions: { ecmaVersion: 6 } }

{ code: "a => ({})", options: ["as-needed", {requireForBlockBody: true}], parserOptions: { ecmaVersion: 6 } },
{ code: "async a => ({})", options: ["as-needed", {requireForBlockBody: true}], parserOptions: { ecmaVersion: 8 } },
{ code: "async a => a", options: ["as-needed", {requireForBlockBody: true}], parserOptions: { ecmaVersion: 8 } }
];

const message = "Expected parentheses around arrow function argument.";
Expand Down Expand Up @@ -134,6 +139,17 @@ const invalid = [
type
}]
},
{
code: "a(async foo => { if (true) {}; });",
output: "a(async (foo) => { if (true) {}; });",
parserOptions: { ecmaVersion: 8 },
errors: [{
line: 1,
column: 3,
message,
type
}]
},

// "as-needed"
{
Expand All @@ -148,6 +164,18 @@ const invalid = [
type
}]
},
{
code: "async (a) => a",
output: "async a => a",
options: ["as-needed"],
parserOptions: { ecmaVersion: 8 },
errors: [{
line: 1,
column: 1,
message: asNeededMessage,
type
}]
},

// "as-needed", { "requireForBlockBody": true }
{
Expand All @@ -173,6 +201,30 @@ const invalid = [
message: requireForBlockBodyMessage,
type
}]
},
{
code: "async a => {}",
output: "async (a) => {}",
options: ["as-needed", {requireForBlockBody: true}],
parserOptions: { ecmaVersion: 8 },
errors: [{
line: 1,
column: 1,
message: requireForBlockBodyNoParensMessage,
type
}]
},
{
code: "async (a) => a",
output: "async a => a",
options: ["as-needed", {requireForBlockBody: true}],
parserOptions: { ecmaVersion: 8 },
errors: [{
line: 1,
column: 1,
message: requireForBlockBodyMessage,
type
}]
}
];

Expand Down

0 comments on commit 16bb802

Please sign in to comment.