eslint-plugin-unicorn
Version:
More than 300 powerful ESLint rules
181 lines (162 loc) • 4.59 kB
JavaScript
import {
isMethodCall,
isCallExpression,
isCallOrNewExpression,
isNullLiteral,
} from './ast/index.js';
import {getParenthesizedRange} from './utils/index.js';
const ERROR_MESSAGE_ID = 'error';
const SUGGESTION_REPLACE_MESSAGE_ID = 'replace';
const SUGGESTION_REMOVE_MESSAGE_ID = 'remove';
const messages = {
[ERROR_MESSAGE_ID]: 'Use `undefined` instead of `null`.',
[SUGGESTION_REPLACE_MESSAGE_ID]: 'Replace `null` with `undefined`.',
[SUGGESTION_REMOVE_MESSAGE_ID]: 'Remove `null`.',
};
const isLooseEqual = node => node.type === 'BinaryExpression' && ['==', '!='].includes(node.operator);
const isStrictEqual = node => node.type === 'BinaryExpression' && ['===', '!=='].includes(node.operator);
const isCallOrNewArgument = node => isCallOrNewExpression(node.parent) && node.parent.arguments.includes(node);
// The suggestion deletes everything after `node` up to the `null` and its parentheses, so it can only be offered when no comment sits in that range
function getRemoveSuggestion(context, node, nullNode) {
const {sourceCode} = context;
const [, start] = sourceCode.getRange(node);
const [, end] = getParenthesizedRange(nullNode, context);
if (sourceCode.getCommentsInside({range: [start, end]}).length > 0) {
return;
}
return {
messageId: SUGGESTION_REMOVE_MESSAGE_ID,
fix: fixer => fixer.removeRange([start, end]),
};
}
/**
@param {import('eslint').Rule.RuleContext} context
*/
const create = context => {
const {checkArguments, checkStrictEquality} = context.options[0];
context.on('Literal', node => {
if (
!isNullLiteral(node)
|| (!checkStrictEquality && isStrictEqual(node.parent))
|| (!checkArguments && isCallOrNewArgument(node))
// `Object.create(null)`, `Object.create(null, foo)`
|| (
isMethodCall(node.parent, {
object: 'Object',
method: 'create',
minimumArguments: 1,
maximumArguments: 2,
optionalCall: false,
optionalMember: false,
})
&& node.parent.arguments[0] === node
)
// `useRef(null)`
|| (
isCallExpression(node.parent, {
name: 'useRef',
argumentsLength: 1,
optionalCall: false,
optionalMember: false,
})
&& node.parent.arguments[0] === node
)
// `React.useRef(null)`
|| (
isMethodCall(node.parent, {
object: 'React',
method: 'useRef',
argumentsLength: 1,
optionalCall: false,
optionalMember: false,
})
&& node.parent.arguments[0] === node
)
// `foo.insertBefore(bar, null)`
|| (
isMethodCall(node.parent, {
method: 'insertBefore',
argumentsLength: 2,
optionalCall: false,
})
&& node.parent.arguments[1] === node
)
) {
return;
}
const {parent} = node;
const problem = {
node,
messageId: ERROR_MESSAGE_ID,
};
const useUndefinedFix = fixer => fixer.replaceText(node, 'undefined');
if (isLooseEqual(parent)) {
problem.fix = useUndefinedFix;
return problem;
}
const useUndefinedSuggestion = {
messageId: SUGGESTION_REPLACE_MESSAGE_ID,
fix: useUndefinedFix,
};
if (parent.type === 'ReturnStatement' && parent.argument === node) {
problem.suggest = [
getRemoveSuggestion(context, context.sourceCode.getFirstToken(parent), node),
useUndefinedSuggestion,
].filter(Boolean);
return problem;
}
// `const`, `using`, and `await using` declarations and destructuring patterns require an initializer
if (
parent.type === 'VariableDeclarator'
&& parent.init === node
&& parent.id.type === 'Identifier'
&& (parent.parent.kind === 'let' || parent.parent.kind === 'var')
) {
problem.suggest = [
getRemoveSuggestion(context, parent.id, node),
useUndefinedSuggestion,
].filter(Boolean);
return problem;
}
problem.suggest = [useUndefinedSuggestion];
return problem;
});
};
const schema = [
{
type: 'object',
additionalProperties: false,
properties: {
checkArguments: {
type: 'boolean',
description: 'Whether to check `null` used as a direct function call or constructor argument.',
},
checkStrictEquality: {
type: 'boolean',
description: 'Whether to check strict equality comparisons against `null`.',
},
},
},
];
/**
@type {import('eslint').Rule.RuleModule}
*/
const config = {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Disallow the use of the `null` literal.',
recommended: true,
},
fixable: 'code',
hasSuggestions: true,
schema,
defaultOptions: [{checkArguments: true, checkStrictEquality: false}],
messages,
languages: [
'js/js',
],
},
};
export default config;