From 4948d0eacf36dd56c8f443df8597344f89df9d7c Mon Sep 17 00:00:00 2001 From: Macklin Underdown Date: Sun, 11 Feb 2018 10:25:35 -0500 Subject: [PATCH] Allow Object.create(null) in no-mutating-assign --- rules/no-mutating-assign.js | 21 ++++++++++++++++++++- test/no-mutating-assign.js | 6 ++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/rules/no-mutating-assign.js b/rules/no-mutating-assign.js index bfca5ea..d6f40e7 100644 --- a/rules/no-mutating-assign.js +++ b/rules/no-mutating-assign.js @@ -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) { diff --git a/test/no-mutating-assign.js b/test/no-mutating-assign.js index 843e953..42fc6be 100644 --- a/test/no-mutating-assign.js +++ b/test/no-mutating-assign.js @@ -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);', @@ -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] } ] });