Skip to content

Allow readonly with accessor #55290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47436,9 +47436,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (flags & ModifierFlags.Accessor) {
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_already_seen, "accessor");
}
else if (flags & ModifierFlags.Readonly) {
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "accessor", "readonly");
}
else if (flags & ModifierFlags.Ambient) {
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "accessor", "declare");
}
Expand All @@ -47458,7 +47455,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return grammarErrorOnNode(modifier, Diagnostics.readonly_modifier_can_only_appear_on_a_property_declaration_or_index_signature);
}
else if (flags & ModifierFlags.Accessor) {
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "readonly", "accessor");
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_must_precede_1_modifier, "readonly", "accessor");
}
flags |= ModifierFlags.Readonly;
break;
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/autoAccessorAllowedModifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ abstract class C1 {
accessor 108: any;
accessor ["m"]: any;
accessor n!: number;
readonly accessor o: any;
}

class C2 extends C1 {
Expand Down Expand Up @@ -44,6 +45,7 @@ class C1 {
accessor 108;
accessor ["m"];
accessor n;
accessor o;
}
class C2 extends C1 {
accessor e;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
autoAccessorDisallowedModifiers.ts(2,14): error TS1030: 'accessor' modifier already seen.
autoAccessorDisallowedModifiers.ts(3,14): error TS1243: 'accessor' modifier cannot be used with 'readonly' modifier.
autoAccessorDisallowedModifiers.ts(4,13): error TS1243: 'accessor' modifier cannot be used with 'declare' modifier.
autoAccessorDisallowedModifiers.ts(5,14): error TS1029: 'public' modifier must precede 'accessor' modifier.
autoAccessorDisallowedModifiers.ts(6,14): error TS1029: 'private' modifier must precede 'accessor' modifier.
autoAccessorDisallowedModifiers.ts(7,14): error TS1029: 'protected' modifier must precede 'accessor' modifier.
autoAccessorDisallowedModifiers.ts(8,14): error TS1029: 'abstract' modifier must precede 'accessor' modifier.
autoAccessorDisallowedModifiers.ts(9,14): error TS1029: 'static' modifier must precede 'accessor' modifier.
autoAccessorDisallowedModifiers.ts(3,13): error TS1243: 'accessor' modifier cannot be used with 'declare' modifier.
autoAccessorDisallowedModifiers.ts(4,14): error TS1029: 'public' modifier must precede 'accessor' modifier.
autoAccessorDisallowedModifiers.ts(5,14): error TS1029: 'private' modifier must precede 'accessor' modifier.
autoAccessorDisallowedModifiers.ts(6,14): error TS1029: 'protected' modifier must precede 'accessor' modifier.
autoAccessorDisallowedModifiers.ts(7,14): error TS1029: 'abstract' modifier must precede 'accessor' modifier.
autoAccessorDisallowedModifiers.ts(8,14): error TS1029: 'static' modifier must precede 'accessor' modifier.
autoAccessorDisallowedModifiers.ts(9,5): error TS1275: 'accessor' modifier can only appear on a property declaration.
autoAccessorDisallowedModifiers.ts(10,5): error TS1275: 'accessor' modifier can only appear on a property declaration.
autoAccessorDisallowedModifiers.ts(11,5): error TS1275: 'accessor' modifier can only appear on a property declaration.
autoAccessorDisallowedModifiers.ts(12,5): error TS1275: 'accessor' modifier can only appear on a property declaration.
autoAccessorDisallowedModifiers.ts(13,5): error TS1275: 'accessor' modifier can only appear on a property declaration.
autoAccessorDisallowedModifiers.ts(14,15): error TS1276: An 'accessor' property cannot be declared optional.
autoAccessorDisallowedModifiers.ts(15,14): error TS1243: 'readonly' modifier cannot be used with 'accessor' modifier.
autoAccessorDisallowedModifiers.ts(16,14): error TS1243: 'declare' modifier cannot be used with 'accessor' modifier.
autoAccessorDisallowedModifiers.ts(20,14): error TS1029: 'override' modifier must precede 'accessor' modifier.
autoAccessorDisallowedModifiers.ts(24,5): error TS1070: 'accessor' modifier cannot appear on a type member.
autoAccessorDisallowedModifiers.ts(13,15): error TS1276: An 'accessor' property cannot be declared optional.
autoAccessorDisallowedModifiers.ts(14,14): error TS1029: 'readonly' modifier must precede 'accessor' modifier.
autoAccessorDisallowedModifiers.ts(15,14): error TS1243: 'declare' modifier cannot be used with 'accessor' modifier.
autoAccessorDisallowedModifiers.ts(18,7): error TS18052: Non-abstract class 'C2' does not implement all abstract members of 'C1'
autoAccessorDisallowedModifiers.ts(19,14): error TS1029: 'override' modifier must precede 'accessor' modifier.
autoAccessorDisallowedModifiers.ts(19,23): error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'C1'.
autoAccessorDisallowedModifiers.ts(23,5): error TS1070: 'accessor' modifier cannot appear on a type member.
autoAccessorDisallowedModifiers.ts(26,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
autoAccessorDisallowedModifiers.ts(27,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
autoAccessorDisallowedModifiers.ts(28,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
autoAccessorDisallowedModifiers.ts(29,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
Expand All @@ -23,66 +25,67 @@ autoAccessorDisallowedModifiers.ts(31,1): error TS1275: 'accessor' modifier can
autoAccessorDisallowedModifiers.ts(32,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
autoAccessorDisallowedModifiers.ts(33,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
autoAccessorDisallowedModifiers.ts(34,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
autoAccessorDisallowedModifiers.ts(34,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?
autoAccessorDisallowedModifiers.ts(35,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
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?
autoAccessorDisallowedModifiers.ts(36,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
autoAccessorDisallowedModifiers.ts(37,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
autoAccessorDisallowedModifiers.ts(38,1): error TS1275: 'accessor' modifier can only appear on a property declaration.


==== autoAccessorDisallowedModifiers.ts (30 errors) ====
==== autoAccessorDisallowedModifiers.ts (31 errors) ====
abstract class C1 {
accessor accessor a: any;
~~~~~~~~
!!! error TS1030: 'accessor' modifier already seen.
readonly accessor b: any;
~~~~~~~~
!!! error TS1243: 'accessor' modifier cannot be used with 'readonly' modifier.
declare accessor c: any;
declare accessor b: any;
~~~~~~~~
!!! error TS1243: 'accessor' modifier cannot be used with 'declare' modifier.
accessor public d: any;
accessor public c: any;
~~~~~~
!!! error TS1029: 'public' modifier must precede 'accessor' modifier.
accessor private e: any;
accessor private d: any;
~~~~~~~
!!! error TS1029: 'private' modifier must precede 'accessor' modifier.
accessor protected f: any;
accessor protected e: any;
~~~~~~~~~
!!! error TS1029: 'protected' modifier must precede 'accessor' modifier.
accessor abstract g: any;
accessor abstract f: any;
~~~~~~~~
!!! error TS1029: 'abstract' modifier must precede 'accessor' modifier.
accessor static h: any;
accessor static g: any;
~~~~~~
!!! error TS1029: 'static' modifier must precede 'accessor' modifier.
accessor i() {}
accessor h() {}
~~~~~~~~
!!! error TS1275: 'accessor' modifier can only appear on a property declaration.
accessor get j() { return false; }
accessor get i() { return false; }
~~~~~~~~
!!! error TS1275: 'accessor' modifier can only appear on a property declaration.
accessor set k(v: any) {}
accessor set j(v: any) {}
~~~~~~~~
!!! error TS1275: 'accessor' modifier can only appear on a property declaration.
accessor constructor() {}
~~~~~~~~
!!! error TS1275: 'accessor' modifier can only appear on a property declaration.
accessor l?: any;
accessor k?: any;
~
!!! error TS1276: An 'accessor' property cannot be declared optional.
accessor readonly m: any;
accessor readonly l: any;
~~~~~~~~
!!! error TS1243: 'readonly' modifier cannot be used with 'accessor' modifier.
accessor declare n: any;
!!! error TS1029: 'readonly' modifier must precede 'accessor' modifier.
accessor declare m: any;
~~~~~~~
!!! error TS1243: 'declare' modifier cannot be used with 'accessor' modifier.
}

class C2 extends C1 {
~~
!!! error TS18052: Non-abstract class 'C2' does not implement all abstract members of 'C1'
!!! related TS2515 autoAccessorDisallowedModifiers.ts:7:23: Non-abstract class 'C2' does not implement inherited abstract member 'f' from class 'C1'.
accessor override g: any;
~~~~~~~~
!!! error TS1029: 'override' modifier must precede 'accessor' modifier.
~
!!! error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'C1'.
}

interface I1 {
Expand Down
38 changes: 18 additions & 20 deletions tests/baselines/reference/autoAccessorDisallowedModifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
//// [autoAccessorDisallowedModifiers.ts]
abstract class C1 {
accessor accessor a: any;
readonly accessor b: any;
declare accessor c: any;
accessor public d: any;
accessor private e: any;
accessor protected f: any;
accessor abstract g: any;
accessor static h: any;
accessor i() {}
accessor get j() { return false; }
accessor set k(v: any) {}
declare accessor b: any;
accessor public c: any;
accessor private d: any;
accessor protected e: any;
accessor abstract f: any;
accessor static g: any;
accessor h() {}
accessor get i() { return false; }
accessor set j(v: any) {}
accessor constructor() {}
accessor l?: any;
accessor readonly m: any;
accessor declare n: any;
accessor k?: any;
accessor readonly l: any;
accessor declare m: any;
}

class C2 extends C1 {
Expand Down Expand Up @@ -44,17 +43,16 @@ accessor import N2 = N1;
//// [autoAccessorDisallowedModifiers.js]
class C1 {
accessor accessor a;
accessor b;
accessor c;
accessor d;
accessor e;
accessor f;
accessor static h;
accessor i() { }
accessor get j() { return false; }
accessor set k(v) { }
accessor static g;
accessor h() { }
accessor get i() { return false; }
accessor set j(v) { }
constructor() { }
accessor k;
accessor l;
accessor m;
}
class C2 extends C1 {
accessor g;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ abstract class C1 {
accessor 108: any;
accessor ["m"]: any;
accessor n!: number;
readonly accessor o: any;
}

class C2 extends C1 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@

abstract class C1 {
accessor accessor a: any;
readonly accessor b: any;
declare accessor c: any;
accessor public d: any;
accessor private e: any;
accessor protected f: any;
accessor abstract g: any;
accessor static h: any;
accessor i() {}
accessor get j() { return false; }
accessor set k(v: any) {}
declare accessor b: any;
accessor public c: any;
accessor private d: any;
accessor protected e: any;
accessor abstract f: any;
accessor static g: any;
accessor h() {}
accessor get i() { return false; }
accessor set j(v: any) {}
accessor constructor() {}
accessor l?: any;
accessor readonly m: any;
accessor declare n: any;
accessor k?: any;
accessor readonly l: any;
accessor declare m: any;
}

class C2 extends C1 {
Expand Down