Permalink
Browse files

Add exception support no-mutation rule

  • Loading branch information...
scottnonnenberg committed May 28, 2016
1 parent 1dc2114 commit 3ca46b22ce605e1b4dd78bf473dae8197833020f
Showing with 128 additions and 2 deletions.
  1. +37 −0 check_exceptions.js
  2. +9 −0 index.js
  3. +82 −2 test/test_no_mutation.js
View
@@ -0,0 +1,37 @@
function match(value, exception) {
return value === exception;
}
function checkExceptions(node, exceptions) {
if (!exceptions || !exceptions.length) {
return false;
}
var object = node.left.object.name;
var property = node.left.property.name;
for (var i = 0, length = exceptions.length; i < length; i += 1) {
var exception = exceptions[i];
var objectMatch = match(object, exception.object);
var propertyMatch = match(property, exception.property);
if (exception.object && exception.property) {
if (objectMatch && propertyMatch) {
return true;
}
}
else if (exception.object) {
if (objectMatch) {
return true;
}
}
else if (exception.property) {
if (propertyMatch) {
return true;
}
}
}
}
module.exports = checkExceptions;
View
@@ -1,5 +1,7 @@
"use strict";
var checkExceptions = require('./check_exceptions');
module.exports = {
rules: {
"no-let": function(context) {
@@ -21,6 +23,13 @@ module.exports = {
"no-mutation": function(context) {
return {
"AssignmentExpression": function(node) {
var options = context.options[0] || {};
var exceptions = options.exceptions || [];
if (checkExceptions(node, exceptions)) {
return;
}
if (node.left.type === "MemberExpression") {
context.report(node, "No object mutation allowed.");
}
View
@@ -17,10 +17,90 @@ ruleTester.run("no-mutation", rule, {
"const { x, y } = obj;",
"export const x = 4;",
"export const { x, y } = obj;",
"x = 4;",
"x = 4;", {
code: "module.exports = fn;",
options: [{
exceptions: [{
object: 'module',
property: 'exports',
}]
}]
}, {
code: "module.x = fn;",
options: [{
exceptions: [{
object: 'module',
}]
}]
}, {
code: "x.exports = fn;",
options: [{
exceptions: [{
property: 'blah',
}, {
property: 'exports',
}],
}]
}
],
invalid: [{
code: "obj.x = 4;",
code: "obj.x = 'no exceptions';",
errors: [{
message: "No object mutation allowed.",
}],
}, {
code: "obj.x = 'empty options object';",
options: [{}],
errors: [{
message: "No object mutation allowed.",
}],
}, {
code: "obj.x = 'empty exception array';",
options: [{
exceptions: [],
}],
errors: [{
message: "No object mutation allowed.",
}],
}, {
code: "obj.x = 'empty exception';",
options: [{
exceptions: [{}],
}],
errors: [{
message: "No object mutation allowed.",
}],
}, {
code: "obj.x = 'non-matching exceptions';",
options: [{
exceptions: [{
object: 'obj',
property: 'y',
}, {
object: 'item',
property: 'x',
}],
}],
errors: [{
message: "No object mutation allowed.",
}],
}, {
code: "obj.x = 'non-matching object exception';",
options: [{
exceptions: [{
object: 'item',
}],
}],
errors: [{
message: "No object mutation allowed.",
}],
}, {
code: "obj.x = 'non-matching property exception';",
options: [{
exceptions: [{
property: 'y',
}],
}],
errors: [{
message: "No object mutation allowed.",
}],

0 comments on commit 3ca46b2

Please sign in to comment.