Skip to content

Commit

Permalink
fix(rules): fixes eslint-6.x compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ext committed Jul 3, 2019
1 parent 5baa68a commit 66975e7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"error",
"unix"
],
"no-prototype-builtins": "off",
"quotes": [
"error",
"single"
Expand Down
36 changes: 26 additions & 10 deletions lib/rules/array-callback-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,9 @@
* @author Alexander Afanasyev (based on Toru Nagashima's work)
*/

var astUtils

try {
astUtils = require('eslint/lib/util/ast-utils')
} catch (err) {
astUtils = require('eslint/lib/ast-utils')
}

var isElementArrayFinder = require('../is-element-array-finder')

var ANY_FUNCTION_PATTERN = /^(?:Function(?:Declaration|Expression)|ArrowFunctionExpression)$/
var TARGET_NODE_TYPE = /^(?:Arrow)?FunctionExpression$/
var TARGET_METHODS = /^(?:filter|map|reduce)$/

Expand Down Expand Up @@ -45,6 +38,29 @@ function getLocation (node, sourceCode) {
return node.id || node
}

/**
* Finds a function node from ancestors of a node.
* @param {ASTNode} node - A start node to find.
* @returns {Node|null} A found function node.
*/
function getUpperFunction (node) {
for (var currentNode = node; currentNode; currentNode = currentNode.parent) {
if (ANY_FUNCTION_PATTERN.test(currentNode.type)) {
return currentNode
}
}
return null
}

/**
* Checks whether or not a node is callee.
* @param {ASTNode} node - A node to check.
* @returns {boolean} Whether or not the node is callee.
*/
function isCallee (node) {
return node.parent.type === 'CallExpression' && node.parent.callee === node
}

/**
* Checks a given node is a MemberExpression node which has the specified name's
* property.
Expand Down Expand Up @@ -80,9 +96,9 @@ function isCallbackOfArrayMethod (node) {
break

case 'ReturnStatement':
var func = astUtils.getUpperFunction(parent)
var func = getUpperFunction(parent)

if (func === null || !astUtils.isCallee(func)) {
if (func === null || !isCallee(func)) {
return false
}
node = func.parent
Expand Down

0 comments on commit 66975e7

Please sign in to comment.