Thanks to visit codestin.com
Credit goes to github.com

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
Copy link
Member

@kirkwaiblinger kirkwaiblinger Oct 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there's a minor bug somewhere with:

declare const foo: { bar: number | null } | undefined;
if (foo === undefined || foo.bar === null) { // suggests to foo?.bar === null, which is not equivalent.
} else {
  foo.bar.toExponential(); // after accepting fix, this crashes if `foo` is `undefined`.
}

I guess this should either not report or it could maybe try to use foo?.bar == null? But this seems kind of bad since it is impossible to work with for an === nullishValue-prefering user

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (
includesType(parserServices, operand.comparedName, ts.TypeFlags.Null)
) {
// we know the next operand is not a `null` check and that this
// operand includes `null` - which means that making this an
// optional chain would change the runtime behavior of the expression
return null;
}

I believe this bug didn’t originate from this PR — it seems to have existed beforehand.
Since I wasn’t the original author, it might take some time to fix it properly.

If possible, we could apply a quick hotfix for the current issue first and then open a new issue to track it, but it’s just a suggestion — feel free to decide as you think best.

In any case, I’ll start digging into it now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine if we defer it!

Just out of curiosity, do you mean to say it didn't originate in this PR, ie #11702, or in #11533? (either way we can defer it just good to know)

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
} from '@typescript-eslint/utils/ts-eslint';

import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import { isFalsyType, unionConstituents } from 'ts-api-utils';
import { unionConstituents } from 'ts-api-utils';
import * as ts from 'typescript';

import type { LastChainOperand, ValidOperand } from './gatherLogicalOperands';
Expand Down Expand Up @@ -48,40 +48,6 @@ function includesType(
return false;
}

function isAlwaysTruthyOperand(
comparedName: TSESTree.Node,
nullishComparisonType: NullishComparisonType,
parserServices: ParserServicesWithTypeInformation,
): boolean {
const ANY_UNKNOWN_FLAGS = ts.TypeFlags.Any | ts.TypeFlags.Unknown;
const comparedNameType = parserServices.getTypeAtLocation(comparedName);

if (isTypeFlagSet(comparedNameType, ANY_UNKNOWN_FLAGS)) {
return false;
}
switch (nullishComparisonType) {
case NullishComparisonType.Boolean:
case NullishComparisonType.NotBoolean: {
const types = unionConstituents(comparedNameType);
return types.every(type => !isFalsyType(type));
}
case NullishComparisonType.NotStrictEqualUndefined:
case NullishComparisonType.NotStrictEqualNull:
case NullishComparisonType.StrictEqualNull:
case NullishComparisonType.StrictEqualUndefined:
return !isTypeFlagSet(
comparedNameType,
ts.TypeFlags.Null | ts.TypeFlags.Undefined,
);
case NullishComparisonType.NotEqualNullOrUndefined:
case NullishComparisonType.EqualNullOrUndefined:
return !isTypeFlagSet(
comparedNameType,
ts.TypeFlags.Null | ts.TypeFlags.Undefined,
);
}
}

function isValidAndLastChainOperand(
ComparisonValueType: TSESTree.Node,
comparisonType: ComparisonType,
Expand Down Expand Up @@ -710,20 +676,6 @@ export function analyzeChain(
}
break;
}
case NullishComparisonType.StrictEqualNull:
case NullishComparisonType.NotStrictEqualNull: {
if (
comparisonResult === NodeComparisonResult.Subset &&
isAlwaysTruthyOperand(
lastOperand.comparedName,
lastOperand.comparisonType,
parserServices,
)
) {
lastChain = operand;
}
break;
}
}
}
maybeReportThenReset();
Expand Down Expand Up @@ -768,16 +720,11 @@ export function analyzeChain(
: isValidOrLastChainOperand;
if (
comparisonResult === NodeComparisonResult.Subset &&
(isAlwaysTruthyOperand(
lastOperand.comparedName,
lastOperand.comparisonType,
isValidLastChainOperand(
lastChainOperand.comparisonValue,
lastChainOperand.comparisonType,
parserServices,
) ||
isValidLastChainOperand(
lastChainOperand.comparisonValue,
lastChainOperand.comparisonType,
parserServices,
))
)
) {
lastChain = lastChainOperand;
}
Expand Down
Loading