Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [prefer-optional-chain] handling first member expression #2156

Merged
merged 5 commits into from Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/eslint-plugin/src/rules/prefer-optional-chain.ts
Expand Up @@ -55,6 +55,7 @@ export default util.createRule({
return {
[[
'LogicalExpression[operator="&&"] > Identifier',
'LogicalExpression[operator="&&"] > MemberExpression',
'LogicalExpression[operator="&&"] > BinaryExpression[operator="!=="]',
'LogicalExpression[operator="&&"] > BinaryExpression[operator="!="]',
].join(',')](
yeonjuan marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -66,7 +67,7 @@ export default util.createRule({
const initialExpression = initialIdentifierOrNotEqualsExpr.parent as TSESTree.LogicalExpression;

if (initialExpression.left !== initialIdentifierOrNotEqualsExpr) {
// the identifier is not the deepest left node
// the node(identifier or member expression) is not the deepest left node
return;
}
if (!isValidChainTarget(initialIdentifierOrNotEqualsExpr, true)) {
Expand Down
70 changes: 66 additions & 4 deletions packages/eslint-plugin/tests/rules/prefer-optional-chain.test.ts
Expand Up @@ -16,24 +16,44 @@ const baseCases = [
code: 'foo && foo.bar',
output: 'foo?.bar',
},
{
code: 'foo.bar && foo.bar.baz',
output: 'foo.bar?.baz',
},
{
code: 'foo && foo()',
output: 'foo?.()',
},
{
code: 'foo.bar && foo.bar()',
output: 'foo.bar?.()',
},
{
code: 'foo && foo.bar && foo.bar.baz && foo.bar.baz.buzz',
output: 'foo?.bar?.baz?.buzz',
},
{
// case with a jump (i.e. a non-nullish prop)
code: 'foo.bar && foo.bar.baz && foo.bar.baz.buzz',
output: 'foo.bar?.baz?.buzz',
},
// case with a jump (i.e. a non-nullish prop)
{
code: 'foo && foo.bar && foo.bar.baz.buzz',
output: 'foo?.bar?.baz.buzz',
},
{
// case where for some reason there is a doubled up expression
code: 'foo.bar && foo.bar.baz.buzz',
output: 'foo.bar?.baz.buzz',
},
// case where for some reason there is a doubled up expression
{
code: 'foo && foo.bar && foo.bar.baz && foo.bar.baz && foo.bar.baz.buzz',
output: 'foo?.bar?.baz?.buzz',
},
{
code: 'foo.bar && foo.bar.baz && foo.bar.baz && foo.bar.baz.buzz',
output: 'foo.bar?.baz?.buzz',
},
// chained members with element access
{
code: 'foo && foo[bar] && foo[bar].baz && foo[bar].baz.buzz',
Expand All @@ -55,10 +75,18 @@ const baseCases = [
output: 'foo?.bar?.baz?.buzz?.()',
},
{
// case with a jump (i.e. a non-nullish prop)
code: 'foo.bar && foo.bar.baz && foo.bar.baz.buzz && foo.bar.baz.buzz()',
output: 'foo.bar?.baz?.buzz?.()',
},
// case with a jump (i.e. a non-nullish prop)
{
code: 'foo && foo.bar && foo.bar.baz.buzz()',
output: 'foo?.bar?.baz.buzz()',
},
{
code: 'foo.bar && foo.bar.baz.buzz()',
output: 'foo.bar?.baz.buzz()',
},
{
// case with a jump (i.e. a non-nullish prop)
code: 'foo && foo.bar && foo.bar.baz.buzz && foo.bar.baz.buzz()',
Expand Down Expand Up @@ -94,6 +122,10 @@ const baseCases = [
code: 'foo && foo?.() && foo?.().bar',
output: 'foo?.()?.bar',
},
{
code: 'foo.bar && foo.bar?.() && foo.bar?.().baz',
output: 'foo.bar?.()?.baz',
},
].map(
c =>
({
Expand Down Expand Up @@ -220,8 +252,8 @@ ruleTester.run('prefer-optional-chain', rule, {
},
],
},
// case with inconsistent checks
{
// case with inconsistent checks
code:
'foo && foo.bar != null && foo.bar.baz !== undefined && foo.bar.baz.buzz;',
output: null,
Expand All @@ -237,6 +269,21 @@ ruleTester.run('prefer-optional-chain', rule, {
},
],
},
{
code: noFormat`foo.bar && foo.bar.baz != null && foo.bar.baz.qux !== undefined && foo.bar.baz.qux.buzz;`,
output: null,
errors: [
{
messageId: 'preferOptionalChain',
suggestions: [
{
messageId: 'optionalChainSuggest',
output: 'foo.bar?.baz?.qux?.buzz;',
},
],
},
],
},
// ensure essential whitespace isn't removed
{
code: 'foo && foo.bar(baz => <This Requires Spaces />);',
Expand Down Expand Up @@ -404,6 +451,21 @@ foo?.bar(/* comment */a,
},
],
},
{
code: 'foo.bar && foo.bar?.();',
output: null,
errors: [
{
messageId: 'preferOptionalChain',
suggestions: [
{
messageId: 'optionalChainSuggest',
output: 'foo.bar?.();',
},
],
},
],
},
// using suggestion instead of autofix
{
code:
Expand Down