Skip to content

Commit

Permalink
no-null: Allow Object.create(null, …) (#1432)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jul 19, 2021
1 parent ee9f609 commit 768f301
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 36 deletions.
45 changes: 10 additions & 35 deletions rules/no-null.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
const {
not,
matches,
methodCallSelector,
callExpressionSelector,
} = require('./selectors/index.js');
Expand All @@ -15,43 +14,23 @@ const messages = {
[SUGGESTION_REMOVE_MESSAGE_ID]: 'Remove `null`.',
};

const objectCreateSelector = methodCallSelector({
object: 'Object',
method: 'create',
argumentsLength: 1,
});

// `useRef(null)`
// eslint-disable-next-line unicorn/prevent-abbreviations
const useRefSelector = callExpressionSelector({name: 'useRef', argumentsLength: 1});

// `React.useRef(null)`
// eslint-disable-next-line unicorn/prevent-abbreviations
const reactUseRefSelector = methodCallSelector({
object: 'React',
method: 'useRef',
argumentsLength: 1,
});

const selector = [
'Literal',
'[raw="null"]',
not(`${matches([objectCreateSelector, useRefSelector, reactUseRefSelector])} > .arguments`),
not([
// `Object.create(null)`, `Object.create(null, foo)`
`${methodCallSelector({object: 'Object', method: 'create', minimumArguments: 1, maximumArguments: 2})} > .arguments:first-child`,
// `useRef(null)`
`${callExpressionSelector({name: 'useRef', argumentsLength: 1})} > .arguments:first-child`,
// `React.useRef(null)`
`${methodCallSelector({object: 'React', method: 'useRef', argumentsLength: 1})} > .arguments:first-child`,
// `foo.insertBefore(bar, null)`
`${methodCallSelector({method: 'insertBefore', argumentsLength: 2})}[arguments.0.type!="SpreadElement"] > .arguments:nth-child(2)`,
]),
].join('');

const isLooseEqual = node => node.type === 'BinaryExpression' && ['==', '!='].includes(node.operator);
const isStrictEqual = node => node.type === 'BinaryExpression' && ['===', '!=='].includes(node.operator);
const isSecondArgumentOfInsertBefore = node =>
node.parent.type === 'CallExpression' &&
!node.parent.optional &&
node.parent.arguments.length === 2 &&
node.parent.arguments[0].type !== 'SpreadElement' &&
node.parent.arguments[1] === node &&
node.parent.callee.type === 'MemberExpression' &&
!node.parent.callee.computed &&
!node.parent.callee.optional &&
node.parent.callee.property.type === 'Identifier' &&
node.parent.callee.property.name === 'insertBefore';

const create = context => {
const {checkStrictEquality} = {
Expand All @@ -66,10 +45,6 @@ const create = context => {
return;
}

if (isSecondArgumentOfInsertBefore(node)) {
return;
}

const problem = {
node,
messageId: ERROR_MESSAGE_ID,
Expand Down
4 changes: 3 additions & 1 deletion test/no-null.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ test({
valid: [
'let foo',
'Object.create(null)',
'Object.create(null, {foo: {value:1}})',
'let insertedNode = parentNode.insertBefore(newNode, null)',
// Not `null`
'const foo = "null";',
Expand Down Expand Up @@ -222,13 +223,14 @@ test({
// `callee.object.type` is not a `Identifier`
invalidTestCase('lib.Object.create(null)'),
// More/Less arguments
invalidTestCase('Object.create(null, "")'),
invalidTestCase('Object.create(...[null])'),
invalidTestCase('Object.create(null, bar, extraArgument)'),
invalidTestCase('foo.insertBefore(null)'),
invalidTestCase('foo.insertBefore(foo, null, bar)'),
invalidTestCase('foo.insertBefore(...[foo], null)'),
// Not in right position
invalidTestCase('foo.insertBefore(null, bar)'),
invalidTestCase('Object.create(bar, null)'),
],
});

Expand Down

0 comments on commit 768f301

Please sign in to comment.