Skip to content

Commit

Permalink
Add no-mutation-props rule #1113
Browse files Browse the repository at this point in the history
Fix change requests mentioned in #1145
  • Loading branch information
ianschmitz authored and joeybaker committed Sep 7, 2017
1 parent 63eaf60 commit a0a700e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 33 deletions.
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ module.exports = {
'react/no-danger-with-children': 2,
'react/no-deprecated': 2,
'react/no-direct-mutation-state': 2,
'react/no-mutation-props': 2,
'react/no-find-dom-node': 2,
'react/no-is-mounted': 2,
'react/no-render-return-value': 2,
Expand Down
43 changes: 17 additions & 26 deletions lib/rules/no-mutation-props.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/**
* @fileoverview Prevent direct mutation of this.props
* @fileoverview Prevent mutation of this.props
* @author Ian Schmitz
*/
'use strict';

var has = require('has');
var Components = require('../util/Components');

// ------------------------------------------------------------------------------
Expand All @@ -14,36 +13,29 @@ var Components = require('../util/Components');
module.exports = {
meta: {
docs: {
description: 'Prevent direct mutation of this.props',
description: 'Prevent mutation of this.props',
category: 'Possible Errors',
recommended: true
recommended: false
}
},

create: Components.detect(function (context, components, utils) {

/**
* Checks if the component is valid
* @param {Object} component The component to process
* @returns {Boolean} True if the component is valid, false if not.
*/
function isValid(component) {
return Boolean(component && !component.mutateProps);
}

/**
* Reports this.props mutations for a given component
* @param {Object} component The component to process
*/
function reportMutations(component) {
var mutation;
for (var i = 0, j = component.mutations.length; i < j; i++) {
mutation = component.mutations[i];
if (!component.mutations) {
return;
}

component.mutations.forEach(function(mutation) {
context.report({
node: mutation,
message: 'Do not mutate props.'
message: 'A component must never modify its own props.'
});
}
});
}

// --------------------------------------------------------------------------
Expand All @@ -59,7 +51,7 @@ module.exports = {
}

item = node.left.object;
while (item.object.property) {
while (item.object && item.object.property) {
item = item.object;
}

Expand All @@ -68,20 +60,19 @@ module.exports = {
var mutations = component && component.mutations || [];
mutations.push(node.left.object);
components.set(node, {
mutateProps: true,
mutations: mutations
});
}
},

'Program:exit': function () {
var list = components.list();
for (var component in list) {
if (!has(list, component) || isValid(list[component])) {
continue;
}
reportMutations(list[component]);
}

Object.keys(list).forEach(function (key) {
var component = list[key];

reportMutations(component);
});
}
};

Expand Down
12 changes: 6 additions & 6 deletions tests/lib/rules/no-mutation-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ ruleTester.run('no-mutation-props', rule, {
].join('\n'),
parserOptions: parserOptions,
errors: [{
message: 'Do not mutate props.'
message: 'A component must never modify its own props.'
}]
}, {
code: [
Expand All @@ -89,7 +89,7 @@ ruleTester.run('no-mutation-props', rule, {
].join('\n'),
parserOptions: parserOptions,
errors: [{
message: 'Do not mutate props.'
message: 'A component must never modify its own props.'
}]
}, {
code: [
Expand All @@ -102,7 +102,7 @@ ruleTester.run('no-mutation-props', rule, {
].join('\n'),
parserOptions: parserOptions,
errors: [{
message: 'Do not mutate props.'
message: 'A component must never modify its own props.'
}]
}, {
code: [
Expand All @@ -116,11 +116,11 @@ ruleTester.run('no-mutation-props', rule, {
].join('\n'),
parserOptions: parserOptions,
errors: [{
message: 'Do not mutate props.',
message: 'A component must never modify its own props.',
line: 3,
column: 5
}, {
message: 'Do not mutate props.',
message: 'A component must never modify its own props.',
line: 4,
column: 5
}]
Expand All @@ -139,7 +139,7 @@ ruleTester.run('no-mutation-props', rule, {
].join('\n'),
parserOptions: parserOptions,
errors: [{
message: 'Do not mutate props.'
message: 'A component must never modify its own props.'
}]
}*/
]
Expand Down

0 comments on commit a0a700e

Please sign in to comment.