Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,52 @@ function getReportDescriptor(
const fix: ReportFixFunction = fixer =>
fixer.replaceTextRange(reportRange, newCode);

// #11840: Don't suggest optional-chain + strict-null when OR-chain has undefined check
if (
node.type === AST_NODE_TYPES.LogicalExpression &&
node.operator === '||' &&
parts.some(part => part.optional) &&
lastOperand.node.type === AST_NODE_TYPES.BinaryExpression &&
(lastOperand.node.operator === '===' ||
lastOperand.node.operator === '!==') &&
((lastOperand.node.left.type === AST_NODE_TYPES.Literal &&
lastOperand.node.left.value == null) ||
(lastOperand.node.right.type === AST_NODE_TYPES.Literal &&
lastOperand.node.right.value == null))
) {
// Check if any part of the OR-chain is "x === undefined" or "x !== undefined"
const checkForUndefined = (expr: TSESTree.Node): boolean => {
if (
expr.type === AST_NODE_TYPES.BinaryExpression &&
(expr.operator === '===' || expr.operator === '!==')
) {
return (
(expr.left.type === AST_NODE_TYPES.Identifier &&
expr.left.name === 'undefined') ||
(expr.right.type === AST_NODE_TYPES.Identifier &&
expr.right.name === 'undefined')
);
}
if (
expr.type === AST_NODE_TYPES.LogicalExpression &&
expr.operator === '||'
) {
return checkForUndefined(expr.left) || checkForUndefined(expr.right);
}
return false;
};

if (checkForUndefined(node)) {
return {
loc: {
end: sourceCode.getLocFromIndex(reportRange[1]),
start: sourceCode.getLocFromIndex(reportRange[0]),
},
messageId: 'preferOptionalChain',
};
}
}

return {
loc: {
end: sourceCode.getLocFromIndex(reportRange[1]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,44 @@ describe('chain ending with comparison', () => {
errors: [{ messageId: 'preferOptionalChain', suggestions: null }],
output: `undefined !== foo?.bar?.baz;`,
},
{
// https://github.com/typescript-eslint/typescript-eslint/issues/11840
code: `
declare const foo: { bar: number | null } | undefined;
if (foo === undefined || foo.bar === null) {
}
`,
errors: [
{
messageId: 'preferOptionalChain',
suggestions: [], // This should not offer suggestions as it's not a true error
},
],
},
{
code: `
declare const foo: { bar: number | null } | undefined;
if (foo.bar === null || foo === undefined) {
}
`,
errors: [{ messageId: 'preferOptionalChain', suggestions: [] }],
},
{
code: `
declare const foo: { bar: number | null } | undefined;
if (undefined === foo || null === foo.bar) {
}
`,
errors: [{ messageId: 'preferOptionalChain', suggestions: [] }],
},
{
code: `
declare const foo: { bar: { baz: number | null } } | undefined;
if (foo === undefined || foo.bar.baz === null) {
}
`,
errors: [{ messageId: 'preferOptionalChain', suggestions: [] }],
},
],
valid: [
'foo && foo.bar == undeclaredVar;',
Expand Down
Loading