🤖 Pick PR #58750 (Preserve elided imports used in dec...) into release-5.5 (#58757)

Co-authored-by: Titian Cernicova-Dragomir <tcernicovad1@bloomberg.net>
This commit is contained in:
TypeScript Bot 2024-06-03 12:02:45 -07:00 committed by GitHub
parent 4ee5d0588d
commit 35d2952454
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 153 additions and 11 deletions

View File

@ -8522,10 +8522,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
function createRecoveryBoundary() {
let trackedSymbols: TrackedSymbol[];
let unreportedErrors: (() => void)[];
const oldTracker = context.tracker;
const oldTrackedSymbols = context.trackedSymbols;
context.trackedSymbols = [];
context.trackedSymbols = undefined;
const oldEncounteredError = context.encounteredError;
context.tracker = new SymbolTrackerImpl(context, {
...oldTracker.inner,
@ -8545,11 +8546,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
markError(() => oldTracker.reportNonSerializableProperty(name));
},
trackSymbol(sym, decl, meaning) {
const accessibility = isSymbolAccessible(sym, decl, meaning, /*shouldComputeAliasesToMakeVisible*/ false);
if (accessibility.accessibility !== SymbolAccessibility.Accessible) {
(context.trackedSymbols ??= []).push([sym, decl, meaning]);
return true;
}
(trackedSymbols ??= []).push([sym, decl, meaning]);
return false;
},
moduleResolverHost: context.tracker.moduleResolverHost,
@ -8566,13 +8563,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
function startRecoveryScope() {
const initialTrackedSymbolsTop = context.trackedSymbols?.length ?? 0;
const trackedSymbolsTop = trackedSymbols?.length ?? 0;
const unreportedErrorsTop = unreportedErrors?.length ?? 0;
return () => {
hadError = false;
// Reset the tracked symbols to before the error
if (context.trackedSymbols) {
context.trackedSymbols.length = initialTrackedSymbolsTop;
if (trackedSymbols) {
trackedSymbols.length = trackedSymbolsTop;
}
if (unreportedErrors) {
unreportedErrors.length = unreportedErrorsTop;
@ -8582,7 +8579,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
function finalizeBoundary() {
context.tracker = oldTracker;
const newTrackedSymbols = context.trackedSymbols;
context.trackedSymbols = oldTrackedSymbols;
context.encounteredError = oldEncounteredError;
@ -8590,7 +8586,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (hadError) {
return false;
}
newTrackedSymbols?.forEach(
trackedSymbols?.forEach(
([symbol, enclosingDeclaration, meaning]) =>
context.tracker.trackSymbol(
symbol,

View File

@ -0,0 +1,45 @@
//// [tests/cases/compiler/declarationEmitPreserveReferencedImports.ts] ////
//// [utils.ts]
export interface Evt { }
//// [decl.ts]
import {Evt} from './utils'
export const o = <T>(o: T) => () : T => null!
//// [main.ts]
import { o } from './decl'
import { Evt } from './utils'
export const f = { o: o({ v: null! as Evt}) };
//// [utils.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
//// [decl.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.o = void 0;
var o = function (o) { return function () { return null; }; };
exports.o = o;
//// [main.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.f = void 0;
var decl_1 = require("./decl");
exports.f = { o: (0, decl_1.o)({ v: null }) };
//// [utils.d.ts]
export interface Evt {
}
//// [decl.d.ts]
export declare const o: <T>(o: T) => () => T;
//// [main.d.ts]
import { Evt } from './utils';
export declare const f: {
o: () => {
v: Evt;
};
};

View File

@ -0,0 +1,32 @@
//// [tests/cases/compiler/declarationEmitPreserveReferencedImports.ts] ////
=== utils.ts ===
export interface Evt { }
>Evt : Symbol(Evt, Decl(utils.ts, 0, 0))
=== decl.ts ===
import {Evt} from './utils'
>Evt : Symbol(Evt, Decl(decl.ts, 0, 8))
export const o = <T>(o: T) => () : T => null!
>o : Symbol(o, Decl(decl.ts, 1, 12))
>T : Symbol(T, Decl(decl.ts, 1, 18))
>o : Symbol(o, Decl(decl.ts, 1, 21))
>T : Symbol(T, Decl(decl.ts, 1, 18))
>T : Symbol(T, Decl(decl.ts, 1, 18))
=== main.ts ===
import { o } from './decl'
>o : Symbol(o, Decl(main.ts, 0, 8))
import { Evt } from './utils'
>Evt : Symbol(Evt, Decl(main.ts, 1, 8))
export const f = { o: o({ v: null! as Evt}) };
>f : Symbol(f, Decl(main.ts, 3, 12))
>o : Symbol(o, Decl(main.ts, 3, 18))
>o : Symbol(o, Decl(main.ts, 0, 8))
>v : Symbol(v, Decl(main.ts, 3, 25))
>Evt : Symbol(Evt, Decl(main.ts, 1, 8))

View File

@ -0,0 +1,53 @@
//// [tests/cases/compiler/declarationEmitPreserveReferencedImports.ts] ////
=== utils.ts ===
export interface Evt { }
=== decl.ts ===
import {Evt} from './utils'
>Evt : any
> : ^^^
export const o = <T>(o: T) => () : T => null!
>o : <T>(o: T) => () => T
> : ^ ^^ ^^ ^^^^^^^^^^^
><T>(o: T) => () : T => null! : <T>(o: T) => () => T
> : ^ ^^ ^^ ^^^^^^^^^^^
>o : T
> : ^
>() : T => null! : () => T
> : ^^^^^^
>null! : never
> : ^^^^^
=== main.ts ===
import { o } from './decl'
>o : <T>(o: T) => () => T
> : ^ ^^ ^^ ^^^^^^^^^^^
import { Evt } from './utils'
>Evt : any
> : ^^^
export const f = { o: o({ v: null! as Evt}) };
>f : { o: () => { v: Evt; }; }
> : ^^^^^^^^^^^^^^^^ ^^^^^^
>{ o: o({ v: null! as Evt}) } : { o: () => { v: Evt; }; }
> : ^^^^^^^^^^^^^^^^ ^^^^^^
>o : () => { v: Evt; }
> : ^^^^^^^^^^^ ^^^
>o({ v: null! as Evt}) : () => { v: Evt; }
> : ^^^^^^^^^^^ ^^^
>o : <T>(o: T) => () => T
> : ^ ^^ ^^ ^^^^^^^^^^^
>{ v: null! as Evt} : { v: Evt; }
> : ^^^^^ ^^^
>v : Evt
> : ^^^
>null! as Evt : Evt
> : ^^^
>null! : never
> : ^^^^^

View File

@ -0,0 +1,16 @@
// @declaration: true
// @strict: true
// @filename: utils.ts
export interface Evt { }
// @filename: decl.ts
import {Evt} from './utils'
export const o = <T>(o: T) => () : T => null!
// @filename: main.ts
import { o } from './decl'
import { Evt } from './utils'
export const f = { o: o({ v: null! as Evt}) };