Skip to content

Commit

Permalink
Use local symbol rather then target symbol for tracking reused refere…
Browse files Browse the repository at this point in the history
…nces (#59493)
  • Loading branch information
weswigham authored and typescript-bot committed Aug 1, 2024
1 parent 5270c59 commit 51093ed
Show file tree
Hide file tree
Showing 15 changed files with 324 additions and 26 deletions.
3 changes: 3 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8387,6 +8387,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
introducesError = true;
return { introducesError, node, sym };
}
else {
sym = symAtLocation;
}
}

if (sym) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//// [tests/cases/compiler/declarationEmitEnumReferenceViaImportEquals.ts] ////

//// [translation.ts]
export interface Translation {
translationKey: Translation.TranslationKeyEnum;
}

export namespace Translation {
export type TranslationKeyEnum = 'translation1' | 'translation2';
export const TranslationKeyEnum = {
Translation1: 'translation1' as TranslationKeyEnum,
Translation2: 'translation2' as TranslationKeyEnum,
}
}


//// [test.ts]
import { Translation } from "./translation";
import TranslationKeyEnum = Translation.TranslationKeyEnum;

export class Test {
TranslationKeyEnum = TranslationKeyEnum;
print() {
console.log(TranslationKeyEnum.Translation1);
}
}

//// [index.ts]
import { Test } from "./test";
new Test().print();

//// [translation.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Translation = void 0;
var Translation;
(function (Translation) {
Translation.TranslationKeyEnum = {
Translation1: 'translation1',
Translation2: 'translation2',
};
})(Translation || (exports.Translation = Translation = {}));
//// [test.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Test = void 0;
var translation_1 = require("./translation");
var TranslationKeyEnum = translation_1.Translation.TranslationKeyEnum;
var Test = /** @class */ (function () {
function Test() {
this.TranslationKeyEnum = TranslationKeyEnum;
}
Test.prototype.print = function () {
console.log(TranslationKeyEnum.Translation1);
};
return Test;
}());
exports.Test = Test;
//// [index.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var test_1 = require("./test");
new test_1.Test().print();


