Skip to content

Commit 9e93e54

Browse files
committed
refactor: Move some ast decisions to their own module
1 parent fc4f434 commit 9e93e54

File tree

4 files changed

+46
-26
lines changed

4 files changed

+46
-26
lines changed

package-lock.json

Lines changed: 24 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"@freaktechnik/eslint-config-test": "^5.2.1",
1616
"ava": "^0.23.0",
1717
"codecov": "^3.0.0",
18-
"eslint": "^4.9.0",
18+
"eslint": "^4.10.0",
1919
"eslint-ava-rule-tester": "^2.0.2",
2020
"eslint-plugin-eslint-plugin": "^1.2.0",
2121
"nyc": "^11.3.0"

rules/from-map.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
*/
55
"use strict";
66

7+
const {
8+
isMethod,
9+
getParent,
10+
isOnObject
11+
} = require("../lib/helpers/call-expression");
12+
const { ARROW_FUNCTION_EXPRESSION } = require("../lib/type");
13+
714
module.exports = {
815
meta: {
916
docs: {
@@ -16,13 +23,13 @@ module.exports = {
1623
create(context) {
1724
return {
1825
"CallExpression:exit"(node) {
19-
if(!node.callee || node.callee.type !== "MemberExpression" || node.callee.property.name !== "map") {
26+
if(!isMethod(node, "map")) {
2027
return;
2128
}
2229
const { callee } = node,
23-
{ object: parent } = callee;
30+
parent = getParent(node);
2431

25-
if(!parent.callee || parent.callee.type !== "MemberExpression" || parent.callee.property.name !== "from" || !parent.callee.object || parent.callee.object.type !== "Identifier" || parent.callee.object.name !== "Array") {
32+
if(!isMethod(parent, "from") || !isOnObject(parent, "Array")) {
2633
return;
2734
}
2835

@@ -56,7 +63,7 @@ module.exports = {
5663
paramString = params.map((p) => p.name).join(PARAM_SEPARATOR),
5764
getCallback = (cbk, targ, ps) => {
5865
const source = `(${sourceCode.getText(cbk)})`;
59-
if(targ && cbk.type !== "ArrowFunctionExpression") {
66+
if(targ && cbk.type !== ARROW_FUNCTION_EXPRESSION) {
6067
return `${source}.call(${targ.name}${PARAM_SEPARATOR}${ps})`;
6168
}
6269
return `${source}(${ps})`;
@@ -67,7 +74,7 @@ module.exports = {
6774
let functionStart = `(${paramString}) => `,
6875
functionEnd = "",
6976
restParamString = '';
70-
if(thisArg && callback.type !== "ArrowFunctionExpression") {
77+
if(thisArg && callback.type !== ARROW_FUNCTION_EXPRESSION) {
7178
functionStart = `function(${paramString}) { return `;
7279
functionEnd = "; }";
7380
}

rules/no-unnecessary-this-arg.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
*/
55
"use strict";
66

7+
const {
8+
isMethod,
9+
isOnObject
10+
} = require("../lib/helpers/call-expression");
11+
const { ARROW_FUNCTION_EXPRESSION } = require("../lib/type");
12+
713
const arrayFunctions = {
814
from: 3
915
},
@@ -20,9 +26,10 @@ const arrayFunctions = {
2026
METHOD_ARG = 2,
2127
POS_TO_ARRAY = 1,
2228
FUNC_POS = -2,
23-
checkFunction = (node, functionName, argumentPosition) => !node.callee || node.callee.type !== "MemberExpression" || node.callee.property.name !== functionName || node.arguments.length < argumentPosition || node.arguments[argumentPosition + FUNC_POS].type !== "ArrowFunctionExpression",
29+
//TODO should also check if the identifier is not an arrow function expression. Similar problem to array detection.
30+
checkFunction = (node, functionName, argumentPosition) => !isMethod(node, functionName) || node.arguments.length < argumentPosition || node.arguments[argumentPosition + FUNC_POS].type !== ARROW_FUNCTION_EXPRESSION,
2431
checkArrayFunction = (functionName, paramPosition, node, context) => {
25-
if(checkFunction(node, functionName, paramPosition) || node.callee.object.name !== "Array") {
32+
if(checkFunction(node, functionName, paramPosition) || !isOnObject(node, "Array")) {
2633
return;
2734
}
2835
const argument = node.arguments[paramPosition - POS_TO_ARRAY];

0 commit comments

Comments
 (0)