Skip to content

Commit

Permalink
Merge pull request #38 from macklinu/fix/allow-object-create-null
Browse files Browse the repository at this point in the history
Allow Object.create(null) in no-mutating-assign
  • Loading branch information
graingert authored Feb 14, 2018
2 parents 87ea135 + 4948d0e commit 205861e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
21 changes: 20 additions & 1 deletion rules/no-mutating-assign.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,32 @@ const isObjectExpression = _.flow(
_.includes(_, ['ObjectExpression', 'ArrayExpression'])
);

const isObjectCreateNull = _.matches({
type: 'CallExpression',
callee: {
type: 'MemberExpression',
object: {
type: 'Identifier',
name: 'Object'
},
property: {
type: 'Identifier',
name: 'create'
}
},
arguments: [{
type: 'Literal',
value: null
}]
});

const isFunctionExpression = _.flow(
_.property('type'),
_.includes(_, ['FunctionExpression', 'ArrowFunctionExpression'])
);

function isAllowedFirstArgument(arg) {
return isObjectExpression(arg) || isFunctionExpression(arg);
return isObjectExpression(arg) || isObjectCreateNull(arg) || isFunctionExpression(arg);
}

const create = function (context) {
Expand Down
6 changes: 6 additions & 0 deletions test/no-mutating-assign.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ ruleTester.run('no-mutating-assign', rule, {
'Object.foo(a, b);',
'Object.assign({});',
'Object.assign({}, b);',
'Object.assign(Object.create(null));',
'Object.assign(Object.create(null), b);',
'Object.assign({}, b, c, d, e);',
'Object.assign({foo: 1, bar: 2}, b);',
'Object.assign([1, 2], b);',
Expand Down Expand Up @@ -58,6 +60,10 @@ ruleTester.run('no-mutating-assign', rule, {
{
code: 'function fn() {}; Object.assign(fn, b);',
errors: [error]
},
{
code: 'Object.assign(Object.create)',
errors: [error]
}
]
});

0 comments on commit 205861e

Please sign in to comment.