//// [translation.d.ts]
export interface Translation {
translationKey: Translation.TranslationKeyEnum;
}
export declare namespace Translation {
type TranslationKeyEnum = 'translation1' | 'translation2';
const TranslationKeyEnum: {
Translation1: TranslationKeyEnum;
Translation2: TranslationKeyEnum;
};
}
//// [test.d.ts]
import { Translation } from "./translation";
import TranslationKeyEnum = Translation.TranslationKeyEnum;
export declare class Test {
TranslationKeyEnum: {
Translation1: TranslationKeyEnum;
Translation2: TranslationKeyEnum;
};
print(): void;
}
//// [index.d.ts]
export {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//// [tests/cases/compiler/declarationEmitEnumReferenceViaImportEquals.ts] ////

=== translation.ts ===
export interface Translation {
>Translation : Symbol(Translation, Decl(translation.ts, 0, 0), Decl(translation.ts, 2, 1))

translationKey: Translation.TranslationKeyEnum;
>translationKey : Symbol(Translation.translationKey, Decl(translation.ts, 0, 30))
>Translation : Symbol(Translation, Decl(translation.ts, 0, 0), Decl(translation.ts, 2, 1))
>TranslationKeyEnum : Symbol(Translation.TranslationKeyEnum, Decl(translation.ts, 4, 30), Decl(translation.ts, 6, 14))
}

export namespace Translation {
>Translation : Symbol(Translation, Decl(translation.ts, 0, 0), Decl(translation.ts, 2, 1))

export type TranslationKeyEnum = 'translation1' | 'translation2';
>TranslationKeyEnum : Symbol(TranslationKeyEnum, Decl(translation.ts, 4, 30), Decl(translation.ts, 6, 14))

export const TranslationKeyEnum = {
>TranslationKeyEnum : Symbol(TranslationKeyEnum, Decl(translation.ts, 4, 30), Decl(translation.ts, 6, 14))

Translation1: 'translation1' as TranslationKeyEnum,
>Translation1 : Symbol(Translation1, Decl(translation.ts, 6, 37))
>TranslationKeyEnum : Symbol(TranslationKeyEnum, Decl(translation.ts, 4, 30), Decl(translation.ts, 6, 14))

Translation2: 'translation2' as TranslationKeyEnum,
>Translation2 : Symbol(Translation2, Decl(translation.ts, 7, 55))
>TranslationKeyEnum : Symbol(TranslationKeyEnum, Decl(translation.ts, 4, 30), Decl(translation.ts, 6, 14))
}
}


=== test.ts ===
import { Translation } from "./translation";
>Translation : Symbol(Translation, Decl(test.ts, 0, 8))

import TranslationKeyEnum = Translation.TranslationKeyEnum;
>TranslationKeyEnum : Symbol(TranslationKeyEnum, Decl(test.ts, 0, 44))
>Translation : Symbol(Translation, Decl(test.ts, 0, 8))
>TranslationKeyEnum : Symbol(Translation.TranslationKeyEnum, Decl(translation.ts, 4, 30), Decl(translation.ts, 6, 14))

export class Test {
>Test : Symbol(Test, Decl(test.ts, 1, 59))

TranslationKeyEnum = TranslationKeyEnum;
>TranslationKeyEnum : Symbol(Test.TranslationKeyEnum, Decl(test.ts, 3, 19))
>TranslationKeyEnum : Symbol(TranslationKeyEnum, Decl(test.ts, 0, 44))

print() {
>print : Symbol(Test.print, Decl(test.ts, 4, 42))

console.log(TranslationKeyEnum.Translation1);
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>TranslationKeyEnum.Translation1 : Symbol(Translation1, Decl(translation.ts, 6, 37))
>TranslationKeyEnum : Symbol(TranslationKeyEnum, Decl(test.ts, 0, 44))
>Translation1 : Symbol(Translation1, Decl(translation.ts, 6, 37))
}
}

=== index.ts ===
import { Test } from "./test";
>Test : Symbol(Test, Decl(index.ts, 0, 8))

new Test().print();
>new Test().print : Symbol(Test.print, Decl(test.ts, 4, 42))
>Test : Symbol(Test, Decl(index.ts, 0, 8))
>print : Symbol(Test.print, Decl(test.ts, 4, 42))

Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//// [tests/cases/compiler/declarationEmitEnumReferenceViaImportEquals.ts] ////

=== translation.ts ===
export interface Translation {
translationKey: Translation.TranslationKeyEnum;
>translationKey : Translation.TranslationKeyEnum
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>Translation : any
> : ^^^
}

export namespace Translation {
>Translation : typeof Translation
> : ^^^^^^^^^^^^^^^^^^

export type TranslationKeyEnum = 'translation1' | 'translation2';
>TranslationKeyEnum : TranslationKeyEnum
> : ^^^^^^^^^^^^^^^^^^

export const TranslationKeyEnum = {
>TranslationKeyEnum : { Translation1: TranslationKeyEnum; Translation2: TranslationKeyEnum; }
> : ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^ ^^^
>{ Translation1: 'translation1' as TranslationKeyEnum, Translation2: 'translation2' as TranslationKeyEnum, } : { Translation1: TranslationKeyEnum; Translation2: TranslationKeyEnum; }
> : ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^ ^^^

Translation1: 'translation1' as TranslationKeyEnum,
>Translation1 : TranslationKeyEnum
> : ^^^^^^^^^^^^^^^^^^
>'translation1' as TranslationKeyEnum : TranslationKeyEnum
> : ^^^^^^^^^^^^^^^^^^
>'translation1' : "translation1"
> : ^^^^^^^^^^^^^^

Translation2: 'translation2' as TranslationKeyEnum,
>Translation2 : TranslationKeyEnum
> : ^^^^^^^^^^^^^^^^^^
>'translation2' as TranslationKeyEnum : TranslationKeyEnum
> : ^^^^^^^^^^^^^^^^^^
>'translation2' : "translation2"
> : ^^^^^^^^^^^^^^
}
}


=== test.ts ===
import { Translation } from "./translation";
>Translation : typeof Translation
> : ^^^^^^^^^^^^^^^^^^

import TranslationKeyEnum = Translation.TranslationKeyEnum;
>TranslationKeyEnum : { Translation1: TranslationKeyEnum; Translation2: TranslationKeyEnum; }
> : ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^ ^^^
>Translation : Translation
> : ^^^^^^^^^^^
>TranslationKeyEnum : Translation.TranslationKeyEnum
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

export class Test {
>Test : Test
> : ^^^^

TranslationKeyEnum = TranslationKeyEnum;
>TranslationKeyEnum : { Translation1: TranslationKeyEnum; Translation2: TranslationKeyEnum; }
> : ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^ ^^^
>TranslationKeyEnum : { Translation1: TranslationKeyEnum; Translation2: TranslationKeyEnum; }
> : ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^ ^^^

print() {
>print : () => void
> : ^^^^^^^^^^

console.log(TranslationKeyEnum.Translation1);
>console.log(TranslationKeyEnum.Translation1) : void
> : ^^^^
>console.log : (...data: any[]) => void
> : ^^^^ ^^ ^^^^^
>console : Console
> : ^^^^^^^
>log : (...data: any[]) => void
> : ^^^^ ^^ ^^^^^
>TranslationKeyEnum.Translation1 : Translation.TranslationKeyEnum
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>TranslationKeyEnum : { Translation1: TranslationKeyEnum; Translation2: TranslationKeyEnum; }
> : ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^ ^^^
>Translation1 : Translation.TranslationKeyEnum
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
}

=== index.ts ===
import { Test } from "./test";
>Test : typeof Test
> : ^^^^^^^^^^^

new Test().print();
>new Test().print() : void
> : ^^^^
>new Test().print : () => void
> : ^^^^^^^^^^
>new Test() : Test
> : ^^^^
>Test : typeof Test
> : ^^^^^^^^^^^
>print : () => void
> : ^^^^^^^^^^

Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function child1(prototype: ParentThing) {
=== parent.ts ===
import { child1 } from './child1'; // this import should still exist in some form in the output, since it augments this module
>child1 : (prototype: ParentThing) => void
> : ^ ^^^^^^^^^^^^^^^^^^^^^^
> : ^ ^^ ^^^^^^^^^

export class ParentThing implements ParentThing {}
>ParentThing : ParentThing
Expand All @@ -62,7 +62,7 @@ child1(ParentThing.prototype);
>child1(ParentThing.prototype) : void
> : ^^^^
>child1 : (prototype: ParentThing) => void
> : ^ ^^^^^^^^^^^^^^^^^^^^^^
> : ^ ^^ ^^^^^^^^^
>ParentThing.prototype : ParentThing
> : ^^^^^^^^^^^
>ParentThing : typeof ParentThing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,18 @@ export { shouldLookupName, shouldReuseLocalName, reuseDepName, shouldBeElided };
=== src/index.ts ===
import { goodDeclaration, shouldReuseLocalName, shouldBeElided } from "some-dep";
>goodDeclaration : <T>() => () => { shouldPrintResult: T extends import("node_modules/some-dep/dist/inner").Other ? "O" : "N"; shouldPrintResult2: T extends typeof shouldBeElided ? import("node_modules/some-dep/dist/inner").Other : "N"; shouldLookupName: typeof import("node_modules/some-dep/dist/other").shouldLookupName; shouldReuseLocalName: typeof shouldReuseLocalName; reuseDepName: typeof import("node_modules/some-dep/dist/other").reuseDepName; }
> : ^ ^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^
> : ^ ^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^
>shouldReuseLocalName : unique symbol
> : ^^^^^^^^^^^^^
>shouldBeElided : unique symbol
> : ^^^^^^^^^^^^^

export const bar = goodDeclaration<{}>;
>bar : () => () => { shouldPrintResult: "N"; shouldPrintResult2: "N"; shouldLookupName: typeof import("node_modules/some-dep/dist/other").shouldLookupName; shouldReuseLocalName: typeof shouldReuseLocalName; reuseDepName: typeof import("node_modules/some-dep/dist/other").reuseDepName; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^
>goodDeclaration<{}> : () => () => { shouldPrintResult: "N"; shouldPrintResult2: "N"; shouldLookupName: typeof import("node_modules/some-dep/dist/other").shouldLookupName; shouldReuseLocalName: typeof shouldReuseLocalName; reuseDepName: typeof import("node_modules/some-dep/dist/other").reuseDepName; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^
>goodDeclaration : <T>() => () => { shouldPrintResult: T extends import("node_modules/some-dep/dist/inner").Other ? "O" : "N"; shouldPrintResult2: T extends typeof shouldBeElided ? import("node_modules/some-dep/dist/inner").Other : "N"; shouldLookupName: typeof import("node_modules/some-dep/dist/other").shouldLookupName; shouldReuseLocalName: typeof shouldReuseLocalName; reuseDepName: typeof import("node_modules/some-dep/dist/other").reuseDepName; }
> : ^ ^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^
> : ^ ^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^


Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ import { MyBaseClass } from './BaseClass';

import { MyMixin } from './MixinClass';
>MyMixin : <T extends import("BaseClass").Constructor<MyBaseClass<any>>>(base: T) => T & import("BaseClass").Constructor<MyMixin>
> : ^ ^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^ ^^ ^^ ^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^
> : ^ ^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^ ^^ ^^ ^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^

export class MyExtendedClass extends MyMixin(MyBaseClass)<string> {
>MyExtendedClass : MyExtendedClass
> : ^^^^^^^^^^^^^^^
>MyMixin(MyBaseClass) : MyBaseClass<string> & MyMixin
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>MyMixin : <T extends import("BaseClass").Constructor<MyBaseClass<any>>>(base: T) => T & import("BaseClass").Constructor<MyMixin>
> : ^ ^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^ ^^ ^^ ^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^
> : ^ ^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^ ^^ ^^ ^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^
>MyBaseClass : typeof MyBaseClass
> : ^^^^^^^^^^^^^^^^^^

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

=== functionVariableInReturnTypeAnnotation.ts ===
function bar(): typeof b {
>bar : () => typeof b
> : ^^^^^^
>bar : () => any
> : ^^^^^^^^^
>b : any
> : ^^^

Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/importDecl.types
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@ export var d = m5.foo2();
>m5.foo2() : m4.d
> : ^^^^
>m5.foo2 : () => m4.d
> : ^^^^^^^^^^
> : ^^^^^^
>m5 : typeof m5
> : ^^^^^^^^^
>foo2 : () => m4.d
> : ^^^^^^^^^^
> : ^^^^^^

// Do not emit multiple used import statements
import multiImport_m4 = require("./importDecl_require"); // Emit used
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function child1(prototype: ParentThing) {
=== parent.ts ===
import { child1 } from './child1'; // this import should still exist in some form in the output, since it augments this module
>child1 : (prototype: ParentThing) => void
> : ^ ^^^^^^^^^^^^^^^^^^^^^^
> : ^ ^^ ^^^^^^^^^

export class ParentThing implements ParentThing {}
>ParentThing : ParentThing
Expand All @@ -62,7 +62,7 @@ child1(ParentThing.prototype);
>child1(ParentThing.prototype) : void
> : ^^^^
>child1 : (prototype: ParentThing) => void
> : ^ ^^^^^^^^^^^^^^^^^^^^^^
> : ^ ^^ ^^^^^^^^^
>ParentThing.prototype : ParentThing
> : ^^^^^^^^^^^
>ParentThing : typeof ParentThing
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/umd-augmentation-1.types
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ p = v.reverse();
> : ^^^^^^^
>v.reverse() : m.Point
> : ^^^^^^^
>v.reverse : () => m.Point
> : ^^^^^^^^^^^^^
>v.reverse : () => Math2d.Point
> : ^^^^^^
>v : m.Vector
> : ^^^^^^^^
>reverse : () => m.Point
> : ^^^^^^^^^^^^^
>reverse : () => Math2d.Point
> : ^^^^^^

var t = p.x;
>t : number
Expand Down
Loading

0 comments on commit 51093ed

Please sign in to comment.