diff --git a/packages/eslint-plugin/src/rules/prefer-optional-chain-utils/analyzeChain.ts b/packages/eslint-plugin/src/rules/prefer-optional-chain-utils/analyzeChain.ts index 24ba787dec77..af68f6e6c515 100644 --- a/packages/eslint-plugin/src/rules/prefer-optional-chain-utils/analyzeChain.ts +++ b/packages/eslint-plugin/src/rules/prefer-optional-chain-utils/analyzeChain.ts @@ -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 && @@ -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 && @@ -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 && @@ -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) === diff --git a/packages/eslint-plugin/tests/rules/prefer-optional-chain/prefer-optional-chain.test.ts b/packages/eslint-plugin/tests/rules/prefer-optional-chain/prefer-optional-chain.test.ts index eddfd57bb994..fbab86a20910 100644 --- a/packages/eslint-plugin/tests/rules/prefer-optional-chain/prefer-optional-chain.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-optional-chain/prefer-optional-chain.test.ts @@ -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: '
&& (
).wtf;', parserOptions: { ecmaFeatures: { jsx: true } },