Skip to content

Commit b446a66

Browse files
committed
feat(parser): report duplicate private / protected / public modifier (#11996)
closes #11712
1 parent 2e79d01 commit b446a66

File tree

3 files changed

+126
-10
lines changed

3 files changed

+126
-10
lines changed

crates/oxc_parser/src/modifiers.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,14 @@ impl<'a> ParserImpl<'a> {
460460
}
461461

462462
fn check_for_duplicate_modifiers(&mut self, seen_flags: ModifierFlags, modifier: &Modifier) {
463-
if seen_flags.contains(modifier.kind.into()) {
463+
if seen_flags.contains(modifier.kind.into())
464+
|| (matches!(
465+
modifier.kind,
466+
ModifierKind::Public | ModifierKind::Protected | ModifierKind::Private
467+
) && seen_flags.intersects(
468+
ModifierFlags::PUBLIC | ModifierFlags::PROTECTED | ModifierFlags::PRIVATE,
469+
))
470+
{
464471
self.error(diagnostics::modifier_already_seen(modifier));
465472
}
466473
}

tasks/coverage/snapshots/parser_babel.snap

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12121,6 +12121,51 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
1212112121
╰────
1212212122
help: Remove the duplicate modifier.
1212312123

12124+
× TS(1030): 'public' modifier already seen.
12125+
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/duplicates-accessibility/input.ts:3:11]
12126+
2 │ public public a;
12127+
3 │ private public b;
12128+
· ──────
12129+
4 │ protected private c;
12130+
╰────
12131+
help: Remove the duplicate modifier.
12132+
12133+
× TS(1030): 'private' modifier already seen.
12134+
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/duplicates-accessibility/input.ts:4:13]
12135+
3 │ private public b;
12136+
4 │ protected private c;
12137+
· ───────
12138+
5 │ public protected d;
12139+
╰────
12140+
help: Remove the duplicate modifier.
12141+
12142+
× TS(1030): 'protected' modifier already seen.
12143+
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/duplicates-accessibility/input.ts:5:10]
12144+
4 │ protected private c;
12145+
5 │ public protected d;
12146+
· ─────────
12147+
6 │ public protected private e;
12148+
╰────
12149+
help: Remove the duplicate modifier.
12150+
12151+
× TS(1030): 'protected' modifier already seen.
12152+
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/duplicates-accessibility/input.ts:6:10]
12153+
5 │ public protected d;
12154+
6 │ public protected private e;
12155+
· ─────────
12156+
7 │ }
12157+
╰────
12158+
help: Remove the duplicate modifier.
12159+
12160+
× TS(1030): 'private' modifier already seen.
12161+
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/duplicates-accessibility/input.ts:6:20]
12162+
5 │ public protected d;
12163+
6 │ public protected private e;
12164+
· ───────
12165+
7 │ }
12166+
╰────
12167+
help: Remove the duplicate modifier.
12168+
1212412169
× TS(1098): Type parameter list cannot be empty.
1212512170
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/empty-type-parameters/input.ts:1:8]
1212612171
1 │ class C<> {}

tasks/coverage/snapshots/parser_typescript.snap

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ commit: 81c95189
33
parser_typescript Summary:
44
AST Parsed : 6569/6576 (99.89%)
55
Positive Passed: 6558/6576 (99.73%)
6-
Negative Passed: 1418/5722 (24.78%)
6+
Negative Passed: 1422/5722 (24.85%)
77
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ExportAssignment7.ts
88

99
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ExportAssignment8.ts
@@ -800,8 +800,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/constraintWi
800800

801801
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/constraints0.ts
802802

803-
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/constructorArgsErrors4.ts
804-
805803
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/constructorAsType.ts
806804

807805
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/constructorInvocationWithTooFewTypeArgs.ts
@@ -7134,8 +7132,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ec
71347132

71357133
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/ConstructorDeclarations/parserConstructorDeclaration4.ts
71367134

7137-
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/ConstructorDeclarations/parserConstructorDeclaration7.ts
7138-
71397135
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/EnumDeclarations/parserEnumDeclaration2.ts
71407136

71417137
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/EnumDeclarations/parserEnumDeclaration3.d.ts
@@ -7284,12 +7280,8 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ec
72847280

72857281
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/ParameterLists/parserParameterList2.ts
72867282

7287-
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/Protected/Protected4.ts
7288-
72897283
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/Protected/Protected6.ts
72907284

7291-
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/Protected/Protected7.ts
7292-
72937285
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/RealWorld/parserindenter.ts
72947286

