Skip to content

Commit

Permalink
Fix no-mocha-arrow fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmengels committed Dec 13, 2016
1 parent 3dc54a2 commit b486eba
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
6 changes: 5 additions & 1 deletion lib/rules/no-mocha-arrows.js
Expand Up @@ -47,7 +47,7 @@ module.exports = function (context) {
paramsRightParen = sourceCode.getTokenBefore(sourceCode.getTokenBefore(fn.body)),
paramsFullText =
sourceCode.text.slice(paramsLeftParen.range[0], paramsRightParen.range[1]),
functionKeyword = 'function ',
functionKeyword = 'function',
bodyText;

if (fn.async) {
Expand All @@ -57,6 +57,10 @@ module.exports = function (context) {
paramsFullText = paramsFullText.slice(5);
}

if (fn.params.length > 0) {
paramsFullText = '(' + sourceCode.text.slice(fn.params[0].start, R.last(fn.params).end) + ')';
}

if (fn.body.type === 'BlockStatement') {
// When it((...) => { ... }),
// simply replace '(...) => ' with 'function () '
Expand Down
34 changes: 27 additions & 7 deletions test/rules/no-mocha-arrows.js
Expand Up @@ -26,38 +26,48 @@ ruleTester.run('no-mocha-arrows', rules['no-mocha-arrows'], {
{
code: 'it(() => { assert(something, false); })',
errors: errors,
output: 'it(function () { assert(something, false); })'
output: 'it(function() { assert(something, false); })'
},
{
code: 'it(() => { assert(something, false); })',
globals: [ 'it' ],
errors: errors,
output: 'it(function () { assert(something, false); })'
output: 'it(function() { assert(something, false); })'
},
{
code: 'it(() => assert(something, false))',
errors: errors,
output: 'it(function () { return assert(something, false); })'
output: 'it(function() { return assert(something, false); })'
},
{
code: 'it(done => assert(something, false))',
errors: errors,
output: 'it(function(done) { return assert(something, false); })'
},
{
code: 'it("should be false", () => { assert(something, false); })',
errors: errors,
output: 'it("should be false", function () { assert(something, false); })'
output: 'it("should be false", function() { assert(something, false); })'
},
{
code: 'it.only(() => { assert(something, false); })',
errors: [ { message: 'Do not pass arrow functions to it.only()', column: 1, line: 1 } ],
output: 'it.only(function () { assert(something, false); })'
output: 'it.only(function() { assert(something, false); })'
},
{
code: 'it((done) => { assert(something, false); })',
errors: errors,
output: 'it(function (done) { assert(something, false); })'
output: 'it(function(done) { assert(something, false); })'
},
{
code: 'it(done => { assert(something, false); })',
errors: errors,
output: 'it(function(done) { assert(something, false); })'
},
{
code: 'it("should be false", () => {\n assert(something, false);\n})',
errors: errors,
output: 'it("should be false", function () {\n assert(something, false);\n})'
output: 'it("should be false", function() {\n assert(something, false);\n})'
},
{
code: 'it(async () => { assert(something, false) })',
Expand All @@ -69,6 +79,16 @@ ruleTester.run('no-mocha-arrows', rules['no-mocha-arrows'], {
errors: errors,
output: 'it(async function () { return assert(something, false); })'
},
{
code: 'it(async done => assert(something, false))',
errors: errors,
output: 'it(async function(done) { return assert(something, false); })'
},
{
code: 'it(async (done) => assert(something, false))',
errors: errors,
output: 'it(async function(done) { return assert(something, false); })'
},
{
code: 'it(async() => assert(something, false))',
errors: errors,
Expand Down

0 comments on commit b486eba

Please sign in to comment.