Skip to content

Commit

Permalink
more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
bradzacher committed Feb 7, 2023
1 parent e1c5eee commit 3c387a9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/eslint-plugin/src/rules/prefer-optional-chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ function compareNodesUncached(
// we don't want to check the arguments in this case
const aSubsetOfB = compareNodes(nodeA, nodeBCall.callee);
if (aSubsetOfB !== NodeComparisonResult.Invalid) {
return aSubsetOfB;
return NodeComparisonResult.Subset;
}

const calleeCompare = compareNodes(nodeA.callee, nodeBCall.callee);
Expand Down Expand Up @@ -625,6 +625,7 @@ function compareNodesUncached(
// https://github.com/typescript-eslint/typescript-eslint/blob/20d7caee35ab84ae6381fdf04338c9e2b9e2bc48/packages/ast-spec/src/unions/Expression.ts#L37-L43
case AST_NODE_TYPES.ArrayPattern:
case AST_NODE_TYPES.ObjectPattern:
/* istanbul ignore next */
return NodeComparisonResult.Invalid;

// update expression returns a number and also changes the value each time - so it makes no sense to compare the chain
Expand Down Expand Up @@ -787,6 +788,7 @@ function analyzeChain(
return analyzeOrChainOperand;

case '??':
/* istanbul ignore next -- previous checks make this unreachable, but keep it for exhaustiveness check */
return null;
}
})();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,21 @@ describe('hand-crafted cases', () => {
},
],
},
{
code: 'foo && foo[`some ${long} string`] && foo[`some ${long} string`].baz;',
output: null,
errors: [
{
messageId: 'preferOptionalChain',
suggestions: [
{
messageId: 'optionalChainSuggest',
output: 'foo?.[`some ${long} string`]?.baz;',
},
],
},
],
},
// complex computed properties should be handled correctly
{
code: 'foo && foo[bar as string] && foo[bar as string].baz;',
Expand Down Expand Up @@ -987,6 +1002,16 @@ describe('hand-crafted cases', () => {
},
],
},
{
code: 'foo() && foo()(bar)',
output: 'foo()?.(bar)',
errors: [
{
messageId: 'preferOptionalChain',
suggestions: null,
},
],
},
// type parameters are considered
{
code: 'foo && foo<string>() && foo<string>().bar',
Expand Down

0 comments on commit 3c387a9

Please sign in to comment.