Skip to content

Commit 7c31be3

Browse files
committed
Fix binding of this-assignments w/computed properties
Top-level this-assignments do not support computed properties. But the binder forgets to check for computed properties and tries to bind them normally. This hits a helpful assert. This change stop binding this-properties with computed properties at the top-level. There's nothing sensible we could do with them; we're unable to late-bind entries to the global scope or to modules.
1 parent fc0f67d commit 7c31be3

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

src/compiler/binder.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2694,7 +2694,10 @@ namespace ts {
26942694
break;
26952695
case SyntaxKind.SourceFile:
26962696
// this.property = assignment in a source file -- declare symbol in exports for a module, in locals for a script
2697-
if ((thisContainer as SourceFile).commonJsModuleIndicator) {
2697+
if (hasDynamicName(node)) {
2698+
break;
2699+
}
2700+
else if ((thisContainer as SourceFile).commonJsModuleIndicator) {
26982701
declareSymbol(thisContainer.symbol.exports!, thisContainer.symbol, node, SymbolFlags.Property | SymbolFlags.ExportValue, SymbolFlags.None);
26992702
}
27002703
else {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/conformance/salsa/thisPropertyAssignmentComputed.js(1,1): error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof globalThis'.
2+
No index signature with a parameter of type 'string' was found on type 'typeof globalThis'.
3+
4+
5+
==== tests/cases/conformance/salsa/thisPropertyAssignmentComputed.js (1 errors) ====
6+
this["a" + "b"] = 0
7+
~~~~~~~~~~~~~~~
8+
!!! error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof globalThis'.
9+
!!! error TS7053: No index signature with a parameter of type 'string' was found on type 'typeof globalThis'.
10+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/conformance/salsa/thisPropertyAssignmentComputed.js ===
2+
this["a" + "b"] = 0
3+
>this : Symbol(globalThis)
4+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/conformance/salsa/thisPropertyAssignmentComputed.js ===
2+
this["a" + "b"] = 0
3+
>this["a" + "b"] = 0 : 0
4+
>this["a" + "b"] : any
5+
>this : typeof globalThis
6+
>"a" + "b" : string
7+
>"a" : "a"
8+
>"b" : "b"
9+
>0 : 0
10+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @allowjs: true
2+
// @checkjs: true
3+
// @noemit: true
4+
// @strict: true
5+
// @filename: thisPropertyAssignmentComputed.js
6+
this["a" + "b"] = 0

0 commit comments

Comments
 (0)