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

no-null: Exclude useRef(null) and React.useRef(null) #890

Merged
merged 4 commits into from Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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