From 3a6811b3910d84cdfcb92d900dd7b61e58c7fe30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=9B=E5=AE=9A=E8=B0=94=E7=9A=84=E7=8C=AB?= Date: Sat, 1 Jul 2017 10:14:46 +0800 Subject: [PATCH 1/8] New: disallow identical tests --- lib/rules/no-identical-tests.js | 53 ++++++++++++++++++++++ tests/lib/rules/no-identical-tests.js | 64 +++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 lib/rules/no-identical-tests.js create mode 100644 tests/lib/rules/no-identical-tests.js diff --git a/lib/rules/no-identical-tests.js b/lib/rules/no-identical-tests.js new file mode 100644 index 00000000..9299c921 --- /dev/null +++ b/lib/rules/no-identical-tests.js @@ -0,0 +1,53 @@ +/** + * @fileoverview disallow identical tests + * @author 薛定谔的猫 + */ + +'use strict'; + +const utils = require('../utils'); + +// ------------------------------------------------------------------------------ +// Rule Definition +// ------------------------------------------------------------------------------ + +module.exports = { + meta: { + docs: { + description: 'disallow identical tests', + category: 'Tests', + recommended: true, + }, + fixable: null, // or "code" or "whitespace" + schema: [], + }, + + create (context) { + // ---------------------------------------------------------------------- + // Public + // ---------------------------------------------------------------------- + const message = 'This test case should not be identical to someone else.'; + const sourceCode = context.getSourceCode(); + + return { + Program (ast) { + utils.getTestInfo(context, ast).forEach(testRun => { + [testRun.valid, testRun.invalid].forEach(tests => { + const cache = Object.create(null); + (tests || []).forEach(test => { + const testCode = sourceCode.getText(test); + if (cache[testCode]) { + context.report({ + node: test, + message, + }); + } else { + cache[testCode] = true; + } + }); + }); + }); + }, + }; + }, +}; diff --git a/tests/lib/rules/no-identical-tests.js b/tests/lib/rules/no-identical-tests.js new file mode 100644 index 00000000..1b9c79a8 --- /dev/null +++ b/tests/lib/rules/no-identical-tests.js @@ -0,0 +1,64 @@ +/** + * @fileoverview disallow identical tests + * @author 薛定谔的猫 + */ + +'use strict'; + +// ------------------------------------------------------------------------------ +// Requirements +// ------------------------------------------------------------------------------ + +const rule = require('../../../lib/rules/no-identical-tests'); +const RuleTester = require('eslint').RuleTester; + +const ERROR = { message: 'This test case should not be identical to someone else.' }; + +// ------------------------------------------------------------------------------ +// Tests +// ------------------------------------------------------------------------------ + +const ruleTester = new RuleTester(); +ruleTester.run('no-identical-tests', rule, { + valid: [ + ` + new RuleTester().run('foo', bar, { + valid: [ + { code: 'foo' }, + { code: 'bar' } + ], + invalid: [] + }); + `, + ], + + invalid: [ + { + code: ` + new RuleTester().run('foo', bar, { + valid: [ + { code: 'foo' }, + { code: 'foo' } + ], + invalid: [] + }); + `, + errors: [ERROR], + }, + { + code: ` + new RuleTester().run('foo', bar, { + valid: [ + { code: 'foo' }, + { code: 'foo' }, + ], + invalid: [ + { code: 'foo', errors: ['bar'] }, + { code: 'foo', errors: ['bar'] }, + ] + }); + `, + errors: [ERROR, ERROR], + }, + ], +}); From 2dd49d411c83de28b877397e8d439ea21131c5ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=9B=E5=AE=9A=E8=B0=94=E7=9A=84=E7=8C=AB?= Date: Sat, 1 Jul 2017 10:50:37 +0800 Subject: [PATCH 2/8] comment. --- lib/rules/no-identical-tests.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/rules/no-identical-tests.js b/lib/rules/no-identical-tests.js index 9299c921..bb75f1f0 100644 --- a/lib/rules/no-identical-tests.js +++ b/lib/rules/no-identical-tests.js @@ -34,6 +34,7 @@ module.exports = { utils.getTestInfo(context, ast).forEach(testRun => { [testRun.valid, testRun.invalid].forEach(tests => { const cache = Object.create(null); + // to avoid tests being null (tests || []).forEach(test => { const testCode = sourceCode.getText(test); if (cache[testCode]) { From 409e5aa650922c07476caeffd2d85a7b029af508 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=9B=E5=AE=9A=E8=B0=94=E7=9A=84=E7=8C=AB?= Date: Sat, 1 Jul 2017 10:52:18 +0800 Subject: [PATCH 3/8] Chore: remove identical tests. --- tests/lib/rules/no-deprecated-report-api.js | 17 ----------------- tests/lib/rules/report-message-format.js | 20 -------------------- tests/lib/rules/require-meta-fixable.js | 8 -------- 3 files changed, 45 deletions(-) diff --git a/tests/lib/rules/no-deprecated-report-api.js b/tests/lib/rules/no-deprecated-report-api.js index db295992..f0eeafe0 100644 --- a/tests/lib/rules/no-deprecated-report-api.js +++ b/tests/lib/rules/no-deprecated-report-api.js @@ -31,23 +31,6 @@ ruleTester.run('no-deprecated-report-api', rule, { } }; `, - ` - module.exports = { - create(context) { - context.report({ - node, - message: "Foo." - }); - } - }; - `, - ` - module.exports = { - create(context) { - foo.report(bar, baz); - } - }; - `, ` module.exports = { create(context) { diff --git a/tests/lib/rules/report-message-format.js b/tests/lib/rules/report-message-format.js index 81389d70..c2ec438d 100644 --- a/tests/lib/rules/report-message-format.js +++ b/tests/lib/rules/report-message-format.js @@ -63,26 +63,6 @@ ruleTester.run('report-message-format', rule, { `, options: ['^foo$'], }, - { - code: ` - module.exports = { - create(context) { - context.report(node, 'not foo' + message); - } - }; - `, - options: ['^foo$'], - }, - { - code: ` - module.exports = { - create(context) { - context.report({node, message: 'foo'}); - } - }; - `, - options: ['^foo$'], - }, { code: ` module.exports = { diff --git a/tests/lib/rules/require-meta-fixable.js b/tests/lib/rules/require-meta-fixable.js index 11407156..a4a8c9bf 100644 --- a/tests/lib/rules/require-meta-fixable.js +++ b/tests/lib/rules/require-meta-fixable.js @@ -37,14 +37,6 @@ ruleTester.run('require-meta-fixable', rule, { } }; `, - ` - module.exports = { - meta: { fixable: 'code' }, - create(context) { - context.report({node, message, fix: foo}); - } - }; - `, ` module.exports = { meta: { fixable: 'whitespace' }, From 41c8252a78cad0d6ffca539055238b8c1d5259af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=9B=E5=AE=9A=E8=B0=94=E7=9A=84=E7=8C=AB?= Date: Sat, 1 Jul 2017 11:47:13 +0800 Subject: [PATCH 4/8] Update: recommended: false --- lib/rules/no-identical-tests.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/no-identical-tests.js b/lib/rules/no-identical-tests.js index bb75f1f0..659619f2 100644 --- a/lib/rules/no-identical-tests.js +++ b/lib/rules/no-identical-tests.js @@ -16,7 +16,7 @@ module.exports = { docs: { description: 'disallow identical tests', category: 'Tests', - recommended: true, + recommended: false, }, fixable: null, // or "code" or "whitespace" schema: [], From 83610cd6a9f8015b0083515717605088c1c98533 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=9B=E5=AE=9A=E8=B0=94=E7=9A=84=E7=8C=AB?= Date: Sat, 1 Jul 2017 11:49:17 +0800 Subject: [PATCH 5/8] Update: error msg. --- lib/rules/no-identical-tests.js | 2 +- tests/lib/rules/no-identical-tests.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/rules/no-identical-tests.js b/lib/rules/no-identical-tests.js index 659619f2..f0aa0cb7 100644 --- a/lib/rules/no-identical-tests.js +++ b/lib/rules/no-identical-tests.js @@ -26,7 +26,7 @@ module.exports = { // ---------------------------------------------------------------------- // Public // ---------------------------------------------------------------------- - const message = 'This test case should not be identical to someone else.'; + const message = 'This test case is identical to another case.'; const sourceCode = context.getSourceCode(); return { diff --git a/tests/lib/rules/no-identical-tests.js b/tests/lib/rules/no-identical-tests.js index 1b9c79a8..4f46611f 100644 --- a/tests/lib/rules/no-identical-tests.js +++ b/tests/lib/rules/no-identical-tests.js @@ -12,7 +12,7 @@ const rule = require('../../../lib/rules/no-identical-tests'); const RuleTester = require('eslint').RuleTester; -const ERROR = { message: 'This test case should not be identical to someone else.' }; +const ERROR = { message: 'This test case is identical to another case.' }; // ------------------------------------------------------------------------------ // Tests From 642802c372a64e3ab54951e2c6f35b53a2db2878 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=9B=E5=AE=9A=E8=B0=94=E7=9A=84=E7=8C=AB?= Date: Sat, 1 Jul 2017 12:09:51 +0800 Subject: [PATCH 6/8] Docs: no-identical-tests.md --- docs/rules/no-identical-tests.md | 39 ++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 docs/rules/no-identical-tests.md diff --git a/docs/rules/no-identical-tests.md b/docs/rules/no-identical-tests.md new file mode 100644 index 00000000..c5a17071 --- /dev/null +++ b/docs/rules/no-identical-tests.md @@ -0,0 +1,39 @@ +# Disallow identical tests (no-identical-tests) + +When a rule has a lot of tests, it's sometimes difficult to tell if any tests are duplicates. This rule would warn if any test cases have the same properties. + +## Rule Details + +Examples of **incorrect** code for this rule: + +```js +/* eslint eslint-plugin/consistent-output: error */ + +new RuleTester().run('foo', bar, { +valid: [ + { code: 'foo' }, + { code: 'foo' } +], +invalid: [] +}); + +``` + +Examples of **correct** code for this rule: + +```js +/* eslint eslint-plugin/consistent-output: error */ + +new RuleTester().run('foo', bar, { +valid: [ + { code: 'foo' }, + { code: 'bar' } +], +invalid: [] +}); + +``` + +## When Not To Use It + +If you want to allow identical tests, do not enable this rule. From c58b8b997af4e0273c858ae49b5174288aa35f46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=9B=E5=AE=9A=E8=B0=94=E7=9A=84=E7=8C=AB?= Date: Sat, 1 Jul 2017 12:19:55 +0800 Subject: [PATCH 7/8] Docs: no-identical-tests in readme.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 468e05b6..0fa75232 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ Name | ✔️ | 🛠 | Description [prefer-placeholders](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/prefer-placeholders.md) | | | Disallows template literals as report messages [no-useless-token-range](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/no-useless-token-range.md) | ✔️ | 🛠 | Disallows unnecessary calls to sourceCode.getFirstToken and sourceCode.getLastToken [consistent-output](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/consistent-output.md) | | | Enforces consistent use of output assertions in rule tests +[no-identical-tests](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/no-identical-tests.md) | | | Disallows identical tests ## Supported Presets From d3f2f327358fd3e42a3d98782fecb5be747cf7af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=9B=E5=AE=9A=E8=B0=94=E7=9A=84=E7=8C=AB?= Date: Sat, 1 Jul 2017 05:59:24 +0100 Subject: [PATCH 8/8] docs: fix rulename --- docs/rules/no-identical-tests.md | 78 ++++++++++++++++---------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/docs/rules/no-identical-tests.md b/docs/rules/no-identical-tests.md index c5a17071..53e76155 100644 --- a/docs/rules/no-identical-tests.md +++ b/docs/rules/no-identical-tests.md @@ -1,39 +1,39 @@ -# Disallow identical tests (no-identical-tests) - -When a rule has a lot of tests, it's sometimes difficult to tell if any tests are duplicates. This rule would warn if any test cases have the same properties. - -## Rule Details - -Examples of **incorrect** code for this rule: - -```js -/* eslint eslint-plugin/consistent-output: error */ - -new RuleTester().run('foo', bar, { -valid: [ - { code: 'foo' }, - { code: 'foo' } -], -invalid: [] -}); - -``` - -Examples of **correct** code for this rule: - -```js -/* eslint eslint-plugin/consistent-output: error */ - -new RuleTester().run('foo', bar, { -valid: [ - { code: 'foo' }, - { code: 'bar' } -], -invalid: [] -}); - -``` - -## When Not To Use It - -If you want to allow identical tests, do not enable this rule. +# Disallow identical tests (no-identical-tests) + +When a rule has a lot of tests, it's sometimes difficult to tell if any tests are duplicates. This rule would warn if any test cases have the same properties. + +## Rule Details + +Examples of **incorrect** code for this rule: + +```js +/* eslint eslint-plugin/no-identical-tests: error */ + +new RuleTester().run('foo', bar, { +valid: [ + { code: 'foo' }, + { code: 'foo' } +], +invalid: [] +}); + +``` + +Examples of **correct** code for this rule: + +```js +/* eslint eslint-plugin/no-identical-tests: error */ + +new RuleTester().run('foo', bar, { +valid: [ + { code: 'foo' }, + { code: 'bar' } +], +invalid: [] +}); + +``` + +## When Not To Use It + +If you want to allow identical tests, do not enable this rule.