From 88adf89c62576e7441ca516e0ab01f014a53ed5a Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Wed, 11 Dec 2019 23:59:29 +0000 Subject: [PATCH] Cherry-pick PR #35639 into release-3.7 Component commits: 7c31be3b17 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. --- 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 a4d73056e0995..616fff013ac61 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -2744,7 +2744,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 0000000000000..e38c2c1754643 --- /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 0000000000000..72f1a5188e96c --- /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 0000000000000..324518c76c054 --- /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 0000000000000..37db10ca3b2a2 --- /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