From b98cb0645d2e44947b369a4f7d8921f7d5ca8c56 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Wed, 11 Dec 2019 15:55:08 -0800 Subject: [PATCH] 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. --- src/compiler/binder.ts | 5 ++++- .../thisPropertyAssignmentComputed.errors.txt | 10 ++++++++++ .../reference/thisPropertyAssignmentComputed.symbols | 4 ++++ .../reference/thisPropertyAssignmentComputed.types | 10 ++++++++++ .../salsa/thisPropertyAssignmentComputed.ts | 6 ++++++ 5 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/thisPropertyAssignmentComputed.errors.txt create mode 100644 tests/baselines/reference/thisPropertyAssignmentComputed.symbols create mode 100644 tests/baselines/reference/thisPropertyAssignmentComputed.types create mode 100644 tests/cases/conformance/salsa/thisPropertyAssignmentComputed.ts diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 262d8c72268..990674b7b40 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -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 { diff --git a/tests/baselines/reference/thisPropertyAssignmentComputed.errors.txt b/tests/baselines/reference/thisPropertyAssignmentComputed.errors.txt new file mode 100644 index 00000000000..e38c2c17546 --- /dev/null +++ b/tests/baselines/reference/thisPropertyAssignmentComputed.errors.txt @@ -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'. + \ No newline at end of file diff --git a/tests/baselines/reference/thisPropertyAssignmentComputed.symbols b/tests/baselines/reference/thisPropertyAssignmentComputed.symbols new file mode 100644 index 00000000000..72f1a5188e9 --- /dev/null +++ b/tests/baselines/reference/thisPropertyAssignmentComputed.symbols @@ -0,0 +1,4 @@ +=== tests/cases/conformance/salsa/thisPropertyAssignmentComputed.js === +this["a" + "b"] = 0 +>this : Symbol(globalThis) + diff --git a/tests/baselines/reference/thisPropertyAssignmentComputed.types b/tests/baselines/reference/thisPropertyAssignmentComputed.types new file mode 100644 index 00000000000..324518c76c0 --- /dev/null +++ b/tests/baselines/reference/thisPropertyAssignmentComputed.types @@ -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 + diff --git a/tests/cases/conformance/salsa/thisPropertyAssignmentComputed.ts b/tests/cases/conformance/salsa/thisPropertyAssignmentComputed.ts new file mode 100644 index 00000000000..37db10ca3b2 --- /dev/null +++ b/tests/cases/conformance/salsa/thisPropertyAssignmentComputed.ts @@ -0,0 +1,6 @@ +// @allowjs: true +// @checkjs: true +// @noemit: true +// @strict: true +// @filename: thisPropertyAssignmentComputed.js +this["a" + "b"] = 0