From 07d3a2ec7e4bba39df98a9f1bd16bc04112b9716 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Wed, 23 Oct 2019 16:01:25 -0700 Subject: [PATCH] Do not consider element accesses which are neither statically bindable nor late bound as special assignments (#34679) --- src/compiler/utilities.ts | 8 +++++--- .../jsNegativeElementAccessNotBound.symbols | 7 +++++++ .../reference/jsNegativeElementAccessNotBound.types | 13 +++++++++++++ .../compiler/jsNegativeElementAccessNotBound.ts | 6 ++++++ 4 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 tests/baselines/reference/jsNegativeElementAccessNotBound.symbols create mode 100644 tests/baselines/reference/jsNegativeElementAccessNotBound.types create mode 100644 tests/cases/compiler/jsNegativeElementAccessNotBound.ts diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index e86f79b360a..12b11eeae29 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -2115,7 +2115,7 @@ namespace ts { if (expr.operatorToken.kind !== SyntaxKind.EqualsToken || !isAccessExpression(expr.left)) { return AssignmentDeclarationKind.None; } - if (isBindableStaticNameExpression(expr.left.expression) && getElementOrPropertyAccessName(expr.left) === "prototype" && isObjectLiteralExpression(getInitializerOfBinaryExpression(expr))) { + if (isBindableStaticNameExpression(expr.left.expression, /*excludeThisKeyword*/ true) && getElementOrPropertyAccessName(expr.left) === "prototype" && isObjectLiteralExpression(getInitializerOfBinaryExpression(expr))) { // F.prototype = { ... } return AssignmentDeclarationKind.Prototype; } @@ -2183,8 +2183,10 @@ namespace ts { // exports.name = expr OR module.exports.name = expr OR exports["name"] = expr ... return AssignmentDeclarationKind.ExportsProperty; } - // F.G...x = expr - return AssignmentDeclarationKind.Property; + if (isBindableStaticNameExpression(lhs, /*excludeThisKeyword*/ true) || (isElementAccessExpression(lhs) && isDynamicName(lhs) && lhs.expression.kind !== SyntaxKind.ThisKeyword)) { + // F.G...x = expr + return AssignmentDeclarationKind.Property; + } } return AssignmentDeclarationKind.None; diff --git a/tests/baselines/reference/jsNegativeElementAccessNotBound.symbols b/tests/baselines/reference/jsNegativeElementAccessNotBound.symbols new file mode 100644 index 00000000000..d8ff3f52e23 --- /dev/null +++ b/tests/baselines/reference/jsNegativeElementAccessNotBound.symbols @@ -0,0 +1,7 @@ +=== tests/cases/compiler/jsNegativeELementAccessNotBound.js === +var indexMap = {}; +>indexMap : Symbol(indexMap, Decl(jsNegativeELementAccessNotBound.js, 0, 3)) + +indexMap[-1] = 0; +>indexMap : Symbol(indexMap, Decl(jsNegativeELementAccessNotBound.js, 0, 3)) + diff --git a/tests/baselines/reference/jsNegativeElementAccessNotBound.types b/tests/baselines/reference/jsNegativeElementAccessNotBound.types new file mode 100644 index 00000000000..dbd5d4c005d --- /dev/null +++ b/tests/baselines/reference/jsNegativeElementAccessNotBound.types @@ -0,0 +1,13 @@ +=== tests/cases/compiler/jsNegativeELementAccessNotBound.js === +var indexMap = {}; +>indexMap : {} +>{} : {} + +indexMap[-1] = 0; +>indexMap[-1] = 0 : 0 +>indexMap[-1] : any +>indexMap : {} +>-1 : -1 +>1 : 1 +>0 : 0 + diff --git a/tests/cases/compiler/jsNegativeElementAccessNotBound.ts b/tests/cases/compiler/jsNegativeElementAccessNotBound.ts new file mode 100644 index 00000000000..c7a204e578f --- /dev/null +++ b/tests/cases/compiler/jsNegativeElementAccessNotBound.ts @@ -0,0 +1,6 @@ +// @allowJs: true +// @checkJs: true +// @noEmit: true +// @filename: jsNegativeELementAccessNotBound.js +var indexMap = {}; +indexMap[-1] = 0;