From abfcd69cbb8941b7ae49007f34d68397a8a9fdac Mon Sep 17 00:00:00 2001 From: Anix Date: Thu, 12 Mar 2020 15:59:07 +0000 Subject: [PATCH 1/8] Fix: added async in allow method in no-empty-function --- lib/rules/no-empty-function.js | 4 +++- tests/lib/rules/no-empty-function.js | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/rules/no-empty-function.js b/lib/rules/no-empty-function.js index c57e66fd534..c74321158b3 100644 --- a/lib/rules/no-empty-function.js +++ b/lib/rules/no-empty-function.js @@ -23,7 +23,9 @@ const ALLOW_OPTIONS = Object.freeze([ "generatorMethods", "getters", "setters", - "constructors" + "constructors", + "asyncFunctions", + "asyncMethods" ]); /** diff --git a/tests/lib/rules/no-empty-function.js b/tests/lib/rules/no-empty-function.js index 71077842fb5..c07ddd88dd5 100644 --- a/tests/lib/rules/no-empty-function.js +++ b/tests/lib/rules/no-empty-function.js @@ -24,7 +24,9 @@ const ALLOW_OPTIONS = Object.freeze([ "generatorMethods", "getters", "setters", - "constructors" + "constructors", + "asyncFunctions", + "asyncMethods" ]); /** From 9d918efc9048aa1e5aedd7e253c0871c9a2c1761 Mon Sep 17 00:00:00 2001 From: Anix Date: Fri, 13 Mar 2020 06:42:13 +0000 Subject: [PATCH 2/8] Chore: added tests for async* allow --- tests/lib/rules/no-empty-function.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/lib/rules/no-empty-function.js b/tests/lib/rules/no-empty-function.js index c07ddd88dd5..d0a599fca64 100644 --- a/tests/lib/rules/no-empty-function.js +++ b/tests/lib/rules/no-empty-function.js @@ -269,6 +269,16 @@ ruleTester.run("no-empty-function", rule, [ { code: "var foo = () => 0;", parserOptions: { ecmaVersion: 6 } + }, + { + code: "const foo = { async method() {} }", + options: [{ allow: ["asyncMethods"] }], + parserOptions: { ecmaVersion: 8 } + }, + { + code: "async function a(){}", + options: [{ allow: ["asyncFunctions"] }], + parserOptions: { ecmaVersion: 8 } } ], invalid: [] From 897773cde4cdf59b2941427a0295a584abc058a2 Mon Sep 17 00:00:00 2001 From: Anix Date: Sun, 15 Mar 2020 14:42:32 +0000 Subject: [PATCH 3/8] Docs: updated docs for async method and funct --- docs/rules/no-empty-function.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/docs/rules/no-empty-function.md b/docs/rules/no-empty-function.md index c0c1a1ea98d..25b29f90beb 100644 --- a/docs/rules/no-empty-function.md +++ b/docs/rules/no-empty-function.md @@ -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"` - Class methods and method shorthands of object literals with async #### allow: functions @@ -307,6 +309,35 @@ class A { } ``` +#### allow: asyncFunctions + +Examples of **correct** code for `{ "allow": ["asyncFunctions"] }` options: + +```js +/*eslint no-empty-function: ["error", { "allow": ["asyncFunctions"] }]*/ +/*eslint-env es6*/ + +async function a(){} +``` + +#### allow: asyncMethods + +Examples of **correct** code for `{ "allow": ["asyncMethods"] }` options: + +```js +/*eslint no-empty-function: ["error", { "allow": ["asyncMethods"] }]*/ +/*eslint-env es6*/ + +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. From 1373f021f1f0b364dddcce3f6812f46f7a25e88b Mon Sep 17 00:00:00 2001 From: Anix Date: Tue, 17 Mar 2020 08:08:48 +0000 Subject: [PATCH 4/8] Chore: more tests and test fixes and docs refactore --- docs/rules/no-empty-function.md | 4 +- tests/lib/rules/no-empty-function.js | 63 +++++++++++++++++++++------- 2 files changed, 49 insertions(+), 18 deletions(-) diff --git a/docs/rules/no-empty-function.md b/docs/rules/no-empty-function.md index 25b29f90beb..c116b1d07aa 100644 --- a/docs/rules/no-empty-function.md +++ b/docs/rules/no-empty-function.md @@ -177,8 +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"` - Class methods and method shorthands of object literals with async + * `"asyncFunctions"` - Async Functions. + * `"asyncMethods"` - Async class methods and method shorthands of object literals. #### allow: functions diff --git a/tests/lib/rules/no-empty-function.js b/tests/lib/rules/no-empty-function.js index d0a599fca64..c43ec4a0cb8 100644 --- a/tests/lib/rules/no-empty-function.js +++ b/tests/lib/rules/no-empty-function.js @@ -38,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 } } ); @@ -65,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) @@ -76,7 +81,7 @@ function toValidInvalid(patterns, item) { code: `${item.code} // allow: ${allow}`, errors: [error], options: [{ allow: [allow] }], - parserOptions: { ecmaVersion: 6 } + parserOptions: { ecmaVersion } }); }); @@ -263,23 +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 } - }, - { - code: "const foo = { async method() {} }", - options: [{ allow: ["asyncMethods"] }], - parserOptions: { ecmaVersion: 8 } - }, - { - code: "async function a(){}", - options: [{ allow: ["asyncFunctions"] }], - parserOptions: { ecmaVersion: 8 } } + ], invalid: [] })); From 7d9974a0b28655d99cb7004222bdc3c32b2e9f42 Mon Sep 17 00:00:00 2001 From: Anix Date: Tue, 17 Mar 2020 16:54:48 +0530 Subject: [PATCH 5/8] Update docs/rules/no-empty-function.md Co-Authored-By: YeonJuan --- docs/rules/no-empty-function.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/rules/no-empty-function.md b/docs/rules/no-empty-function.md index c116b1d07aa..d0df178370e 100644 --- a/docs/rules/no-empty-function.md +++ b/docs/rules/no-empty-function.md @@ -311,7 +311,7 @@ class A { #### allow: asyncFunctions -Examples of **correct** code for `{ "allow": ["asyncFunctions"] }` options: +Examples of **correct** code for the `{ "allow": ["asyncFunctions"] }` options: ```js /*eslint no-empty-function: ["error", { "allow": ["asyncFunctions"] }]*/ From c773fed586ae7ac09698f2c397f016a408d506c3 Mon Sep 17 00:00:00 2001 From: Anix Date: Tue, 17 Mar 2020 16:55:09 +0530 Subject: [PATCH 6/8] Update docs/rules/no-empty-function.md Co-Authored-By: YeonJuan --- docs/rules/no-empty-function.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/rules/no-empty-function.md b/docs/rules/no-empty-function.md index d0df178370e..6d43cfe0805 100644 --- a/docs/rules/no-empty-function.md +++ b/docs/rules/no-empty-function.md @@ -322,7 +322,7 @@ async function a(){} #### allow: asyncMethods -Examples of **correct** code for `{ "allow": ["asyncMethods"] }` options: +Examples of **correct** code for the `{ "allow": ["asyncMethods"] }` options: ```js /*eslint no-empty-function: ["error", { "allow": ["asyncMethods"] }]*/ From 82219920c750216df87aa8b68a70885b01a4b6c8 Mon Sep 17 00:00:00 2001 From: Anix Date: Tue, 17 Mar 2020 16:55:23 +0530 Subject: [PATCH 7/8] Update docs/rules/no-empty-function.md Co-Authored-By: YeonJuan --- docs/rules/no-empty-function.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/rules/no-empty-function.md b/docs/rules/no-empty-function.md index 6d43cfe0805..a3f2b25622c 100644 --- a/docs/rules/no-empty-function.md +++ b/docs/rules/no-empty-function.md @@ -177,7 +177,7 @@ This rule has an option to allow specific kinds of functions to be empty. * `"getters"` - Getters. * `"setters"` - Setters. * `"constructors"` - Class constructors. - * `"asyncFunctions"` - Async Functions. + * `"asyncFunctions"` - Async functions. * `"asyncMethods"` - Async class methods and method shorthands of object literals. #### allow: functions From c39f2604dbfcd76424e7573d3ccced65752c12d7 Mon Sep 17 00:00:00 2001 From: Anix Date: Wed, 18 Mar 2020 18:39:49 +0000 Subject: [PATCH 8/8] Docs: fixed the ecma version for docs snippet --- docs/rules/no-empty-function.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/rules/no-empty-function.md b/docs/rules/no-empty-function.md index a3f2b25622c..0f8691c82d3 100644 --- a/docs/rules/no-empty-function.md +++ b/docs/rules/no-empty-function.md @@ -315,7 +315,7 @@ Examples of **correct** code for the `{ "allow": ["asyncFunctions"] }` options: ```js /*eslint no-empty-function: ["error", { "allow": ["asyncFunctions"] }]*/ -/*eslint-env es6*/ +/*eslint-env es2017*/ async function a(){} ``` @@ -326,7 +326,7 @@ Examples of **correct** code for the `{ "allow": ["asyncMethods"] }` options: ```js /*eslint no-empty-function: ["error", { "allow": ["asyncMethods"] }]*/ -/*eslint-env es6*/ +/*eslint-env es2017*/ var obj = { async foo() {} @@ -334,7 +334,7 @@ var obj = { class A { async foo() {} - static async foo() {} + static async foo() {} } ```