Skip to content

Commit

Permalink
fix(eslint-plugin): [prefer-optional-chain] suggests optional chainin…
Browse files Browse the repository at this point in the history
…g during strict null equality check (#8717)

* fix [prefer-optional-chain] suggests optional chaining during strict null equality

* fix [prefer-optional-chain] suggests optional chaining during strict null equality

* Fix lint

* Add prefer-optional-chain test

* Fix lint

* rebase and fix prefer optional chain on strict null equality

* fix prefer optional chain on strict null equality

* fix lint

* tests: code review suggestion

* Update packages/eslint-plugin/src/rules/prefer-optional-chain-utils/analyzeChain.ts

---------

Co-authored-by: Marta Cardoso <up201304504@up.pt>
Co-authored-by: Marta Cardoso <145560260+up201304504@users.noreply.github.com>
Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
  • Loading branch information
4 people committed Apr 24, 2024
1 parent b0f7aa4 commit 4bed24d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
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

0 comments on commit 4bed24d

Please sign in to comment.