@@ -3,12 +3,13 @@ package explicit_member_accessibility
3
3
import (
4
4
"fmt"
5
5
6
+ "strings"
7
+
6
8
"github.com/microsoft/typescript-go/shim/ast"
7
9
"github.com/microsoft/typescript-go/shim/core"
8
10
"github.com/microsoft/typescript-go/shim/scanner"
9
11
"github.com/web-infra-dev/rslint/internal/rule"
10
12
"github.com/web-infra-dev/rslint/internal/utils"
11
- "strings"
12
13
)
13
14
14
15
type AccessibilityLevel string
@@ -377,7 +378,7 @@ var ExplicitMemberAccessibilityRule = rule.Rule{
377
378
ignoredMethodNames [name ] = true
378
379
}
379
380
380
- checkMethodAccessibilityModifier := func (node * ast.Node ) {
381
+ checkMethodAccessibilityModifier := func (node * ast.Node ) {
381
382
if isPrivateIdentifier (node ) {
382
383
return
383
384
}
@@ -403,7 +404,7 @@ var ExplicitMemberAccessibilityRule = rule.Rule{
403
404
404
405
accessibility := getAccessibilityModifier (node )
405
406
406
- if check == AccessibilityNoPublic && accessibility == "public" {
407
+ if check == AccessibilityNoPublic && accessibility == "public" {
407
408
// Find and report on the public keyword specifically, and provide fix
408
409
var modifiers * ast.ModifierList
409
410
switch kind := node .Kind ; kind {
@@ -429,7 +430,7 @@ var ExplicitMemberAccessibilityRule = rule.Rule{
429
430
}
430
431
}
431
432
}
432
- } else if check == AccessibilityExplicit && accessibility == "" {
433
+ } else if check == AccessibilityExplicit && accessibility == "" {
433
434
// Report precisely on the member name (or keyword for constructors/abstract)
434
435
r := getMissingAccessibilityRange (ctx , node )
435
436
ctx .ReportRange (r , rule.RuleMessage {
@@ -477,7 +478,7 @@ var ExplicitMemberAccessibilityRule = rule.Rule{
477
478
}
478
479
}
479
480
480
- checkParameterPropertyAccessibilityModifier := func (node * ast.Node ) {
481
+ checkParameterPropertyAccessibilityModifier := func (node * ast.Node ) {
481
482
if node .Kind != ast .KindParameter {
482
483
return
483
484
}
@@ -503,7 +504,7 @@ var ExplicitMemberAccessibilityRule = rule.Rule{
503
504
}
504
505
}
505
506
506
- // A parameter property must have readonly OR accessibility modifier
507
+ // Consider only parameters that are parameter properties ( have readonly or accessibility)
507
508
if ! hasReadonly && ! hasAccessibility {
508
509
return
509
510
}
@@ -532,42 +533,34 @@ var ExplicitMemberAccessibilityRule = rule.Rule{
532
533
return
533
534
}
534
535
535
- switch paramPropCheck {
536
- case AccessibilityExplicit :
537
- if accessibility == "" {
538
- // Calculate the proper range for the parameter property
539
- var reportRange core.TextRange
540
- if hasReadonly && readonlyNode != nil {
541
- // Report from readonly keyword to end of parameter name
542
- reportRange = core .NewTextRange (readonlyNode .Pos (), name .End ())
543
- } else {
544
- // Report the entire parameter name
545
- reportRange = core .NewTextRange (node .Pos (), name .End ())
546
- }
547
-
548
- ctx .ReportRange (reportRange , rule.RuleMessage {
549
- Id : "missingAccessibility" ,
550
- Description : fmt .Sprintf ("Missing accessibility modifier on %s %s." , nodeType , nodeName ),
551
- })
536
+ // Emit at most one diagnostic per parameter property, matching TS-ESLint tests
537
+ if paramPropCheck == AccessibilityExplicit && accessibility == "" {
538
+ var reportRange core.TextRange
539
+ if hasReadonly && readonlyNode != nil {
540
+ reportRange = core .NewTextRange (readonlyNode .Pos (), name .End ())
541
+ } else {
542
+ reportRange = core .NewTextRange (node .Pos (), name .End ())
552
543
}
553
- case AccessibilityNoPublic :
554
- if accessibility == "public" {
555
- // Find and report on the public keyword specifically
556
- if param .Modifiers () != nil {
557
- for _ , mod := range param .Modifiers ().Nodes {
558
- if mod .Kind == ast .KindPublicKeyword {
559
- message := rule.RuleMessage {
560
- Id : "unwantedPublicAccessibility" ,
561
- Description : fmt .Sprintf ("Public accessibility modifier on %s %s." , nodeType , nodeName ),
562
- }
563
- ctx .ReportNode (mod , message )
564
- return
544
+ ctx .ReportRange (reportRange , rule.RuleMessage {
545
+ Id : "missingAccessibility" ,
546
+ Description : fmt .Sprintf ("Missing accessibility modifier on %s %s." , nodeType , nodeName ),
547
+ })
548
+ return
549
+ }
550
+
551
+ if paramPropCheck == AccessibilityNoPublic && accessibility == "public" {
552
+ if param .Modifiers () != nil {
553
+ for _ , mod := range param .Modifiers ().Nodes {
554
+ if mod .Kind == ast .KindPublicKeyword {
555
+ message := rule.RuleMessage {
556
+ Id : "unwantedPublicAccessibility" ,
557
+ Description : fmt .Sprintf ("Public accessibility modifier on %s %s." , nodeType , nodeName ),
565
558
}
559
+ ctx .ReportNode (mod , message )
560
+ return
566
561
}
567
562
}
568
563
}
569
- case AccessibilityOff :
570
- // Don't check parameter properties when off
571
564
return
572
565
}
573
566
}
0 commit comments