8000 try · omril1/typescript-eslint@5acc1af · GitHub
[go: up one dir, main page]

Skip to content

Commit 5acc1af

Browse files
author
Omri Luzon
committed
try
1 parent 71cb502 commit 5acc1af

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

packages/eslint-plugin/src/rules/prefer-optional-chain.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,13 @@ export default util.createRule({
113113
},
114114
[[
115115
'LogicalExpression[operator="||"] > UnaryExpression[operator="!"] > Identifier',
116+
'LogicalExpression[operator="||"] > UnaryExpression[operator="!"] > PrivateIdentifier',
116117
'LogicalExpression[operator="||"] > UnaryExpression[operator="!"] > MemberExpression',
117118
'LogicalExpression[operator="||"] > UnaryExpression[operator="!"] > ChainExpression > MemberExpression',
118119
].join(',')](
119120
initialIdentifierOrNotEqualsExpr:
120121
| TSESTree.Identifier
122+
| TSESTree.PrivateIdentifier
121123
| TSESTree.MemberExpression,
122124
): void {
123125
// selector guarantees this cast
@@ -425,7 +427,12 @@ export default util.createRule({
425427

426428
/* istanbul ignore next */
427429
default:
428-
if (![AST_NODE_TYPES.Identifier, AST_NODE_TYPES.ThisExpression].includes(node.object.type)) {
430+
if (
431+
![
432+
AST_NODE_TYPES.Identifier,
433+
AST_NODE_TYPES.ThisExpression,
434+
].includes(node.object.type)
435+
) {
429436
throw new Error(
430437
`Unexpected member property type: ${node.object.type}`,
431438
);
@@ -442,17 +449,20 @@ export default util.createRule({
442449
const ALLOWED_MEMBER_OBJECT_TYPES: ReadonlySet<AST_NODE_TYPES> = new Set([
443450
AST_NODE_TYPES.CallExpression,
444451
AST_NODE_TYPES.Identifier,
452+
AST_NODE_TYPES.PrivateIdentifier,
445453
AST_NODE_TYPES.MemberExpression,
446454
AST_NODE_TYPES.ThisExpression,
447455
]);
448456
const ALLOWED_COMPUTED_PROP_TYPES: ReadonlySet<AST_NODE_TYPES> = new Set([
449457
AST_NODE_TYPES.Identifier,
458+
AST_NODE_TYPES.PrivateIdentifier,
450459
AST_NODE_TYPES.Literal,
451460
AST_NODE_TYPES.MemberExpression,
452461
AST_NODE_TYPES.TemplateLiteral,
453462
]);
454463
const ALLOWED_NON_COMPUTED_PROP_TYPES: ReadonlySet<AST_NODE_TYPES> = new Set([
455464
AST_NODE_TYPES.Identifier,
465+
AST_NODE_TYPES.PrivateIdentifier,
456466
]);
457467

458468
interface ReportIfMoreThanOneOptions {
@@ -487,18 +497,19 @@ function reportIfMoreThanOne({
487497
previous.right.operator
488498
} ${sourceCode.getText(previous.right.right)}`;
489499
}
500+
const newText = `${
501+
shouldHandleChainedAnds ? '' : '!'
502+
}${optionallyChainedCode}`;
490503

491504
context.report({
492505
node: previous,
493506
messageId: 'preferOptionalChain',
494507
suggest: [
495508
{
496509
messageId: 'optionalChainSuggest',
510+
// For some reason the fixer either doesn't accept it or accepts and reverts? :shrug:
497511
fix: (fixer): TSESLint.RuleFix[] => [
498-
fixer.replaceText(
499-
previous,
500-
`${shouldHandleChainedAnds ? '' : '!'}${optionallyChainedCode}`,
501-
),
512+
fixer.replaceText(previous, newText),
502513
],
503514
},
504515
],

packages/eslint-plugin/tests/rules/prefer-optional-chain.test.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,6 @@ ruleTester.run('prefer-optional-chain', rule, {
197197
// private properties
198198
'this.#a && this.#b;',
199199
'!this.#a || !this.#b;',
200-
'a.#foo && a.#foo.bar;',
201-
'!a.#foo || !a.#foo.bar;',
202200
'a.#foo?.bar;',
203201
'!a.#foo?.bar;',
204202
// currently do not handle complex computed properties
@@ -1328,5 +1326,35 @@ foo?.bar(/* comment */a,
13281326
},
13291327
],
13301328
},
1329+
{
1330+
code: 'a.#foo && a.#foo.bar;',
1331+
output: 'a.#foo?.bar;',
1332+
errors: [
1333+
{
1334+
messageId: 'preferOptionalChain',
1335+
suggestions: [
1336+
{
1337+
messageId: 'optionalChainSuggest',
1338+
output: 'a.#foo?.bar;',
1339+
},
1340+
],
1341+
},
1342+
],
1343+
},
1344+
{
1345+
code: '!a.#foo || !a.#foo.bar;',
1346+
output: '!a.#foo?.bar;',
1347+
errors: [
1348+
{
1349+
messageId: 'preferOptionalChain',
1350+
suggestions: [
1351+
{
1352+
messageId: 'optionalChainSuggest',
1353+
output: '!a.#foo?.bar;',
1354+
},
1355+
],
1356+
},
1357+
],
1358+
},
13311359
],
13321360
});

0 commit comments

Comments
 (0)
0