Skip to content

Commit

Permalink
🤖 Pick PR #58750 (Preserve elided imports used in dec...) into releas…
Browse files Browse the repository at this point in the history
…e-5.5 (#58757)

Co-authored-by: Titian Cernicova-Dragomir <[email protected]>
  • Loading branch information
TypeScript Bot and dragomirtitian authored Jun 3, 2024
1 parent 4ee5d05 commit 35d2952
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -8582,15 +8579,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {

function finalizeBoundary() {
context.tracker = oldTracker;
const newTrackedSymbols = context.trackedSymbols;
context.trackedSymbols = oldTrackedSymbols;
context.encounteredError = oldEncounteredError;

unreportedErrors?.forEach(fn => fn());
if (hadError) {
return false;
}
newTrackedSymbols?.forEach(
trackedSymbols?.forEach(
([symbol, enclosingDeclaration, meaning]) =>
context.tracker.trackSymbol(
symbol,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
};
};
Original file line number Diff line number Diff line change
@@ -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))

Original file line number Diff line number Diff line change
@@ -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
> : ^^^^^

16 changes: 16 additions & 0 deletions tests/cases/compiler/declarationEmitPreserveReferencedImports.ts
Original file line number Diff line number Diff line change
@@ -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}) };

0 comments on commit 35d2952

Please sign in to comment.