Skip to content

Commit

Permalink
no-null: Exclude useRef(null) and React.useRef(null) (#890)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Oct 21, 2020
1 parent 643169b commit 7a4dbf4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
23 changes: 20 additions & 3 deletions rules/no-null.js
Expand Up @@ -17,11 +17,28 @@ const objectCreateSelector = methodSelector({
length: 1
});

// `useRef(null)`
// eslint-disable-next-line unicorn/prevent-abbreviations
const useRefSelector = [
'CallExpression',
'[callee.type="Identifier"]',
'[callee.name="useRef"]',
'[arguments.length=1]',
'[arguments.0.type!="SpreadElement"]'
].join('');

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

const selector = [
`:not(${objectCreateSelector})`,
'>',
'Literal',
'[raw="null"]'
'[raw="null"]',
`:not(:matches(${[objectCreateSelector, useRefSelector, reactUseRefSelector].join(', ')}) > .arguments)`
].join('');

const isLooseEqual = node => node.type === 'BinaryExpression' && ['==', '!='].includes(node.operator);
Expand Down
4 changes: 4 additions & 0 deletions test/no-null.js
Expand Up @@ -67,6 +67,10 @@ test({
'Object.create(bar)',
'Object.create("null")',

// `React.useRef(null)`
'useRef(null)',
'React.useRef(null)',

// Ignored
'if (foo === null) {}',
'if (null === foo) {}',
Expand Down

0 comments on commit 7a4dbf4

Please sign in to comment.