From 83617091a362735350fadb3c1c217925f9b90892 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Fri, 8 Jun 2018 13:57:25 -0700 Subject: [PATCH] Always resolve the first identifier of computed property name to get the symbol and track it Fixes #24798 --- src/compiler/checker.ts | 10 ++-- ...eclarationEmitWithDefaultAsComputedName.js | 46 ++++++++++++++++ ...ationEmitWithDefaultAsComputedName.symbols | 45 ++++++++++++++++ ...arationEmitWithDefaultAsComputedName.types | 50 ++++++++++++++++++ ...clarationEmitWithDefaultAsComputedName2.js | 46 ++++++++++++++++ ...tionEmitWithDefaultAsComputedName2.symbols | 47 +++++++++++++++++ ...rationEmitWithDefaultAsComputedName2.types | 52 +++++++++++++++++++ ...eclarationEmitWithDefaultAsComputedName.ts | 19 +++++++ ...clarationEmitWithDefaultAsComputedName2.ts | 19 +++++++ 9 files changed, 331 insertions(+), 3 deletions(-) create mode 100644 tests/baselines/reference/declarationEmitWithDefaultAsComputedName.js create mode 100644 tests/baselines/reference/declarationEmitWithDefaultAsComputedName.symbols create mode 100644 tests/baselines/reference/declarationEmitWithDefaultAsComputedName.types create mode 100644 tests/baselines/reference/declarationEmitWithDefaultAsComputedName2.js create mode 100644 tests/baselines/reference/declarationEmitWithDefaultAsComputedName2.symbols create mode 100644 tests/baselines/reference/declarationEmitWithDefaultAsComputedName2.types create mode 100644 tests/cases/compiler/declarationEmitWithDefaultAsComputedName.ts create mode 100644 tests/cases/compiler/declarationEmitWithDefaultAsComputedName2.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 54283da848c72..c1a1233d90d0a 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -3536,9 +3536,13 @@ namespace ts { context.enclosingDeclaration = undefined; if (getCheckFlags(propertySymbol) & CheckFlags.Late) { const decl = firstOrUndefined(propertySymbol.declarations); - const name = hasLateBindableName(decl) && resolveEntityName(decl.name.expression, SymbolFlags.Value); - if (name && context.tracker.trackSymbol) { - context.tracker.trackSymbol(name, saveEnclosingDeclaration, SymbolFlags.Value); + if (context.tracker.trackSymbol && hasLateBindableName(decl)) { + // get symbol of the first identifier of the entityName + const firstIdentifier = getFirstIdentifier(decl.name.expression); + const name = resolveName(firstIdentifier, firstIdentifier.escapedText, SymbolFlags.Value | SymbolFlags.ExportValue, /*nodeNotFoundErrorMessage*/ undefined, /*nameArg*/ undefined, /*isUse*/ true); + if (name) { + context.tracker.trackSymbol(name, saveEnclosingDeclaration, SymbolFlags.Value); + } } } const propertyName = symbolToName(propertySymbol, context, SymbolFlags.Value, /*expectsIdentifier*/ true); diff --git a/tests/baselines/reference/declarationEmitWithDefaultAsComputedName.js b/tests/baselines/reference/declarationEmitWithDefaultAsComputedName.js new file mode 100644 index 0000000000000..ae45c09438f2d --- /dev/null +++ b/tests/baselines/reference/declarationEmitWithDefaultAsComputedName.js @@ -0,0 +1,46 @@ +//// [tests/cases/compiler/declarationEmitWithDefaultAsComputedName.ts] //// + +//// [other.ts] +type Experiment = { + name: Name; +}; +declare const createExperiment: ( + options: Experiment +) => Experiment; +export default createExperiment({ + name: "foo" +}); + +//// [main.ts] +import other from "./other"; +export const obj = { + [other.name]: 1, +}; + +//// [other.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.default = createExperiment({ + name: "foo" +}); +//// [main.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +var _a; +var other_1 = require("./other"); +exports.obj = (_a = {}, + _a[other_1.default.name] = 1, + _a); + + +//// [other.d.ts] +declare type Experiment = { + name: Name; +}; +declare const _default: Experiment<"foo">; +export default _default; +//// [main.d.ts] +import other from "./other"; +export declare const obj: { + [other.name]: number; +}; diff --git a/tests/baselines/reference/declarationEmitWithDefaultAsComputedName.symbols b/tests/baselines/reference/declarationEmitWithDefaultAsComputedName.symbols new file mode 100644 index 0000000000000..50d91d4c6030a --- /dev/null +++ b/tests/baselines/reference/declarationEmitWithDefaultAsComputedName.symbols @@ -0,0 +1,45 @@ +=== tests/cases/compiler/other.ts === +type Experiment = { +>Experiment : Symbol(Experiment, Decl(other.ts, 0, 0)) +>Name : Symbol(Name, Decl(other.ts, 0, 16)) + + name: Name; +>name : Symbol(name, Decl(other.ts, 0, 25)) +>Name : Symbol(Name, Decl(other.ts, 0, 16)) + +}; +declare const createExperiment: ( +>createExperiment : Symbol(createExperiment, Decl(other.ts, 3, 13)) +>Name : Symbol(Name, Decl(other.ts, 3, 33)) + + options: Experiment +>options : Symbol(options, Decl(other.ts, 3, 54)) +>Experiment : Symbol(Experiment, Decl(other.ts, 0, 0)) +>Name : Symbol(Name, Decl(other.ts, 3, 33)) + +) => Experiment; +>Experiment : Symbol(Experiment, Decl(other.ts, 0, 0)) +>Name : Symbol(Name, Decl(other.ts, 3, 33)) + +export default createExperiment({ +>createExperiment : Symbol(createExperiment, Decl(other.ts, 3, 13)) + + name: "foo" +>name : Symbol(name, Decl(other.ts, 6, 33)) + +}); + +=== tests/cases/compiler/main.ts === +import other from "./other"; +>other : Symbol(other, Decl(main.ts, 0, 6)) + +export const obj = { +>obj : Symbol(obj, Decl(main.ts, 1, 12)) + + [other.name]: 1, +>[other.name] : Symbol([other.name], Decl(main.ts, 1, 20)) +>other.name : Symbol(name, Decl(other.ts, 0, 25)) +>other : Symbol(other, Decl(main.ts, 0, 6)) +>name : Symbol(name, Decl(other.ts, 0, 25)) + +}; diff --git a/tests/baselines/reference/declarationEmitWithDefaultAsComputedName.types b/tests/baselines/reference/declarationEmitWithDefaultAsComputedName.types new file mode 100644 index 0000000000000..ef11f7c2f5964 --- /dev/null +++ b/tests/baselines/reference/declarationEmitWithDefaultAsComputedName.types @@ -0,0 +1,50 @@ +=== tests/cases/compiler/other.ts === +type Experiment = { +>Experiment : Experiment +>Name : Name + + name: Name; +>name : Name +>Name : Name + +}; +declare const createExperiment: ( +>createExperiment : (options: Experiment) => Experiment +>Name : Name + + options: Experiment +>options : Experiment +>Experiment : Experiment +>Name : Name + +) => Experiment; +>Experiment : Experiment +>Name : Name + +export default createExperiment({ +>createExperiment({ name: "foo"}) : Experiment<"foo"> +>createExperiment : (options: Experiment) => Experiment +>{ name: "foo"} : { name: "foo"; } + + name: "foo" +>name : "foo" +>"foo" : "foo" + +}); + +=== tests/cases/compiler/main.ts === +import other from "./other"; +>other : { name: "foo"; } + +export const obj = { +>obj : { [other.name]: number; } +>{ [other.name]: 1,} : { [other.name]: number; } + + [other.name]: 1, +>[other.name] : number +>other.name : "foo" +>other : { name: "foo"; } +>name : "foo" +>1 : 1 + +}; diff --git a/tests/baselines/reference/declarationEmitWithDefaultAsComputedName2.js b/tests/baselines/reference/declarationEmitWithDefaultAsComputedName2.js new file mode 100644 index 0000000000000..ddf89f2d57dfb --- /dev/null +++ b/tests/baselines/reference/declarationEmitWithDefaultAsComputedName2.js @@ -0,0 +1,46 @@ +//// [tests/cases/compiler/declarationEmitWithDefaultAsComputedName2.ts] //// + +//// [other.ts] +type Experiment = { + name: Name; +}; +declare const createExperiment: ( + options: Experiment +) => Experiment; +export default createExperiment({ + name: "foo" +}); + +//// [main.ts] +import * as other2 from "./other"; +export const obj = { + [other2.default.name]: 1 +}; + +//// [other.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.default = createExperiment({ + name: "foo" +}); +//// [main.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +var _a; +var other2 = require("./other"); +exports.obj = (_a = {}, + _a[other2.default.name] = 1, + _a); + + +//// [other.d.ts] +declare type Experiment = { + name: Name; +}; +declare const _default: Experiment<"foo">; +export default _default; +//// [main.d.ts] +import * as other2 from "./other"; +export declare const obj: { + [other2.default.name]: number; +}; diff --git a/tests/baselines/reference/declarationEmitWithDefaultAsComputedName2.symbols b/tests/baselines/reference/declarationEmitWithDefaultAsComputedName2.symbols new file mode 100644 index 0000000000000..3f4be4d8b43bf --- /dev/null +++ b/tests/baselines/reference/declarationEmitWithDefaultAsComputedName2.symbols @@ -0,0 +1,47 @@ +=== tests/cases/compiler/other.ts === +type Experiment = { +>Experiment : Symbol(Experiment, Decl(other.ts, 0, 0)) +>Name : Symbol(Name, Decl(other.ts, 0, 16)) + + name: Name; +>name : Symbol(name, Decl(other.ts, 0, 25)) +>Name : Symbol(Name, Decl(other.ts, 0, 16)) + +}; +declare const createExperiment: ( +>createExperiment : Symbol(createExperiment, Decl(other.ts, 3, 13)) +>Name : Symbol(Name, Decl(other.ts, 3, 33)) + + options: Experiment +>options : Symbol(options, Decl(other.ts, 3, 54)) +>Experiment : Symbol(Experiment, Decl(other.ts, 0, 0)) +>Name : Symbol(Name, Decl(other.ts, 3, 33)) + +) => Experiment; +>Experiment : Symbol(Experiment, Decl(other.ts, 0, 0)) +>Name : Symbol(Name, Decl(other.ts, 3, 33)) + +export default createExperiment({ +>createExperiment : Symbol(createExperiment, Decl(other.ts, 3, 13)) + + name: "foo" +>name : Symbol(name, Decl(other.ts, 6, 33)) + +}); + +=== tests/cases/compiler/main.ts === +import * as other2 from "./other"; +>other2 : Symbol(other2, Decl(main.ts, 0, 6)) + +export const obj = { +>obj : Symbol(obj, Decl(main.ts, 1, 12)) + + [other2.default.name]: 1 +>[other2.default.name] : Symbol([other2.default.name], Decl(main.ts, 1, 20)) +>other2.default.name : Symbol(name, Decl(other.ts, 0, 25)) +>other2.default : Symbol(other2.default, Decl(other.ts, 5, 22)) +>other2 : Symbol(other2, Decl(main.ts, 0, 6)) +>default : Symbol(other2.default, Decl(other.ts, 5, 22)) +>name : Symbol(name, Decl(other.ts, 0, 25)) + +}; diff --git a/tests/baselines/reference/declarationEmitWithDefaultAsComputedName2.types b/tests/baselines/reference/declarationEmitWithDefaultAsComputedName2.types new file mode 100644 index 0000000000000..a507034378bbb --- /dev/null +++ b/tests/baselines/reference/declarationEmitWithDefaultAsComputedName2.types @@ -0,0 +1,52 @@ +=== tests/cases/compiler/other.ts === +type Experiment = { +>Experiment : Experiment +>Name : Name + + name: Name; +>name : Name +>Name : Name + +}; +declare const createExperiment: ( +>createExperiment : (options: Experiment) => Experiment +>Name : Name + + options: Experiment +>options : Experiment +>Experiment : Experiment +>Name : Name + +) => Experiment; +>Experiment : Experiment +>Name : Name + +export default createExperiment({ +>createExperiment({ name: "foo"}) : Experiment<"foo"> +>createExperiment : (options: Experiment) => Experiment +>{ name: "foo"} : { name: "foo"; } + + name: "foo" +>name : "foo" +>"foo" : "foo" + +}); + +=== tests/cases/compiler/main.ts === +import * as other2 from "./other"; +>other2 : typeof other2 + +export const obj = { +>obj : { [other2.default.name]: number; } +>{ [other2.default.name]: 1} : { [other2.default.name]: number; } + + [other2.default.name]: 1 +>[other2.default.name] : number +>other2.default.name : "foo" +>other2.default : { name: "foo"; } +>other2 : typeof other2 +>default : { name: "foo"; } +>name : "foo" +>1 : 1 + +}; diff --git a/tests/cases/compiler/declarationEmitWithDefaultAsComputedName.ts b/tests/cases/compiler/declarationEmitWithDefaultAsComputedName.ts new file mode 100644 index 0000000000000..868bad4484783 --- /dev/null +++ b/tests/cases/compiler/declarationEmitWithDefaultAsComputedName.ts @@ -0,0 +1,19 @@ +// @declaration: true +// @target: es5 + +// @filename: other.ts +type Experiment = { + name: Name; +}; +declare const createExperiment: ( + options: Experiment +) => Experiment; +export default createExperiment({ + name: "foo" +}); + +// @filename: main.ts +import other from "./other"; +export const obj = { + [other.name]: 1, +}; \ No newline at end of file diff --git a/tests/cases/compiler/declarationEmitWithDefaultAsComputedName2.ts b/tests/cases/compiler/declarationEmitWithDefaultAsComputedName2.ts new file mode 100644 index 0000000000000..8feeaa2fa61dc --- /dev/null +++ b/tests/cases/compiler/declarationEmitWithDefaultAsComputedName2.ts @@ -0,0 +1,19 @@ +// @declaration: true +// @target: es5 + +// @filename: other.ts +type Experiment = { + name: Name; +}; +declare const createExperiment: ( + options: Experiment +) => Experiment; +export default createExperiment({ + name: "foo" +}); + +// @filename: main.ts +import * as other2 from "./other"; +export const obj = { + [other2.default.name]: 1 +}; \ No newline at end of file