72957287
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/RegressionTests/parser509534.ts
@@ -11159,6 +11151,15 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/private
1115911151
╰────
1116011152
help: Remove the duplicate modifier.
1116111153

11154+
× TS(1030): 'public' modifier already seen.
11155+
╭─[typescript/tests/cases/compiler/constructorArgsErrors4.ts:2:26]
11156+
1 │ class foo {
11157+
2 │ constructor (private public a: number) {
11158+
· ──────
11159+
3 │ }
11160+
╰────
11161+
help: Remove the duplicate modifier.
11162+
1116211163
× Identifier expected. 'export' is a reserved word that cannot be used here.
1116311164
╭─[typescript/tests/cases/compiler/constructorArgsErrors5.ts:2:18]
1116411165
1 │ class foo {
@@ -20483,6 +20484,42 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/private
2048320484
3 │ }
2048420485
╰────
2048520486

20487+
× TS(1030): 'public' modifier already seen.
20488+
╭─[typescript/tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts:39:13]
20489+
38 │ class E {
20490+
39 │ private public protected property;
20491+
· ──────
20492+
40 │ public protected method() { }
20493+
╰────
20494+
help: Remove the duplicate modifier.
20495+
20496+
× TS(1030): 'protected' modifier already seen.
20497+
╭─[typescript/tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts:39:20]
20498+
38 │ class E {
20499+
39 │ private public protected property;
20500+
· ─────────
20501+
40 │ public protected method() { }
20502+
╰────
20503+
help: Remove the duplicate modifier.
20504+
20505+
× TS(1030): 'protected' modifier already seen.
20506+
╭─[typescript/tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts:40:12]
20507+
39 │ private public protected property;
20508+
40 │ public protected method() { }
20509+
· ─────────
20510+
41 │ private protected get getter() { return 0; }
20511+
╰────
20512+
help: Remove the duplicate modifier.
20513+
20514+
× TS(1030): 'protected' modifier already seen.
20515+
╭─[typescript/tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts:41:13]
20516+
40 │ public protected method() { }
20517+
41 │ private protected get getter() { return 0; }
20518+
· ─────────
20519+
42 │ public public set setter(a: number) { }
20520+
╰────
20521+
help: Remove the duplicate modifier.
20522+
2048620523
× TS(1030): 'public' modifier already seen.
2048720524
╭─[typescript/tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts:42:12]
2048820525
41 │ private protected get getter() { return 0; }
@@ -26554,6 +26591,15 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/private
2655426591
╰────
2655526592
help: Remove the duplicate modifier.
2655626593

26594+
× TS(1030): 'private' modifier already seen.
26595+
╭─[typescript/tests/cases/conformance/parser/ecmascript5/ConstructorDeclarations/parserConstructorDeclaration7.ts:2:10]
26596+
1 │ class C {
26597+
2 │ public private constructor() { }
26598+
· ───────
26599+
3 │ }
26600+
╰────
26601+
help: Remove the duplicate modifier.
26602+
2655726603
× Expected `(` but found `;`
2655826604
╭─[typescript/tests/cases/conformance/parser/ecmascript5/ConstructorDeclarations/parserConstructorDeclaration8.ts:3:21]
2655926605
2 │ // Not a constructor
@@ -27832,6 +27878,24 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/private
2783227878
2 │ }
2783327879
╰────
2783427880

27881+
× TS(1030): 'public' modifier already seen.
27882+
╭─[typescript/tests/cases/conformance/parser/ecmascript5/Protected/Protected4.ts:2:13]
27883+
1 │ class C {
27884+
2 │ protected public m() { }
27885+
· ──────
27886+
3 │ }
27887+
╰────
27888+
help: Remove the duplicate modifier.
27889+
27890+
× TS(1030): 'private' modifier already seen.
27891+
╭─[typescript/tests/cases/conformance/parser/ecmascript5/Protected/Protected7.ts:2:13]
27892+
1 │ class C {
27893+
2 │ protected private m() { }
27894+
· ───────
27895+
3 │ }
27896+
╰────
27897+
help: Remove the duplicate modifier.
27898+
2783527899
× Expected a semicolon or an implicit semicolon after a statement, but found none
2783627900
╭─[typescript/tests/cases/conformance/parser/ecmascript5/RealWorld/parserharness.ts:1430:16]
2783727901
1429 │ // Regex for parsing options in the format "@Alpha: Value of any sort"

0 commit comments

Comments
 (0)