Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: added async in allow method in no-empty-function (fixes #12768) #13036

Merged
merged 8 commits into from Mar 20, 2020
31 changes: 31 additions & 0 deletions docs/rules/no-empty-function.md
Expand Up @@ -177,6 +177,8 @@ This rule has an option to allow specific kinds of functions to be empty.
* `"getters"` - Getters.
* `"setters"` - Setters.
* `"constructors"` - Class constructors.
* `"asyncFunctions"` - Async functions.
* `"asyncMethods"` - Async class methods and method shorthands of object literals.

#### allow: functions

Expand Down Expand Up @@ -307,6 +309,35 @@ class A {
}
```

#### allow: asyncFunctions

Examples of **correct** code for the `{ "allow": ["asyncFunctions"] }` options:

```js
/*eslint no-empty-function: ["error", { "allow": ["asyncFunctions"] }]*/
/*eslint-env es2017*/

async function a(){}
```

#### allow: asyncMethods

Examples of **correct** code for the `{ "allow": ["asyncMethods"] }` options:

```js
/*eslint no-empty-function: ["error", { "allow": ["asyncMethods"] }]*/
/*eslint-env es2017*/

var obj = {
async foo() {}
};

class A {
async foo() {}
static async foo() {}
}
```

## When Not To Use It

If you don't want to be notified about empty functions, then it's safe to disable this rule.
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/no-empty-function.js
Expand Up @@ -23,7 +23,9 @@ const ALLOW_OPTIONS = Object.freeze([
"generatorMethods",
"getters",
"setters",
"constructors"
"constructors",
"asyncFunctions",
"asyncMethods"
]);

/**
Expand Down
57 changes: 50 additions & 7 deletions tests/lib/rules/no-empty-function.js
Expand Up @@ -24,7 +24,9 @@ const ALLOW_OPTIONS = Object.freeze([
"generatorMethods",
"getters",
"setters",
"constructors"
"constructors",
"asyncFunctions",
"asyncMethods"
]);

/**
Expand All @@ -36,24 +38,29 @@ const ALLOW_OPTIONS = Object.freeze([
*/
function toValidInvalid(patterns, item) {

const ecmaVersion =
item.parserOptions && item.parserOptions.ecmaVersion
? item.parserOptions.ecmaVersion
: 6;

// Valid Patterns
patterns.valid.push(
{
code: item.code.replace("{}", "{ bar(); }"),
parserOptions: { ecmaVersion: 6 }
parserOptions: { ecmaVersion }
},
{
code: item.code.replace("{}", "{ /* empty */ }"),
parserOptions: { ecmaVersion: 6 }
parserOptions: { ecmaVersion }
},
{
code: item.code.replace("{}", "{\n // empty\n}"),
parserOptions: { ecmaVersion: 6 }
parserOptions: { ecmaVersion }
},
{
code: `${item.code} // allow: ${item.allow}`,
options: [{ allow: [item.allow] }],
parserOptions: { ecmaVersion: 6 }
parserOptions: { ecmaVersion }
}
);

Expand All @@ -63,7 +70,7 @@ function toValidInvalid(patterns, item) {
patterns.invalid.push({
code: item.code,
errors: [error],
parserOptions: { ecmaVersion: 6 }
parserOptions: { ecmaVersion }
});
ALLOW_OPTIONS
.filter(allow => allow !== item.allow)
Expand All @@ -74,7 +81,7 @@ function toValidInvalid(patterns, item) {
code: `${item.code} // allow: ${allow}`,
errors: [error],
options: [{ allow: [allow] }],
parserOptions: { ecmaVersion: 6 }
parserOptions: { ecmaVersion }
});
});

Expand Down Expand Up @@ -261,13 +268,49 @@ ruleTester.run("no-empty-function", rule, [
messageId: "unexpected",
data: { name: "constructor" },
allow: "constructors"
},
{
code: "const foo = { async method() {} }",
allow: "asyncMethods",
messageId: "unexpected",
data: { name: "async method 'method'" },
parserOptions: { ecmaVersion: 8 }
},
{
code: "async function a(){}",
allow: "asyncFunctions",
messageId: "unexpected",
data: { name: "async function 'a'" },
parserOptions: { ecmaVersion: 8 }
},
{
code: "const foo = async function () {}",
messageId: "unexpected",
data: { name: "async function" },
allow: "asyncFunctions",
parserOptions: { ecmaVersion: 8 }
},
{
code: "class Foo { async bar() {} }",
messageId: "unexpected",
data: { name: "async method 'bar'" },
allow: "asyncMethods",
parserOptions: { ecmaVersion: 8 }
},
{
code: "const foo = async () => {};",
messageId: "unexpected",
data: { name: "async arrow function" },
allow: "arrowFunctions",
parserOptions: { ecmaVersion: 8 }
}
].reduce(toValidInvalid, {
valid: [
{
code: "var foo = () => 0;",
parserOptions: { ecmaVersion: 6 }
}

],
invalid: []
}));