From 3eda08b2f36d572a744cbf1cfe82ca58d31e8786 Mon Sep 17 00:00:00 2001 From: rhysd Date: Wed, 26 Oct 2016 18:34:53 +0900 Subject: [PATCH 1/9] Make no-mocha-arrows rule fixable --- lib/rules/no-mocha-arrows.js | 28 +++++++++++++++++++++++++++- test/rules/no-mocha-arrows.js | 21 ++++++++++++++++----- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/lib/rules/no-mocha-arrows.js b/lib/rules/no-mocha-arrows.js index 674a884..bc9926a 100644 --- a/lib/rules/no-mocha-arrows.js +++ b/lib/rules/no-mocha-arrows.js @@ -50,7 +50,33 @@ module.exports = function (context) { fnArg = node.arguments.slice(-1)[0]; if (fnArg && fnArg.type === 'ArrowFunctionExpression') { if (isLikelyMochaGlobal(context.getScope(), name)) { - context.report(node, 'Do not pass arrow functions to ' + name + '()'); + context.report({ + node: node, + message: 'Do not pass arrow functions to ' + name + '()', + fix: function (fixer) { + var sourceCode = context.getSourceCode(), + paramsLeftParen = sourceCode.getFirstToken(fnArg), + paramsRightParen = sourceCode.getTokenBefore(sourceCode.getTokenBefore(fnArg.body)), + paramsFullText = + sourceCode.text.slice(paramsLeftParen.range[0], paramsRightParen.range[1]), + bodyText; + + if (fnArg.body.type === 'BlockStatement') { + // When it((...) => { ... }), + // simply replace '(...) => ' with 'function () ' + return fixer.replaceTextRange( + [ fnArg.start, fnArg.body.start ], + 'function ' + paramsFullText + ' ' + ); + } + + bodyText = sourceCode.text.slice(fnArg.body.range[0], fnArg.body.range[1]); + return fixer.replaceTextRange( + [ fnArg.start, fnArg.end ], + 'function ' + paramsFullText + ' { ' + bodyText + '; }' + ); + } + }); } } } diff --git a/test/rules/no-mocha-arrows.js b/test/rules/no-mocha-arrows.js index c9c5919..6798928 100644 --- a/test/rules/no-mocha-arrows.js +++ b/test/rules/no-mocha-arrows.js @@ -23,28 +23,39 @@ ruleTester.run('no-mocha-arrows', rules['no-mocha-arrows'], { { code: 'it(() => { assert(something, false); })', parserOptions: { ecmaVersion: 6 }, - errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ] + errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], + output: 'it(function () { assert(something, false); })' }, { code: 'it(() => { assert(something, false); })', globals: [ 'it' ], parserOptions: { ecmaVersion: 6 }, - errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ] + errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], + output: 'it(function () { assert(something, false); })' }, { code: 'it(() => assert(something, false))', parserOptions: { ecmaVersion: 6 }, - errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ] + errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], + output: 'it(function () { assert(something, false); })' }, { code: 'it("should be false", () => { assert(something, false); })', parserOptions: { ecmaVersion: 6 }, - errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ] + errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], + output: 'it("should be false", function () { assert(something, false); })' }, { code: 'it.only(() => { assert(something, false); })', parserOptions: { ecmaVersion: 6 }, - errors: [ { message: 'Do not pass arrow functions to it.only()', column: 1, line: 1 } ] + errors: [ { message: 'Do not pass arrow functions to it.only()', column: 1, line: 1 } ], + output: 'it.only(function () { assert(something, false); })' + }, + { + code: 'it("should be false", () => {\n assert(something, false);\n})', + parserOptions: { ecmaVersion: 6 }, + errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], + output: 'it("should be false", function () {\n assert(something, false);\n})' } ] From 912508067f0d571718984e6ee28f9b30f05b8482 Mon Sep 17 00:00:00 2001 From: rhysd Date: Thu, 27 Oct 2016 11:25:01 +0900 Subject: [PATCH 2/9] Add missing 'return' on translating arrow function to normal function --- lib/rules/no-mocha-arrows.js | 2 +- test/rules/no-mocha-arrows.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/rules/no-mocha-arrows.js b/lib/rules/no-mocha-arrows.js index bc9926a..8ddc5da 100644 --- a/lib/rules/no-mocha-arrows.js +++ b/lib/rules/no-mocha-arrows.js @@ -73,7 +73,7 @@ module.exports = function (context) { bodyText = sourceCode.text.slice(fnArg.body.range[0], fnArg.body.range[1]); return fixer.replaceTextRange( [ fnArg.start, fnArg.end ], - 'function ' + paramsFullText + ' { ' + bodyText + '; }' + 'function ' + paramsFullText + ' { return ' + bodyText + '; }' ); } }); diff --git a/test/rules/no-mocha-arrows.js b/test/rules/no-mocha-arrows.js index 6798928..3c3621f 100644 --- a/test/rules/no-mocha-arrows.js +++ b/test/rules/no-mocha-arrows.js @@ -37,7 +37,7 @@ ruleTester.run('no-mocha-arrows', rules['no-mocha-arrows'], { code: 'it(() => assert(something, false))', parserOptions: { ecmaVersion: 6 }, errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], - output: 'it(function () { assert(something, false); })' + output: 'it(function () { return assert(something, false); })' }, { code: 'it("should be false", () => { assert(something, false); })', @@ -52,10 +52,10 @@ ruleTester.run('no-mocha-arrows', rules['no-mocha-arrows'], { output: 'it.only(function () { assert(something, false); })' }, { - code: 'it("should be false", () => {\n assert(something, false);\n})', + code: 'it("should be false", () => {\n assert(something, false);\n})', parserOptions: { ecmaVersion: 6 }, errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], - output: 'it("should be false", function () {\n assert(something, false);\n})' + output: 'it("should be false", function () {\n assert(something, false);\n})' } ] From aa09e6ea2934c8686ebe9bdc1c965de3cfc56cdb Mon Sep 17 00:00:00 2001 From: rhysd Date: Thu, 27 Oct 2016 11:34:15 +0900 Subject: [PATCH 3/9] Add tests for generator function, async function and 'done' callback --- test/rules/no-mocha-arrows.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/test/rules/no-mocha-arrows.js b/test/rules/no-mocha-arrows.js index 3c3621f..7de9076 100644 --- a/test/rules/no-mocha-arrows.js +++ b/test/rules/no-mocha-arrows.js @@ -16,7 +16,16 @@ ruleTester.run('no-mocha-arrows', rules['no-mocha-arrows'], { code: 'function it () {}; it(() => { console.log("okay") })', parserOptions: { ecmaVersion: 6 } }, - 'it.only()' + 'it.only()', + 'it(function(done) { assert(something, false); done(); })', + { + code: 'it(function*() { assert(something, false) })', + parserOptions: { ecmaVersion: 6 } + }, + { + code: 'it(async function () { assert(something, false) })', + parserOptions: { ecmaVersion: 2017 } + } ], invalid: [ @@ -51,11 +60,23 @@ ruleTester.run('no-mocha-arrows', rules['no-mocha-arrows'], { errors: [ { message: 'Do not pass arrow functions to it.only()', column: 1, line: 1 } ], output: 'it.only(function () { assert(something, false); })' }, + { + code: 'it((done) => { assert(something, false); })', + parserOptions: { ecmaVersion: 6 }, + errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], + output: 'it(function (done) { assert(something, false); })' + }, { code: 'it("should be false", () => {\n assert(something, false);\n})', parserOptions: { ecmaVersion: 6 }, errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], output: 'it("should be false", function () {\n assert(something, false);\n})' + }, + { + code: 'it(async () => { assert(something, false) })', + parserOptions: { ecmaVersion: 2017 }, + errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], + output: 'it(async function () { assert(something, false) })' } ] From b479d362f57dfc2c6a62431ec6c0a9820cc37337 Mon Sep 17 00:00:00 2001 From: rhysd Date: Thu, 27 Oct 2016 12:02:40 +0900 Subject: [PATCH 4/9] Fix the case where async function is passed --- lib/rules/no-mocha-arrows.js | 12 ++++++++++-- test/rules/no-mocha-arrows.js | 6 ++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/rules/no-mocha-arrows.js b/lib/rules/no-mocha-arrows.js index 8ddc5da..d8efbb0 100644 --- a/lib/rules/no-mocha-arrows.js +++ b/lib/rules/no-mocha-arrows.js @@ -59,21 +59,29 @@ module.exports = function (context) { paramsRightParen = sourceCode.getTokenBefore(sourceCode.getTokenBefore(fnArg.body)), paramsFullText = sourceCode.text.slice(paramsLeftParen.range[0], paramsRightParen.range[1]), + functionKeyword = 'function ', bodyText; + if (fnArg.async) { + // When 'async' specified, take care about the keyword. + functionKeyword = 'async ' + functionKeyword; + // Strip 'async (...)' to '(...)' + paramsFullText = paramsFullText.slice(6); + } + if (fnArg.body.type === 'BlockStatement') { // When it((...) => { ... }), // simply replace '(...) => ' with 'function () ' return fixer.replaceTextRange( [ fnArg.start, fnArg.body.start ], - 'function ' + paramsFullText + ' ' + functionKeyword + paramsFullText + ' ' ); } bodyText = sourceCode.text.slice(fnArg.body.range[0], fnArg.body.range[1]); return fixer.replaceTextRange( [ fnArg.start, fnArg.end ], - 'function ' + paramsFullText + ' { return ' + bodyText + '; }' + functionKeyword + paramsFullText + ' { return ' + bodyText + '; }' ); } }); diff --git a/test/rules/no-mocha-arrows.js b/test/rules/no-mocha-arrows.js index 7de9076..f16a3b0 100644 --- a/test/rules/no-mocha-arrows.js +++ b/test/rules/no-mocha-arrows.js @@ -77,6 +77,12 @@ ruleTester.run('no-mocha-arrows', rules['no-mocha-arrows'], { parserOptions: { ecmaVersion: 2017 }, errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], output: 'it(async function () { assert(something, false) })' + }, + { + code: 'it(async () => assert(something, false))', + parserOptions: { ecmaVersion: 2017 }, + errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], + output: 'it(async function () { return assert(something, false); })' } ] From 55055b15738495542827b62e5f70334fe14a7d71 Mon Sep 17 00:00:00 2001 From: rhysd Date: Thu, 27 Oct 2016 19:51:09 +0900 Subject: [PATCH 5/9] Consider async() => ... pattern (no space before params) Fix https://github.com/lo1tuma/eslint-plugin-mocha/pull/112#discussion_r85296060 --- lib/rules/no-mocha-arrows.js | 6 +++--- test/rules/no-mocha-arrows.js | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/rules/no-mocha-arrows.js b/lib/rules/no-mocha-arrows.js index d8efbb0..274e7c7 100644 --- a/lib/rules/no-mocha-arrows.js +++ b/lib/rules/no-mocha-arrows.js @@ -64,9 +64,9 @@ module.exports = function (context) { if (fnArg.async) { // When 'async' specified, take care about the keyword. - functionKeyword = 'async ' + functionKeyword; - // Strip 'async (...)' to '(...)' - paramsFullText = paramsFullText.slice(6); + functionKeyword = 'async function'; + // Strip 'async (...)' to ' (...)' + paramsFullText = paramsFullText.slice(5); } if (fnArg.body.type === 'BlockStatement') { diff --git a/test/rules/no-mocha-arrows.js b/test/rules/no-mocha-arrows.js index f16a3b0..4dc7b64 100644 --- a/test/rules/no-mocha-arrows.js +++ b/test/rules/no-mocha-arrows.js @@ -83,6 +83,12 @@ ruleTester.run('no-mocha-arrows', rules['no-mocha-arrows'], { parserOptions: { ecmaVersion: 2017 }, errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], output: 'it(async function () { return assert(something, false); })' + }, + { + code: 'it(async() => assert(something, false))', + parserOptions: { ecmaVersion: 2017 }, + errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], + output: 'it(async function() { return assert(something, false); })' } ] From f132ec7c75882ec70a4314c68b1770991a216400 Mon Sep 17 00:00:00 2001 From: rhysd Date: Thu, 27 Oct 2016 19:59:45 +0900 Subject: [PATCH 6/9] Separate test runner by ecmaVersion Fix https://github.com/lo1tuma/eslint-plugin-mocha/pull/112#discussion_r85297128 --- test/rules/no-mocha-arrows.js | 50 ++++++++++++++++------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/test/rules/no-mocha-arrows.js b/test/rules/no-mocha-arrows.js index 4dc7b64..e3cfb82 100644 --- a/test/rules/no-mocha-arrows.js +++ b/test/rules/no-mocha-arrows.js @@ -2,91 +2,87 @@ var RuleTester = require('eslint').RuleTester, rules = require('../../').rules, - ruleTester = new RuleTester(), - expectedErrorMessage = 'Do not pass arrow functions to it()'; + es6RuleTester = new RuleTester({ + parserOptions: { ecmaVersion: 6 } + }), + expectedErrorMessage = 'Do not pass arrow functions to it()', + es2017RuleTester = new RuleTester({ + parserOptions: { ecmaVersion: 2017 } + }); -ruleTester.run('no-mocha-arrows', rules['no-mocha-arrows'], { +es6RuleTester.run('no-mocha-arrows', rules['no-mocha-arrows'], { valid: [ 'it()', 'it(function() { assert(something, false); })', 'it("should be false", function() { assert(something, false); })', - { - // In this example, `it` is not a global. - code: 'function it () {}; it(() => { console.log("okay") })', - parserOptions: { ecmaVersion: 6 } - }, + // In this example, `it` is not a global. + 'function it () {}; it(() => { console.log("okay") })', 'it.only()', 'it(function(done) { assert(something, false); done(); })', - { - code: 'it(function*() { assert(something, false) })', - parserOptions: { ecmaVersion: 6 } - }, - { - code: 'it(async function () { assert(something, false) })', - parserOptions: { ecmaVersion: 2017 } - } + 'it(function*() { assert(something, false) })' ], invalid: [ { code: 'it(() => { assert(something, false); })', - parserOptions: { ecmaVersion: 6 }, errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], output: 'it(function () { assert(something, false); })' }, { code: 'it(() => { assert(something, false); })', globals: [ 'it' ], - parserOptions: { ecmaVersion: 6 }, errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], output: 'it(function () { assert(something, false); })' }, { code: 'it(() => assert(something, false))', - parserOptions: { ecmaVersion: 6 }, errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], output: 'it(function () { return assert(something, false); })' }, { code: 'it("should be false", () => { assert(something, false); })', - parserOptions: { ecmaVersion: 6 }, errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], output: 'it("should be false", function () { assert(something, false); })' }, { code: 'it.only(() => { assert(something, false); })', - parserOptions: { ecmaVersion: 6 }, errors: [ { message: 'Do not pass arrow functions to it.only()', column: 1, line: 1 } ], output: 'it.only(function () { assert(something, false); })' }, { code: 'it((done) => { assert(something, false); })', - parserOptions: { ecmaVersion: 6 }, errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], output: 'it(function (done) { assert(something, false); })' }, { code: 'it("should be false", () => {\n assert(something, false);\n})', - parserOptions: { ecmaVersion: 6 }, errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], output: 'it("should be false", function () {\n assert(something, false);\n})' - }, + } + ] + +}); + +es2017RuleTester.run('no-mocha-arrows', rules['no-mocha-arrows'], { + + valid: [ + 'it(async function () { assert(something, false) })' + ], + + invalid: [ { code: 'it(async () => { assert(something, false) })', - parserOptions: { ecmaVersion: 2017 }, errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], output: 'it(async function () { assert(something, false) })' }, { code: 'it(async () => assert(something, false))', - parserOptions: { ecmaVersion: 2017 }, errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], output: 'it(async function () { return assert(something, false); })' }, { code: 'it(async() => assert(something, false))', - parserOptions: { ecmaVersion: 2017 }, errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], output: 'it(async function() { return assert(something, false); })' } From c5c28f1e0c3c3b6e7d896eb9a9916e4682aedf5d Mon Sep 17 00:00:00 2001 From: rhysd Date: Thu, 27 Oct 2016 20:01:49 +0900 Subject: [PATCH 7/9] DRY common errors Fix https://github.com/lo1tuma/eslint-plugin-mocha/pull/112#discussion_r85297707 --- test/rules/no-mocha-arrows.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/test/rules/no-mocha-arrows.js b/test/rules/no-mocha-arrows.js index e3cfb82..1bcef90 100644 --- a/test/rules/no-mocha-arrows.js +++ b/test/rules/no-mocha-arrows.js @@ -8,7 +8,8 @@ var RuleTester = require('eslint').RuleTester, expectedErrorMessage = 'Do not pass arrow functions to it()', es2017RuleTester = new RuleTester({ parserOptions: { ecmaVersion: 2017 } - }); + }), + errors = [ { message: expectedErrorMessage, column: 1, line: 1 } ]; es6RuleTester.run('no-mocha-arrows', rules['no-mocha-arrows'], { @@ -26,23 +27,23 @@ es6RuleTester.run('no-mocha-arrows', rules['no-mocha-arrows'], { invalid: [ { code: 'it(() => { assert(something, false); })', - errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], + errors: errors, output: 'it(function () { assert(something, false); })' }, { code: 'it(() => { assert(something, false); })', globals: [ 'it' ], - errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], + errors: errors, output: 'it(function () { assert(something, false); })' }, { code: 'it(() => assert(something, false))', - errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], + errors: errors, output: 'it(function () { return assert(something, false); })' }, { code: 'it("should be false", () => { assert(something, false); })', - errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], + errors: errors, output: 'it("should be false", function () { assert(something, false); })' }, { @@ -52,12 +53,12 @@ es6RuleTester.run('no-mocha-arrows', rules['no-mocha-arrows'], { }, { code: 'it((done) => { assert(something, false); })', - errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], + errors: errors, output: 'it(function (done) { assert(something, false); })' }, { code: 'it("should be false", () => {\n assert(something, false);\n})', - errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], + errors: errors, output: 'it("should be false", function () {\n assert(something, false);\n})' } ] @@ -73,17 +74,17 @@ es2017RuleTester.run('no-mocha-arrows', rules['no-mocha-arrows'], { invalid: [ { code: 'it(async () => { assert(something, false) })', - errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], + errors: errors, output: 'it(async function () { assert(something, false) })' }, { code: 'it(async () => assert(something, false))', - errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], + errors: errors, output: 'it(async function () { return assert(something, false); })' }, { code: 'it(async() => assert(something, false))', - errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ], + errors: errors, output: 'it(async function() { return assert(something, false); })' } ] From d025ceea51d7b1436e0df6b6f53649ea305dbe46 Mon Sep 17 00:00:00 2001 From: rhysd Date: Thu, 27 Oct 2016 20:11:33 +0900 Subject: [PATCH 8/9] Separate a function to fix as a helper Fix https://github.com/lo1tuma/eslint-plugin-mocha/pull/112#discussion_r85298196 --- lib/rules/no-mocha-arrows.js | 62 +++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/lib/rules/no-mocha-arrows.js b/lib/rules/no-mocha-arrows.js index 274e7c7..00ca039 100644 --- a/lib/rules/no-mocha-arrows.js +++ b/lib/rules/no-mocha-arrows.js @@ -41,6 +41,38 @@ module.exports = function (context) { return !R.find(R.propEq('name', name), scope.variables); } + function fixArrowFunction(fixer, fn) { + var sourceCode = context.getSourceCode(), + paramsLeftParen = sourceCode.getFirstToken(fn), + paramsRightParen = sourceCode.getTokenBefore(sourceCode.getTokenBefore(fn.body)), + paramsFullText = + sourceCode.text.slice(paramsLeftParen.range[0], paramsRightParen.range[1]), + functionKeyword = 'function ', + bodyText; + + if (fn.async) { + // When 'async' specified, take care about the keyword. + functionKeyword = 'async function'; + // Strip 'async (...)' to ' (...)' + paramsFullText = paramsFullText.slice(5); + } + + if (fn.body.type === 'BlockStatement') { + // When it((...) => { ... }), + // simply replace '(...) => ' with 'function () ' + return fixer.replaceTextRange( + [ fn.start, fn.body.start ], + functionKeyword + paramsFullText + ' ' + ); + } + + bodyText = sourceCode.text.slice(fn.body.range[0], fn.body.range[1]); + return fixer.replaceTextRange( + [ fn.start, fn.end ], + functionKeyword + paramsFullText + ' { return ' + bodyText + '; }' + ); + } + return { CallExpression: function (node) { var name = getCalleeName(node.callee), @@ -54,35 +86,7 @@ module.exports = function (context) { node: node, message: 'Do not pass arrow functions to ' + name + '()', fix: function (fixer) { - var sourceCode = context.getSourceCode(), - paramsLeftParen = sourceCode.getFirstToken(fnArg), - paramsRightParen = sourceCode.getTokenBefore(sourceCode.getTokenBefore(fnArg.body)), - paramsFullText = - sourceCode.text.slice(paramsLeftParen.range[0], paramsRightParen.range[1]), - functionKeyword = 'function ', - bodyText; - - if (fnArg.async) { - // When 'async' specified, take care about the keyword. - functionKeyword = 'async function'; - // Strip 'async (...)' to ' (...)' - paramsFullText = paramsFullText.slice(5); - } - - if (fnArg.body.type === 'BlockStatement') { - // When it((...) => { ... }), - // simply replace '(...) => ' with 'function () ' - return fixer.replaceTextRange( - [ fnArg.start, fnArg.body.start ], - functionKeyword + paramsFullText + ' ' - ); - } - - bodyText = sourceCode.text.slice(fnArg.body.range[0], fnArg.body.range[1]); - return fixer.replaceTextRange( - [ fnArg.start, fnArg.end ], - functionKeyword + paramsFullText + ' { return ' + bodyText + '; }' - ); + return fixArrowFunction(fixer, fnArg); } }); } From 153ec6dfdba948fd2a430fc1d6e4e7ba9b05489a Mon Sep 17 00:00:00 2001 From: rhysd Date: Wed, 2 Nov 2016 19:44:07 +0900 Subject: [PATCH 9/9] Use one test runner for all test cases Fix: https://github.com/lo1tuma/eslint-plugin-mocha/pull/112#discussion_r86108679 --- test/rules/no-mocha-arrows.js | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/test/rules/no-mocha-arrows.js b/test/rules/no-mocha-arrows.js index 1bcef90..f7a899d 100644 --- a/test/rules/no-mocha-arrows.js +++ b/test/rules/no-mocha-arrows.js @@ -2,16 +2,13 @@ var RuleTester = require('eslint').RuleTester, rules = require('../../').rules, - es6RuleTester = new RuleTester({ - parserOptions: { ecmaVersion: 6 } - }), - expectedErrorMessage = 'Do not pass arrow functions to it()', - es2017RuleTester = new RuleTester({ + ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2017 } }), + expectedErrorMessage = 'Do not pass arrow functions to it()', errors = [ { message: expectedErrorMessage, column: 1, line: 1 } ]; -es6RuleTester.run('no-mocha-arrows', rules['no-mocha-arrows'], { +ruleTester.run('no-mocha-arrows', rules['no-mocha-arrows'], { valid: [ 'it()', @@ -21,7 +18,8 @@ es6RuleTester.run('no-mocha-arrows', rules['no-mocha-arrows'], { 'function it () {}; it(() => { console.log("okay") })', 'it.only()', 'it(function(done) { assert(something, false); done(); })', - 'it(function*() { assert(something, false) })' + 'it(function*() { assert(something, false) })', + 'it(async function () { assert(something, false) })' ], invalid: [ @@ -60,18 +58,7 @@ es6RuleTester.run('no-mocha-arrows', rules['no-mocha-arrows'], { code: 'it("should be false", () => {\n assert(something, false);\n})', errors: errors, output: 'it("should be false", function () {\n assert(something, false);\n})' - } - ] - -}); - -es2017RuleTester.run('no-mocha-arrows', rules['no-mocha-arrows'], { - - valid: [ - 'it(async function () { assert(something, false) })' - ], - - invalid: [ + }, { code: 'it(async () => { assert(something, false) })', errors: errors,