diff --git a/rules/no-null.js b/rules/no-null.js index 855868e76f..cd1e103851 100644 --- a/rules/no-null.js +++ b/rules/no-null.js @@ -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); diff --git a/test/no-null.js b/test/no-null.js index 3076584015..f70a3fcf95 100644 --- a/test/no-null.js +++ b/test/no-null.js @@ -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) {}',