Use local symbol rather then target symbol for tracking reused references (#59493)

This commit is contained in:
Wesley Wigham 2024-07-31 15:52:05 -07:00 коммит произвёл GitHub
Родитель 5e9b07001e
Коммит 41b993bebe
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
15 изменённых файлов: 324 добавлений и 26 удалений

Просмотреть файл

@ -8428,6 +8428,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
introducesError = true;
return { introducesError, node, sym };
}
else {
sym = symAtLocation;
}
}
if (sym) {

Просмотреть файл

@ -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 {};

Просмотреть файл

@ -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))

Просмотреть файл

@ -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
> : ^^^^^^^^^^

Просмотреть файл

@ -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
@ -62,7 +62,7 @@ child1(ParentThing.prototype);
>child1(ParentThing.prototype) : void
> : ^^^^
>child1 : (prototype: ParentThing) => void
> : ^ ^^^^^^^^^^^^^^^^^^^^^^
> : ^ ^^ ^^^^^^^^^
>ParentThing.prototype : ParentThing
> : ^^^^^^^^^^^
>ParentThing : typeof ParentThing

Просмотреть файл

@ -98,7 +98,7 @@ 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
@ -106,10 +106,10 @@ import { goodDeclaration, shouldReuseLocalName, shouldBeElided } from "some-dep"
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; }
> : ^ ^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^
> : ^ ^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^

Просмотреть файл

@ -56,7 +56,7 @@ 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
@ -64,7 +64,7 @@ export class MyExtendedClass extends MyMixin(MyBaseClass)<string> {
>MyMixin(MyBaseClass) : MyBaseClass<string> & MyMixin
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>MyMixin : <T extends import("BaseClass").Constructor<MyBaseClass<any>>>(base: T) => T & import("BaseClass").Constructor<MyMixin>
> : ^ ^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^ ^^ ^^ ^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^
> : ^ ^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^ ^^ ^^ ^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^
>MyBaseClass : typeof MyBaseClass
> : ^^^^^^^^^^^^^^^^^^

Просмотреть файл

@ -2,8 +2,8 @@
=== functionVariableInReturnTypeAnnotation.ts ===
function bar(): typeof b {
>bar : () => typeof b
> : ^^^^^^
>bar : () => any
> : ^^^^^^^^^
>b : any
> : ^^^

Просмотреть файл

@ -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

Просмотреть файл

@ -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
@ -62,7 +62,7 @@ child1(ParentThing.prototype);
>child1(ParentThing.prototype) : void
> : ^^^^
>child1 : (prototype: ParentThing) => void
> : ^ ^^^^^^^^^^^^^^^^^^^^^^
> : ^ ^^ ^^^^^^^^^
>ParentThing.prototype : ParentThing
> : ^^^^^^^^^^^
>ParentThing : typeof ParentThing

Просмотреть файл

@ -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

Просмотреть файл

@ -59,11 +59,11 @@ p = v.reverse();
>v.reverse() : Math2d.Point
> : ^^^^^^^^^^^^
>v.reverse : () => Math2d.Point
> : ^^^^^^^^^^^^^^^^^^
> : ^^^^^^
>v : Math2d.Vector
> : ^^^^^^^^^^^^^
>reverse : () => Math2d.Point
> : ^^^^^^^^^^^^^^^^^^
> : ^^^^^^
var t = p.x;
>t : number

Просмотреть файл

@ -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

Просмотреть файл

@ -59,11 +59,11 @@ p = v.reverse();
>v.reverse() : Math2d.Point
> : ^^^^^^^^^^^^
>v.reverse : () => Math2d.Point
> : ^^^^^^^^^^^^^^^^^^
> : ^^^^^^
>v : Math2d.Vector
> : ^^^^^^^^^^^^^
>reverse : () => Math2d.Point
> : ^^^^^^^^^^^^^^^^^^
> : ^^^^^^
var t = p.x;
>t : number

Просмотреть файл

@ -0,0 +1,31 @@
// @strict: true
// @declaration: true
// @filename: 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,
}
}
// @filename: test.ts
import { Translation } from "./translation";
import TranslationKeyEnum = Translation.TranslationKeyEnum;
export class Test {
TranslationKeyEnum = TranslationKeyEnum;
print() {
console.log(TranslationKeyEnum.Translation1);
}
}
// @filename: index.ts
import { Test } from "./test";
new Test().print();