@@ -113,11 +113,13 @@ export default util.createRule({
113
113
} ,
114
114
[ [
115
115
'LogicalExpression[operator="||"] > UnaryExpression[operator="!"] > Identifier' ,
116
+ 'LogicalExpression[operator="||"] > UnaryExpression[operator="!"] > PrivateIdentifier' ,
116
117
'LogicalExpression[operator="||"] > UnaryExpression[operator="!"] > MemberExpression' ,
117
118
'LogicalExpression[operator="||"] > UnaryExpression[operator="!"] > ChainExpression > MemberExpression' ,
118
119
] . join ( ',' ) ] (
119
120
initialIdentifierOrNotEqualsExpr :
120
121
| TSESTree . Identifier
122
+ | TSESTree . PrivateIdentifier
121
123
| TSESTree . MemberExpression ,
122
124
) : void {
123
125
// selector guarantees this cast
@@ -425,7 +427,12 @@ export default util.createRule({
425
427
426
428
/* istanbul ignore next */
427
429
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
+ ) {
429
436
throw new Error (
430
437
`Unexpected member property type: ${ node . object . type } ` ,
431
438
) ;
@@ -442,17 +449,20 @@ export default util.createRule({
442
449
const ALLOWED_MEMBER_OBJECT_TYPES : ReadonlySet < AST_NODE_TYPES > = new Set ( [
443
450
AST_NODE_TYPES . CallExpression ,
444
451
AST_NODE_TYPES . Identifier ,
452
+ AST_NODE_TYPES . PrivateIdentifier ,
445
453
AST_NODE_TYPES . MemberExpression ,
446
454
AST_NODE_TYPES . ThisExpression ,
447
455
] ) ;
448
456
const ALLOWED_COMPUTED_PROP_TYPES : ReadonlySet < AST_NODE_TYPES > = new Set ( [
449
457
AST_NODE_TYPES . Identifier ,
458
+ AST_NODE_TYPES . PrivateIdentifier ,
450
459
AST_NODE_TYPES . Literal ,
451
460
AST_NODE_TYPES . MemberExpression ,
452
461
AST_NODE_TYPES . TemplateLiteral ,
453
462
] ) ;
454
463
const ALLOWED_NON_COMPUTED_PROP_TYPES : ReadonlySet < AST_NODE_TYPES > = new Set ( [
455
464
AST_NODE_TYPES . Identifier ,
465
+ AST_NODE_TYPES . PrivateIdentifier ,
456
466
] ) ;
457
467
458
468
interface ReportIfMoreThanOneOptions {
@@ -487,18 +497,19 @@ function reportIfMoreThanOne({
487
497
previous . right . operator
488
498
} ${ sourceCode . getText ( previous . right . right ) } `;
489
499
}
500
+ const newText = `${
501
+ shouldHandleChainedAnds ? '' : '!'
502
+ } ${ optionallyChainedCode } `;
490
503
491
504
context . report ( {
492
505
node : previous ,
493
506
messageId : 'preferOptionalChain' ,
494
507
suggest : [
495
508
{
496
509
messageId : 'optionalChainSuggest' ,
510
+ // For some reason the fixer either doesn't accept it or accepts and reverts? :shrug:
497
511
fix : ( fixer ) : TSESLint . RuleFix [ ] => [
498
- fixer . replaceText (
499
- previous ,
500
- `${ shouldHandleChainedAnds ? '' : '!' } ${ optionallyChainedCode } ` ,
501
- ) ,
512
+ fixer . replaceText ( previous , newText ) ,
502
513
] ,
503
514
} ,
504
515
] ,
0 commit comments