Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix: added async in allow method in no-empty-function (fixes #12768) (#…
…13036)

* Fix: added async in allow method in no-empty-function

* Chore: added tests for async* allow

* Docs: updated docs for async method and funct

* Chore: more tests and test fixes and docs refactore

* Update docs/rules/no-empty-function.md

Co-Authored-By: YeonJuan <yeonjuan93@naver.com>

* Update docs/rules/no-empty-function.md

Co-Authored-By: YeonJuan <yeonjuan93@naver.com>

* Update docs/rules/no-empty-function.md

Co-Authored-By: YeonJuan <yeonjuan93@naver.com>

* Docs: fixed the ecma version for docs snippet

Co-authored-by: YeonJuan <yeonjuan93@naver.com>
  • Loading branch information
anikethsaha and yeonjuan committed Mar 20, 2020
1 parent f3788af commit 2260611
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 8 deletions.
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: []
}));

0 comments on commit 2260611

Please sign in to comment.