Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exceptions for literal property mutations #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions rules/no-mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,21 @@ function errorMessage(isCommonJs) {
return baseMessage + (isCommonJs ? '. You may want to activate the `commonjs` option for this rule' : '');
}

function makeException(exception) {
function makeExceptions(exception) {
if (!exception.object && !exception.property) {
return _.stubFalse;
}
let query = {type: 'MemberExpression'};
if (exception.object) {
query = _.assign(query, {object: {type: 'Identifier', name: exception.object}});
}
let queries = [];
const query = {type: 'MemberExpression'};
if (exception.property) {
query = _.assign(query, {property: {type: 'Identifier', name: exception.property}});
queries.push(_.assign(query, {property: {type: 'Identifier', name: exception.property}}));
queries.push(_.assign(query, {property: {type: 'Literal', value: exception.property}}));
}
if (exception.object) {
const objquery = {object: {type: 'Identifier', name: exception.object}};
queries = queries.length ? _.map(q => _.assign(q, objquery), queries) : [_.assign(query, objquery)];
}
return _.matches(query);
return _.map(_.matches, queries);
}

function isExempted(exceptions, node) {
Expand All @@ -68,7 +71,7 @@ function isExempted(exceptions, node) {
const create = function (context) {
const options = context.options[0] || {};
const acceptCommonJs = options.commonjs;
const exceptions = _.map(makeException, options.exceptions);
const exceptions = _.flatMap(makeExceptions, options.exceptions);
if (options.allowThis) {
exceptions.push(_.matches({type: 'MemberExpression', object: {type: 'ThisExpression'}}));
}
Expand Down
6 changes: 6 additions & 0 deletions test/no-mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ ruleTester.run('no-mutation', rule, {
{object: 'foo'}
]}]
},
{
code: 'foo["bar"] = {};',
options: [{exceptions: [
{property: 'bar'}
]}]
},
{
code: 'baz.propTypes = {};',
options: [{exceptions: [
Expand Down