Skip to content
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.
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 @@ -61,13 +61,24 @@ const analyzeAndChainOperand: OperandAnalyzer = (
chain,
) => {
switch (operand.comparisonType) {
case NullishComparisonType.Boolean:
case NullishComparisonType.Boolean: {
const nextOperand = chain.at(index + 1);
if (
nextOperand?.comparisonType ===
NullishComparisonType.NotStrictEqualNull &&
operand.comparedName.type === AST_NODE_TYPES.Identifier
) {
return null;
}
return [operand];
}

case NullishComparisonType.NotEqualNullOrUndefined:
return [operand];

case NullishComparisonType.NotStrictEqualNull: {
// handle `x !== null && x !== undefined`
const nextOperand = chain[index + 1] as ValidOperand | undefined;
const nextOperand = chain.at(index + 1);
if (
nextOperand?.comparisonType ===
NullishComparisonType.NotStrictEqualUndefined &&
Expand All @@ -94,7 +105,7 @@ const analyzeAndChainOperand: OperandAnalyzer = (

case NullishComparisonType.NotStrictEqualUndefined: {
// handle `x !== undefined && x !== null`
const nextOperand = chain[index + 1] as ValidOperand | undefined;
const nextOperand = chain.at(index + 1);
if (
nextOperand?.comparisonType ===
NullishComparisonType.NotStrictEqualNull &&
Expand Down Expand Up @@ -132,7 +143,7 @@ const analyzeOrChainOperand: OperandAnalyzer = (

case NullishComparisonType.StrictEqualNull: {
// handle `x === null || x === undefined`
const nextOperand = chain[index + 1] as ValidOperand | undefined;
const nextOperand = chain.at(index + 1);
if (
nextOperand?.comparisonType ===
NullishComparisonType.StrictEqualUndefined &&
Expand All @@ -159,7 +170,7 @@ const analyzeOrChainOperand: OperandAnalyzer = (

case NullishComparisonType.StrictEqualUndefined: {
// handle `x === undefined || x === null`
const nextOperand = chain[index + 1] as ValidOperand | undefined;
const nextOperand = chain.at(index + 1);
if (
nextOperand?.comparisonType === NullishComparisonType.StrictEqualNull &&
compareNodes(operand.comparedName, nextOperand.comparedName) ===
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,8 @@ describe('hand-crafted cases', () => {
'(function () {}) && function () {}.name;',
'(class Foo {}) && class Foo {}.constructor;',
"new Map().get('a') && new Map().get('a').what;",
// https://github.com/typescript-eslint/typescript-eslint/issues/7654
'data && data.value !== null;',
{
code: '<div /> && (<div />).wtf;',
parserOptions: { ecmaFeatures: { jsx: true } },
Expand Down