Skip to content

Commit e73a51d

Browse files
authored
fix(51820): disallow using readonly and declare with auto-accessor (#51824)
1 parent 6f4d340 commit e73a51d

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45580,6 +45580,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4558045580
// If node.kind === SyntaxKind.Parameter, checkParameter reports an error if it's not a parameter property.
4558145581
return grammarErrorOnNode(modifier, Diagnostics.readonly_modifier_can_only_appear_on_a_property_declaration_or_index_signature);
4558245582
}
45583+
else if (flags & ModifierFlags.Accessor) {
45584+
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "readonly", "accessor");
45585+
}
4558345586
flags |= ModifierFlags.Readonly;
4558445587
break;
4558545588

@@ -45637,6 +45640,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4563745640
else if (isPrivateIdentifierClassElementDeclaration(node)) {
4563845641
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_be_used_with_a_private_identifier, "declare");
4563945642
}
45643+
else if (flags & ModifierFlags.Accessor) {
45644+
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "declare", "accessor");
45645+
}
4564045646
flags |= ModifierFlags.Ambient;
4564145647
lastDeclare = modifier;
4564245648
break;

tests/baselines/reference/autoAccessorDisallowedModifiers.errors.txt

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,26 @@ tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowe
1111
tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts(12,5): error TS1275: 'accessor' modifier can only appear on a property declaration.
1212
tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts(13,5): error TS1275: 'accessor' modifier can only appear on a property declaration.
1313
tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts(14,15): error TS1276: An 'accessor' property cannot be declared optional.
14-
tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts(18,14): error TS1029: 'override' modifier must precede 'accessor' modifier.
15-
tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts(22,5): error TS1070: 'accessor' modifier cannot appear on a type member.
16-
tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts(25,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
17-
tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts(26,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
14+
tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts(15,14): error TS1243: 'readonly' modifier cannot be used with 'accessor' modifier.
15+
tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts(16,14): error TS1243: 'declare' modifier cannot be used with 'accessor' modifier.
16+
tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts(20,14): error TS1029: 'override' modifier must precede 'accessor' modifier.
17+
tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts(24,5): error TS1070: 'accessor' modifier cannot appear on a type member.
1818
tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts(27,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
1919
tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts(28,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
2020
tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts(29,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
2121
tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts(30,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
2222
tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts(31,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
2323
tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts(32,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
2424
tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts(33,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
25-
tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts(33,25): error TS2792: Cannot find module 'x'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
2625
tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts(34,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
2726
tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts(35,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
27+
tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts(35,25): error TS2792: Cannot find module 'x'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
2828
tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts(36,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
29+
tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts(37,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
30+
tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts(38,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
2931

3032

31-
==== tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts (28 errors) ====
33+
==== tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts (30 errors) ====
3234
abstract class C1 {
3335
accessor accessor a: any;
3436
~~~~~~~~
@@ -69,6 +71,12 @@ tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowe
6971
accessor l?: any;
7072
~
7173
!!! error TS1276: An 'accessor' property cannot be declared optional.
74+
accessor readonly m: any;
75+
~~~~~~~~
76+
!!! error TS1243: 'readonly' modifier cannot be used with 'accessor' modifier.
77+
accessor declare n: any;
78+
~~~~~~~
79+
!!! error TS1243: 'declare' modifier cannot be used with 'accessor' modifier.
7280
}
7381

7482
class C2 extends C1 {

tests/baselines/reference/autoAccessorDisallowedModifiers.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ abstract class C1 {
1313
accessor set k(v: any) {}
1414
accessor constructor() {}
1515
accessor l?: any;
16+
accessor readonly m: any;
17+
accessor declare n: any;
1618
}
1719

1820
class C2 extends C1 {
@@ -50,6 +52,7 @@ class C1 {
5052
accessor set k(v) { }
5153
constructor() { }
5254
accessor l;
55+
accessor m;
5356
}
5457
class C2 extends C1 {
5558
accessor g;

tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ abstract class C1 {
1515
accessor set k(v: any) {}
1616
accessor constructor() {}
1717
accessor l?: any;
18+
accessor readonly m: any;
19+
accessor declare n: any;
1820
}
1921

2022
class C2 extends C1 {

0 commit comments

Comments
 (0)