Fix binding of this-assignments w/computed properties (#35639)

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.
This commit is contained in:
Nathan Shively-Sanders
2019-12-11 15:55:08 -08:00
committed by GitHub
parent 1fd1b429f0
commit b98cb0645d
5 changed files with 34 additions and 1 deletions

View File

@@ -2694,7 +2694,10 @@ namespace ts {
break;
case SyntaxKind.SourceFile:
// this.property = assignment in a source file -- declare symbol in exports for a module, in locals for a script
if ((thisContainer as SourceFile).commonJsModuleIndicator) {
if (hasDynamicName(node)) {
break;
}
else if ((thisContainer as SourceFile).commonJsModuleIndicator) {
declareSymbol(thisContainer.symbol.exports!, thisContainer.symbol, node, SymbolFlags.Property | SymbolFlags.ExportValue, SymbolFlags.None);
}
else {

View File

@@ -0,0 +1,10 @@
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'.
No index signature with a parameter of type 'string' was found on type 'typeof globalThis'.
==== tests/cases/conformance/salsa/thisPropertyAssignmentComputed.js (1 errors) ====
this["a" + "b"] = 0
~~~~~~~~~~~~~~~
!!! error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof globalThis'.
!!! error TS7053: No index signature with a parameter of type 'string' was found on type 'typeof globalThis'.

View File

@@ -0,0 +1,4 @@
=== tests/cases/conformance/salsa/thisPropertyAssignmentComputed.js ===
this["a" + "b"] = 0
>this : Symbol(globalThis)

View File

@@ -0,0 +1,10 @@
=== tests/cases/conformance/salsa/thisPropertyAssignmentComputed.js ===
this["a" + "b"] = 0
>this["a" + "b"] = 0 : 0
>this["a" + "b"] : any
>this : typeof globalThis
>"a" + "b" : string
>"a" : "a"
>"b" : "b"
>0 : 0

View File

@@ -0,0 +1,6 @@
// @allowjs: true
// @checkjs: true
// @noemit: true
// @strict: true
// @filename: thisPropertyAssignmentComputed.js
this["a" + "b"] = 0