Skip to content

Commit

Permalink
Add allowThis option to no-mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmengels committed Jun 21, 2016
1 parent ccd926d commit 69982ad
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
This change log adheres to standards from [Keep a CHANGELOG](http://keepachangelog.com).

## [Unreleased]
- (none)
### Added
- Added `allowThis` option to [`no-mutation`]

## [1.2.0] - 2016-06-21
### Added
Expand Down
3 changes: 3 additions & 0 deletions docs/rules/no-mutation.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ This rule supports the following options:

`commonjs`: If set to `true`, then this rule will not report when assigning to or to a (sub-) property of `exports` or `module.exports`. Note that this will not report when reassigning or overwriting previous exports.

`allowThis`: If set to `true`, then this rule will not report when assigning to or to a (sub-) property of `this`.

`exceptions`: List of objects that describe exceptions to the rule. Each exception should have either or both `object` and `property` field set.

You can set the options like this:

```js
"fp/no-mutation": ["error", {
"commonjs": true,
"allowThis": true,
"exceptions": [
{"object": "foo", "property": "bar"}
]
Expand Down
6 changes: 6 additions & 0 deletions rules/no-mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ module.exports = function (context) {
var options = context.options[0] || {};
var acceptCommonJs = options.commonjs;
var exceptions = _.map(makeException, options.exceptions);
if (options.allowThis) {
exceptions.push(_.matches({type: 'MemberExpression', object: {type: 'ThisExpression'}}));
}
return {
AssignmentExpression: function (node) {
var isCommonJs = isCommonJsExport(node);
Expand All @@ -97,6 +100,9 @@ module.exports.schema = [{
commonjs: {
type: 'boolean'
},
allowThis: {
type: 'boolean'
},
exceptions: {
type: 'array',
items: {
Expand Down
24 changes: 24 additions & 0 deletions test/no-mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ ruleTester.run('no-mutation', rule, {
{object: 'foo', property: 'bar'},
{object: 'module', property: 'exports'}
]}]
},
{
code: 'this.foo = 100;',
options: [{allowThis: true}]
},
{
code: 'this.foo.bar = 100;',
options: [{allowThis: true}]
},
{
code: 'function bar() { this.foo = 100; }',
options: [{allowThis: true}]
}
],
invalid: [
Expand Down Expand Up @@ -217,6 +229,18 @@ ruleTester.run('no-mutation', rule, {
code: 'baz.propTypes = {};',
options: [{exceptions: [{}]}],
errors: [reassignmentError]
},
{
code: 'this.foo = 100;',
errors: [reassignmentError]
},
{
code: 'this.foo.bar = 100;',
errors: [reassignmentError]
},
{
code: 'function bar() { this.foo = 100; }',
errors: [reassignmentError]
}
]
});

0 comments on commit 69982ad

Please sign in to comment.