Skip to content

Commit

Permalink
add tests and restructure function to reach 100% test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Fields committed Jun 18, 2018
1 parent 94819cc commit 0285e7c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
42 changes: 23 additions & 19 deletions lib/rules/prefer-arrow-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* core eslint `prefer-arrow-callback` rule code so that future updates to that code
* can be more easily applied here.
*/
/* eslint "complexity": [ "error", 18 ], "max-statements": [ "error", 14 ] */
/* eslint "complexity": [ "error", 18 ], "max-statements": [ "error", 15 ] */

/**
* @fileoverview A rule to suggest using arrow functions as callbacks.
Expand Down Expand Up @@ -45,21 +45,22 @@ function checkMetaProperty(node, metaName, propertyName) {
*/
function getVariableOfArguments(scope) {
const variables = scope.variables;
let variableObject = null;

for (let i = 0; i < variables.length; i += 1) {
const variable = variables[i];

if (variable.name === 'arguments') {
/*
* If there was a parameter which is named "arguments", the
* implicit "arguments" is not defined.
* So does fast return with null.
*/
return variable.identifiers.length === 0 ? variable : null;
/*
* If there was a parameter which is named "arguments", the
* implicit "arguments" is not defined.
* So does fast return with null.
*/
if (variable.name === 'arguments' && variable.identifiers.length === 0) {
variableObject = variable;
break;
}
}

return null;
return variableObject;
}

/**
Expand Down Expand Up @@ -96,10 +97,11 @@ function isBindThis(node, currentNode) {
*/
function getCallbackInfo(node, context) {
const retv = { isCallback: false, isLexicalThis: false };
let searchComplete = false;
let currentNode = node;
let parent = node.parent;

while (currentNode) {
while (currentNode && !searchComplete) {
switch (parent.type) {
// Checks parents recursively.

Expand All @@ -115,7 +117,7 @@ function getCallbackInfo(node, context) {
parent.parent.arguments[0].type === 'ThisExpression';
parent = parent.parent;
} else {
return retv;
searchComplete = true;
}
break;

Expand All @@ -129,17 +131,19 @@ function getCallbackInfo(node, context) {
if (retv.isCallback && astUtils.isMochaFunctionCall(parent, context.getScope())) {
retv.isMochaCallback = true;
}
return retv;
searchComplete = true;
break;

default:
return retv;
searchComplete = true;
}

currentNode = parent;
parent = parent.parent;
if (!searchComplete) {
currentNode = parent;
parent = parent.parent;
}
}

throw new Error('unreachable');
return retv;
}

/**
Expand Down Expand Up @@ -197,7 +201,7 @@ module.exports = {
* {Array<{this: boolean, super: boolean, meta: boolean}>}
* - this - A flag which shows there are one or more ThisExpression.
* - super - A flag which shows there are one or more Super.
* - meta - A flag which shows there are one or more MethProperty.
* - meta - A flag which shows there are one or more MetaProperty.
*/
let stack = [];

Expand Down
2 changes: 2 additions & 0 deletions test/rules/prefer-arrow-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ ruleTester.run('prefer-arrow-callback', rules['prefer-arrow-callback'], {
'foo(function bar() { arguments; }.bind(this));',
'foo(function bar() { super.a; });',
'foo(function bar() { super.a; }.bind(this));',
'() => super()',
'foo(function bar() { new.target; });',
'foo(function bar() { new.target; }.bind(this));',
'() => new.target',
'foo(function bar() { this; }.bind(this, somethingElse));',
// mocha-specific valid test cases
'before(function bar() {});',
Expand Down

0 comments on commit 0285e7c

Please sign in to comment.