From 33efe1900fa8a0c0640be634e4aae50382d214fa Mon Sep 17 00:00:00 2001 From: Dave Tonge Date: Mon, 14 Mar 2016 02:02:40 +0000 Subject: [PATCH 1/2] Initial attempt to add a no-array-mutation rule Currently a simplistic check that looks for calls of array methods that mutate. Not restricted to arrays currently so will actually block any method calls with push, pop, splice, shift, unshift, reverse --- index.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 70f2cb2..bfa6772 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,7 @@ "use strict"; +var arrayMethodPattern = /^(?:push|pop|shift|unshift|fill|reverse|splice)$/; + module.exports = { rules: { "no-let": function(context) { @@ -26,17 +28,27 @@ module.exports = { } } } - } }, + "no-array-mutation": function(context) { + return { + "Identifier": function(node) { + if (node.parent.type === "MemberExpression" && arrayMethodPattern.test(node.name)) { + context.report(node, "No array mutation allowed."); + } + } + } + } + }, configs: { recommended: { rules: { 'redux/no-let': 2, 'redux/no-this': 2, - 'redux/no-mutation': 2 + 'redux/no-mutation': 2, + 'redux/no-array-mutation': 2 } } - } + } }; From f3b187956f6542d18d9e3a1e50990fe47da5fa2d Mon Sep 17 00:00:00 2001 From: Dave Tonge Date: Mon, 14 Mar 2016 02:22:53 +0000 Subject: [PATCH 2/2] Only warning when the method is called --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index bfa6772..94c18ca 100644 --- a/index.js +++ b/index.js @@ -32,7 +32,9 @@ module.exports = { "no-array-mutation": function(context) { return { "Identifier": function(node) { - if (node.parent.type === "MemberExpression" && arrayMethodPattern.test(node.name)) { + if (node.parent.type === "MemberExpression" + && node.parent.parent.callee === node.parent + && arrayMethodPattern.test(node.name)) { context.report(node, "No array mutation allowed."); } }