Skip to content

Commit bd0c03d

Browse files
committed
Index re-exporting modules for declaration emit
When a file being emitted imports no module that exports a symbol, getAlternativeContainingModules scanned the exports of every external module in the program, once per symbol. Build one index per checker from a resolved export target to the modules exporting it, and answer the lookup from that. Fixes microsoft#64464
1 parent de61e69 commit bd0c03d

6 files changed

Lines changed: 460 additions & 10 deletions

File tree

‎tsc/internal/checker/checker.go‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ type Checker struct {
697697
ReverseMappedSymbolLinks core.LinkStore[*ast.Symbol, ReverseMappedSymbolLinks]
698698
markedAssignmentSymbolLinks core.LinkStore[*ast.Symbol, MarkedAssignmentSymbolLinks]
699699
symbolContainerLinks core.LinkStore[*ast.Symbol, ContainingSymbolLinks]
700+
externalModuleContainers *externalModuleContainerIndex
700701
sourceFileLinks core.LinkStore[*ast.SourceFile, SourceFileLinks]
701702
regExpScanner *scanner.Scanner
702703
patternForType map[*Type]*ast.Node
@@ -14634,8 +14635,12 @@ func (c *Checker) recordMergedSymbol(target *ast.Symbol, source *ast.Symbol) {
1463414635
c.mergedSymbols[source] = target
1463514636
}
1463614637

14638+
func (c *Checker) getResolvedTarget(symbol *ast.Symbol) *ast.Symbol {
14639+
return c.getMergedSymbol(c.resolveSymbol(c.getMergedSymbol(symbol)))
14640+
}
14641+
1463714642
func (c *Checker) getSymbolIfSameReference(s1 *ast.Symbol, s2 *ast.Symbol) *ast.Symbol {
14638-
if c.getMergedSymbol(c.resolveSymbol(c.getMergedSymbol(s1))) == c.getMergedSymbol(c.resolveSymbol(c.getMergedSymbol(s2))) {
14643+
if c.getResolvedTarget(s1) == c.getResolvedTarget(s2) {
1463914644
return s1
1464014645
}
1464114646
return nil

‎tsc/internal/checker/symbolaccessibility.go‎

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package checker
22

33
import (
4+
"cmp"
45
"slices"
56

67
"github.com/microsoft/TypeScript/tsc/internal/ast"
@@ -207,21 +208,82 @@ func (c *Checker) getAlternativeContainingModules(symbol *ast.Symbol, enclosingD
207208
if links.extendedContainers != nil {
208209
return *links.extendedContainers
209210
}
210-
// No results from files already being imported by this file - expand search (expensive, but not location-specific, so cached)
211-
otherFiles := c.program.SourceFiles()
212-
for _, file := range otherFiles {
211+
// No results from files already being imported by this file - expand search (not location-specific, so cached)
212+
results = c.getExternalModuleContainers(symbol)
213+
links.extendedContainers = &results
214+
return results
215+
}
216+
217+
type externalModuleContainerIndex struct {
218+
complete bool
219+
containersByTarget map[*ast.Symbol][]*ast.Symbol
220+
moduleOrder map[*ast.Symbol]int
221+
}
222+
223+
func (index *externalModuleContainerIndex) add(target *ast.Symbol, container *ast.Symbol) {
224+
// Modules are indexed one at a time, so a repeat of this container is always the last entry.
225+
if existing := index.containersByTarget[target]; len(existing) == 0 || existing[len(existing)-1] != container {
226+
index.containersByTarget[target] = append(existing, container)
227+
}
228+
}
229+
230+
func (c *Checker) getExternalModuleContainers(symbol *ast.Symbol) []*ast.Symbol {
231+
if c.externalModuleContainers == nil {
232+
c.buildExternalModuleContainerIndex()
233+
}
234+
index := c.externalModuleContainers
235+
if !index.complete {
236+
// Re-entered from an alias resolved while building the index; answer this query without it.
237+
return c.scanExternalModuleContainers(symbol)
238+
}
239+
containers := index.containersByTarget[c.getResolvedTarget(symbol)]
240+
parent := c.getParentOfSymbol(symbol)
241+
parentOrder, parentIsModule := index.moduleOrder[parent]
242+
if !parentIsModule {
243+
return containers
244+
}
245+
// The parent module contains the symbol even when the symbol is absent from its exports.
246+
if at, found := slices.BinarySearchFunc(containers, parentOrder, func(container *ast.Symbol, order int) int {
247+
return cmp.Compare(index.moduleOrder[container], order)
248+
}); !found {
249+
return slices.Insert(slices.Clone(containers), at, parent)
250+
}
251+
return containers
252+
}
253+
254+
func (c *Checker) buildExternalModuleContainerIndex() {
255+
index := &externalModuleContainerIndex{
256+
containersByTarget: make(map[*ast.Symbol][]*ast.Symbol),
257+
moduleOrder: make(map[*ast.Symbol]int, len(c.program.SourceFiles())),
258+
}
259+
c.externalModuleContainers = index
260+
for _, file := range c.program.SourceFiles() {
213261
if !ast.IsExternalModule(file) {
214262
continue
215263
}
216-
sym := c.getSymbolOfDeclaration(file.AsNode())
217-
ref := c.getAliasForSymbolInContainer(sym, symbol)
218-
if ref == nil {
264+
container := c.getSymbolOfDeclaration(file.AsNode())
265+
index.moduleOrder[container] = len(index.moduleOrder)
266+
for _, exported := range c.getExportsOfSymbol(container) {
267+
index.add(c.getResolvedTarget(exported), container)
268+
}
269+
if exportEquals := container.Exports[ast.InternalSymbolNameExportEquals]; exportEquals != nil {
270+
index.add(c.getResolvedTarget(exportEquals), container)
271+
}
272+
}
273+
index.complete = true
274+
}
275+
276+
func (c *Checker) scanExternalModuleContainers(symbol *ast.Symbol) []*ast.Symbol {
277+
var containers []*ast.Symbol
278+
for _, file := range c.program.SourceFiles() {
279+
if !ast.IsExternalModule(file) {
219280
continue
220281
}
221-
results = append(results, sym)
282+
if container := c.getSymbolOfDeclaration(file.AsNode()); c.getAliasForSymbolInContainer(container, symbol) != nil {
283+
containers = append(containers, container)
284+
}
222285
}
223-
links.extendedContainers = &results
224-
return results
286+
return containers
225287
}
226288

227289
func (c *Checker) getVariableDeclarationOfObjectLiteral(symbol *ast.Symbol, meaning ast.SymbolFlags) *ast.Symbol {
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//// [tests/cases/compiler/declarationEmitAlternativeContainingModules.ts] ////
2+
3+
//// [model.ts]
4+
export interface Model { value: string; }
5+
export function make(): Model { return { value: "" }; }
6+
7+
//// [factory.ts]
8+
export { make } from "./model";
9+
10+
//// [aliases.ts]
11+
export { Model as First, Model as Second } from "./model";
12+
13+
//// [preferred.ts]
14+
export { Model, Model as Renamed } from "./model";
15+
16+
//// [star.ts]
17+
export * from "./aliases";
18+
19+
//// [direct.ts]
20+
import { make } from "./factory";
21+
export const direct = make();
22+
export const repeated = make();
23+
24+
//// [renamed.ts]
25+
import { make } from "./factory";
26+
import * as aliases from "./aliases";
27+
export const renamed = make();
28+
29+
//// [sameName.ts]
30+
import { make } from "./factory";
31+
import * as preferred from "./preferred";
32+
export const sameName = make();
33+
34+
//// [starred.ts]
35+
import { make } from "./factory";
36+
import * as star from "./star";
37+
export const starred = make();
38+
39+
//// [exported.ts]
40+
import { Model } from "./model";
41+
export = Model;
42+
43+
//// [exportEquals.ts]
44+
import { make } from "./factory";
45+
import Model = require("./exported");
46+
export const exportEquals = make();
47+
48+
//// [index.ts]
49+
export { direct, repeated } from "./direct";
50+
export { renamed } from "./renamed";
51+
export { sameName } from "./sameName";
52+
export { starred } from "./starred";
53+
export { exportEquals } from "./exportEquals";
54+
55+
56+
57+
58+
//// [model.d.ts]
59+
export interface Model {
60+
value: string;
61+
}
62+
export declare function make(): Model;
63+
//// [factory.d.ts]
64+
export { make } from "./model";
65+
//// [aliases.d.ts]
66+
export { Model as First, Model as Second } from "./model";
67+
//// [preferred.d.ts]
68+
export { Model, Model as Renamed } from "./model";
69+
//// [star.d.ts]
70+
export * from "./aliases";
71+
//// [direct.d.ts]
72+
export declare const direct: import("./model").Model;
73+
export declare const repeated: import("./model").Model;
74+
//// [renamed.d.ts]
75+
import * as aliases from "./aliases";
76+
export declare const renamed: aliases.First;
77+
//// [sameName.d.ts]
78+
import * as preferred from "./preferred";
79+
export declare const sameName: preferred.Model;
80+
//// [starred.d.ts]
81+
import * as star from "./star";
82+
export declare const starred: star.First;
83+
//// [exported.d.ts]
84+
import { Model } from "./model";
85+
export = Model;
86+
//// [exportEquals.d.ts]
87+
import Model = require("./exported");
88+
export declare const exportEquals: Model;
89+
//// [index.d.ts]
90+
export { direct, repeated } from "./direct";
91+
export { renamed } from "./renamed";
92+
export { sameName } from "./sameName";
93+
export { starred } from "./starred";
94+
export { exportEquals } from "./exportEquals";
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
//// [tests/cases/compiler/declarationEmitAlternativeContainingModules.ts] ////
2+
3+
=== model.ts ===
4+
export interface Model { value: string; }
5+
>Model : Symbol(Model, Decl(model.ts, 0, 0))
6+
>value : Symbol(Model.value, Decl(model.ts, 0, 24))
7+
8+
export function make(): Model { return { value: "" }; }
9+
>make : Symbol(make, Decl(model.ts, 0, 41))
10+
>Model : Symbol(Model, Decl(model.ts, 0, 0))
11+
>value : Symbol(value, Decl(model.ts, 1, 40))
12+
13+
=== factory.ts ===
14+
export { make } from "./model";
15+
>make : Symbol(make, Decl(factory.ts, 0, 8))
16+
17+
=== aliases.ts ===
18+
export { Model as First, Model as Second } from "./model";
19+
>Model : Symbol(Model, Decl(model.ts, 0, 0))
20+
>First : Symbol(First, Decl(aliases.ts, 0, 8))
21+
>Model : Symbol(Model, Decl(model.ts, 0, 0))
22+
>Second : Symbol(Second, Decl(aliases.ts, 0, 24))
23+
24+
=== preferred.ts ===
25+
export { Model, Model as Renamed } from "./model";
26+
>Model : Symbol(Model, Decl(preferred.ts, 0, 8))
27+
>Model : Symbol(Model, Decl(model.ts, 0, 0))
28+
>Renamed : Symbol(Renamed, Decl(preferred.ts, 0, 15))
29+
30+
=== star.ts ===
31+
32+
export * from "./aliases";
33+
34+
=== direct.ts ===
35+
import { make } from "./factory";
36+
>make : Symbol(make, Decl(direct.ts, 0, 8))
37+
38+
export const direct = make();
39+
>direct : Symbol(direct, Decl(direct.ts, 1, 12))
40+
>make : Symbol(make, Decl(direct.ts, 0, 8))
41+
42+
export const repeated = make();
43+
>repeated : Symbol(repeated, Decl(direct.ts, 2, 12))
44+
>make : Symbol(make, Decl(direct.ts, 0, 8))
45+
46+
=== renamed.ts ===
47+
import { make } from "./factory";
48+
>make : Symbol(make, Decl(renamed.ts, 0, 8))
49+
50+
import * as aliases from "./aliases";
51+
>aliases : Symbol(aliases, Decl(renamed.ts, 1, 6))
52+
53+
export const renamed = make();
54+
>renamed : Symbol(renamed, Decl(renamed.ts, 2, 12))
55+
>make : Symbol(make, Decl(renamed.ts, 0, 8))
56+
57+
=== sameName.ts ===
58+
import { make } from "./factory";
59+
>make : Symbol(make, Decl(sameName.ts, 0, 8))
60+
61+
import * as preferred from "./preferred";
62+
>preferred : Symbol(preferred, Decl(sameName.ts, 1, 6))
63+
64+
export const sameName = make();
65+
>sameName : Symbol(sameName, Decl(sameName.ts, 2, 12))
66+
>make : Symbol(make, Decl(sameName.ts, 0, 8))
67+
68+
=== starred.ts ===
69+
import { make } from "./factory";
70+
>make : Symbol(make, Decl(starred.ts, 0, 8))
71+
72+
import * as star from "./star";
73+
>star : Symbol(star, Decl(starred.ts, 1, 6))
74+
75+
export const starred = make();
76+
>starred : Symbol(starred, Decl(starred.ts, 2, 12))
77+
>make : Symbol(make, Decl(starred.ts, 0, 8))
78+
79+
=== exported.ts ===
80+
import { Model } from "./model";
81+
>Model : Symbol(Model, Decl(exported.ts, 0, 8))
82+
83+
export = Model;
84+
>Model : Symbol(Model, Decl(exported.ts, 0, 8))
85+
86+
=== exportEquals.ts ===
87+
import { make } from "./factory";
88+
>make : Symbol(make, Decl(exportEquals.ts, 0, 8))
89+
90+
import Model = require("./exported");
91+
>Model : Symbol(Model, Decl(exportEquals.ts, 0, 33))
92+
93+
export const exportEquals = make();
94+
>exportEquals : Symbol(exportEquals, Decl(exportEquals.ts, 2, 12))
95+
>make : Symbol(make, Decl(exportEquals.ts, 0, 8))
96+
97+
=== index.ts ===
98+
export { direct, repeated } from "./direct";
99+
>direct : Symbol(direct, Decl(index.ts, 0, 8))
100+
>repeated : Symbol(repeated, Decl(index.ts, 0, 16))
101+
102+
export { renamed } from "./renamed";
103+
>renamed : Symbol(renamed, Decl(index.ts, 1, 8))
104+
105+
export { sameName } from "./sameName";
106+
>sameName : Symbol(sameName, Decl(index.ts, 2, 8))
107+
108+
export { starred } from "./starred";
109+
>starred : Symbol(starred, Decl(index.ts, 3, 8))
110+
111+
export { exportEquals } from "./exportEquals";
112+
>exportEquals : Symbol(exportEquals, Decl(index.ts, 4, 8))
113+

0 commit comments

Comments
 (0)