From 6d89578962a739c1f62a296c04f612c739cabaef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Wed, 4 Jun 2025 14:31:58 +0200 Subject: [PATCH] Port "Allow assignments to readonly class properties within IIFEs contained in constructors" --- internal/checker/checker.go | 2 +- .../compiler/readonlyMembers.errors.txt | 8 +---- .../compiler/readonlyMembers.errors.txt.diff | 32 ------------------- .../submodule/compiler/readonlyMembers.types | 8 ++--- .../compiler/readonlyMembers.types.diff | 23 ------------- 5 files changed, 6 insertions(+), 67 deletions(-) delete mode 100644 testdata/baselines/reference/submodule/compiler/readonlyMembers.errors.txt.diff delete mode 100644 testdata/baselines/reference/submodule/compiler/readonlyMembers.types.diff diff --git a/internal/checker/checker.go b/internal/checker/checker.go index 74fbd0f01a..23ccf228cb 100644 --- a/internal/checker/checker.go +++ b/internal/checker/checker.go @@ -25867,7 +25867,7 @@ func (c *Checker) isAssignmentToReadonlyEntity(expr *ast.Node, symbol *ast.Symbo // Allow assignments to readonly properties within constructors of the same class declaration. if symbol.Flags&ast.SymbolFlagsProperty != 0 && ast.IsAccessExpression(expr) && expr.Expression().Kind == ast.KindThisKeyword { // Look for if this is the constructor for the class that `symbol` is a property of. - ctor := getContainingFunction(expr) + ctor := c.getControlFlowContainer(expr) if ctor == nil || !ast.IsConstructorDeclaration(ctor) { return true } diff --git a/testdata/baselines/reference/submodule/compiler/readonlyMembers.errors.txt b/testdata/baselines/reference/submodule/compiler/readonlyMembers.errors.txt index 8f96d062a7..c3ac0ac353 100644 --- a/testdata/baselines/reference/submodule/compiler/readonlyMembers.errors.txt +++ b/testdata/baselines/reference/submodule/compiler/readonlyMembers.errors.txt @@ -4,8 +4,6 @@ readonlyMembers.ts(16,14): error TS2540: Cannot assign to 'c' because it is a re readonlyMembers.ts(18,18): error TS2540: Cannot assign to 'a' because it is a read-only property. readonlyMembers.ts(19,18): error TS2540: Cannot assign to 'b' because it is a read-only property. readonlyMembers.ts(20,18): error TS2540: Cannot assign to 'c' because it is a read-only property. -readonlyMembers.ts(23,18): error TS2540: Cannot assign to 'a' because it is a read-only property. -readonlyMembers.ts(24,18): error TS2540: Cannot assign to 'b' because it is a read-only property. readonlyMembers.ts(25,18): error TS2540: Cannot assign to 'c' because it is a read-only property. readonlyMembers.ts(29,14): error TS2540: Cannot assign to 'a' because it is a read-only property. readonlyMembers.ts(30,14): error TS2540: Cannot assign to 'b' because it is a read-only property. @@ -18,7 +16,7 @@ readonlyMembers.ts(66,1): error TS2542: Index signature in type '{ readonly [x: readonlyMembers.ts(69,1): error TS2542: Index signature in type '{ readonly [x: number]: string; [x: string]: string; }' only permits reading. -==== readonlyMembers.ts (18 errors) ==== +==== readonlyMembers.ts (16 errors) ==== interface X { readonly a: number; readonly b?: number; @@ -54,11 +52,7 @@ readonlyMembers.ts(69,1): error TS2542: Index signature in type '{ readonly [x: }; (() => { this.a = 1; // Ok - ~ -!!! error TS2540: Cannot assign to 'a' because it is a read-only property. this.b = 1; // Ok - ~ -!!! error TS2540: Cannot assign to 'b' because it is a read-only property. this.c = 1; // Error ~ !!! error TS2540: Cannot assign to 'c' because it is a read-only property. diff --git a/testdata/baselines/reference/submodule/compiler/readonlyMembers.errors.txt.diff b/testdata/baselines/reference/submodule/compiler/readonlyMembers.errors.txt.diff deleted file mode 100644 index 53d09567c4..0000000000 --- a/testdata/baselines/reference/submodule/compiler/readonlyMembers.errors.txt.diff +++ /dev/null @@ -1,32 +0,0 @@ ---- old.readonlyMembers.errors.txt -+++ new.readonlyMembers.errors.txt -@@= skipped -3, +3 lines =@@ - readonlyMembers.ts(18,18): error TS2540: Cannot assign to 'a' because it is a read-only property. - readonlyMembers.ts(19,18): error TS2540: Cannot assign to 'b' because it is a read-only property. - readonlyMembers.ts(20,18): error TS2540: Cannot assign to 'c' because it is a read-only property. -+readonlyMembers.ts(23,18): error TS2540: Cannot assign to 'a' because it is a read-only property. -+readonlyMembers.ts(24,18): error TS2540: Cannot assign to 'b' because it is a read-only property. - readonlyMembers.ts(25,18): error TS2540: Cannot assign to 'c' because it is a read-only property. - readonlyMembers.ts(29,14): error TS2540: Cannot assign to 'a' because it is a read-only property. - readonlyMembers.ts(30,14): error TS2540: Cannot assign to 'b' because it is a read-only property. -@@= skipped -12, +14 lines =@@ - readonlyMembers.ts(69,1): error TS2542: Index signature in type '{ readonly [x: number]: string; [x: string]: string; }' only permits reading. - - --==== readonlyMembers.ts (16 errors) ==== -+==== readonlyMembers.ts (18 errors) ==== - interface X { - readonly a: number; - readonly b?: number; -@@= skipped -36, +36 lines =@@ - }; - (() => { - this.a = 1; // Ok -+ ~ -+!!! error TS2540: Cannot assign to 'a' because it is a read-only property. - this.b = 1; // Ok -+ ~ -+!!! error TS2540: Cannot assign to 'b' because it is a read-only property. - this.c = 1; // Error - ~ - !!! error TS2540: Cannot assign to 'c' because it is a read-only property. \ No newline at end of file diff --git a/testdata/baselines/reference/submodule/compiler/readonlyMembers.types b/testdata/baselines/reference/submodule/compiler/readonlyMembers.types index 732e7c4daf..9fa4c3a8ac 100644 --- a/testdata/baselines/reference/submodule/compiler/readonlyMembers.types +++ b/testdata/baselines/reference/submodule/compiler/readonlyMembers.types @@ -97,16 +97,16 @@ class C { this.a = 1; // Ok >this.a = 1 : 1 ->this.a : any +>this.a : number >this : this ->a : any +>a : number >1 : 1 this.b = 1; // Ok >this.b = 1 : 1 ->this.b : any +>this.b : 1 >this : this ->b : any +>b : 1 >1 : 1 this.c = 1; // Error diff --git a/testdata/baselines/reference/submodule/compiler/readonlyMembers.types.diff b/testdata/baselines/reference/submodule/compiler/readonlyMembers.types.diff deleted file mode 100644 index cb71ce76e7..0000000000 --- a/testdata/baselines/reference/submodule/compiler/readonlyMembers.types.diff +++ /dev/null @@ -1,23 +0,0 @@ ---- old.readonlyMembers.types -+++ new.readonlyMembers.types -@@= skipped -96, +96 lines =@@ - - this.a = 1; // Ok - >this.a = 1 : 1 -->this.a : number -+>this.a : any - >this : this -->a : number -+>a : any - >1 : 1 - - this.b = 1; // Ok - >this.b = 1 : 1 -->this.b : 1 -+>this.b : any - >this : this -->b : 1 -+>b : any - >1 : 1 - - this.c = 1; // Error \ No newline at end of file