Skip to content

Commit 0774fdc

Browse files
committed
Expose visible to outside import declarations and dynamic imports through emitDts output
1 parent d93e0d4 commit 0774fdc

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3908,6 +3908,7 @@ namespace ts {
39083908
const typeParameterNodes = overrideTypeArguments || lookupTypeParameterNodes(chain, 0, context);
39093909
const specifier = getSpecifierForModuleSymbol(chain[0], context);
39103910
const lit = createLiteralTypeNode(createLiteral(specifier));
3911+
if (context.tracker.trackExternalModuleSymbolOfImportTypeNode) context.tracker.trackExternalModuleSymbolOfImportTypeNode(chain[0]);
39113912
context.approximateLength += specifier.length + 10; // specifier + import("")
39123913
if (!nonRootParts || isEntityName(nonRootParts)) {
39133914
if (nonRootParts) {

src/compiler/emitter.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ namespace ts {
113113

114114
let bundleInfo: BundleInfo = createDefaultBundleInfo();
115115
let emitSkipped = false;
116+
let exportedModulesFromDeclarationEmit: ExportedModulesFromDeclarationEmit | undefined;
116117

117118
// Emit each output file
118119
performance.mark("beforePrint");
@@ -125,6 +126,7 @@ namespace ts {
125126
diagnostics: emitterDiagnostics.getDiagnostics(),
126127
emittedFiles: emittedFilesList,
127128
sourceMaps: sourceMapDataList,
129+
exportedModulesFromDeclarationEmit
128130
};
129131

130132
function emitSourceFileOrBundle({ jsFilePath, sourceMapFilePath, declarationFilePath, declarationMapPath, bundleInfoPath }: EmitFileNames, sourceFileOrBundle: SourceFile | Bundle) {
@@ -222,6 +224,11 @@ namespace ts {
222224
if (!declBlocked || emitOnlyDtsFiles) {
223225
Debug.assert(declarationTransform.transformed.length === 1, "Should only see one output from the decl transform");
224226
printSourceFileOrBundle(declarationFilePath, declarationMapPath, declarationTransform.transformed[0], /* bundleInfopath*/ undefined, declarationPrinter, declarationSourceMap);
227+
if (emitOnlyDtsFiles && declarationTransform.transformed[0].kind === SyntaxKind.SourceFile) {
228+
const sourceFile = declarationTransform.transformed[0] as SourceFile;
229+
exportedModulesFromDeclarationEmit = sourceFile.getExportedModulesFromDeclarationEmit &&
230+
sourceFile.getExportedModulesFromDeclarationEmit();
231+
}
225232
}
226233
declarationTransform.dispose();
227234
}

src/compiler/transformers/declarations.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ namespace ts {
3737
let lateMarkedStatements: LateVisibilityPaintedStatement[] | undefined;
3838
let lateStatementReplacementMap: Map<VisitResult<LateVisibilityPaintedStatement>>;
3939
let suppressNewDiagnosticContexts: boolean;
40+
let exportedModuleSpecifiers: StringLiteralLike[] | undefined;
41+
let exportedModuleSymbolsUsingImportTypeNodes: Symbol[] | undefined;
4042

4143
const host = context.getEmitHost();
4244
const symbolTracker: SymbolTracker = {
@@ -46,6 +48,7 @@ namespace ts {
4648
reportPrivateInBaseOfClassExpression,
4749
moduleResolverHost: host,
4850
trackReferencedAmbientModule,
51+
trackExternalModuleSymbolOfImportTypeNode
4952
};
5053
let errorNameNode: DeclarationName | undefined;
5154

@@ -115,6 +118,12 @@ namespace ts {
115118
}
116119
}
117120

121+
function trackExternalModuleSymbolOfImportTypeNode(symbol: Symbol) {
122+
if (!isBundledEmit) {
123+
(exportedModuleSymbolsUsingImportTypeNodes || (exportedModuleSymbolsUsingImportTypeNodes = [])).push(symbol);
124+
}
125+
}
126+
118127
function trackSymbol(symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags) {
119128
if (symbol.flags & SymbolFlags.TypeParameter) return;
120129
handleSymbolAccessibilityError(resolver.isSymbolAccessible(symbol, enclosingDeclaration, meaning, /*shouldComputeAliasesToMakeVisible*/ true));
@@ -224,6 +233,12 @@ namespace ts {
224233
combinedStatements = setTextRange(createNodeArray([...combinedStatements, createExportDeclaration(/*decorators*/ undefined, /*modifiers*/ undefined, createNamedExports([]), /*moduleSpecifier*/ undefined)]), combinedStatements);
225234
}
226235
const updated = updateSourceFileNode(node, combinedStatements, /*isDeclarationFile*/ true, references, getFileReferencesForUsedTypeReferences(), node.hasNoDefaultLib);
236+
if (exportedModuleSpecifiers || exportedModuleSymbolsUsingImportTypeNodes) {
237+
updated.getExportedModulesFromDeclarationEmit = () => ({
238+
exportedModuleSpecifiers: exportedModuleSpecifiers || emptyArray,
239+
exportedModuleSymbolsUsingImportTypeNodes: exportedModuleSymbolsUsingImportTypeNodes || emptyArray
240+
});
241+
}
227242
return updated;
228243

229244
function getFileReferencesForUsedTypeReferences() {
@@ -483,10 +498,15 @@ namespace ts {
483498
function rewriteModuleSpecifier<T extends Node>(parent: ImportEqualsDeclaration | ImportDeclaration | ExportDeclaration | ModuleDeclaration | ImportTypeNode, input: T | undefined): T | StringLiteral {
484499
if (!input) return undefined!; // TODO: GH#18217
485500
resultHasExternalModuleIndicator = resultHasExternalModuleIndicator || (parent.kind !== SyntaxKind.ModuleDeclaration && parent.kind !== SyntaxKind.ImportType);
486-
if (input.kind === SyntaxKind.StringLiteral && isBundledEmit) {
487-
const newName = getExternalModuleNameFromDeclaration(context.getEmitHost(), resolver, parent);
488-
if (newName) {
489-
return createLiteral(newName);
501+
if (isStringLiteralLike(input)) {
502+
if (isBundledEmit) {
503+
const newName = getExternalModuleNameFromDeclaration(context.getEmitHost(), resolver, parent);
504+
if (newName) {
505+
return createLiteral(newName);
506+
}
507+
}
508+
else {
509+
(exportedModuleSpecifiers || (exportedModuleSpecifiers = [])).push(input);
490510
}
491511
}
492512
return input;

src/compiler/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2624,6 +2624,14 @@ namespace ts {
26242624
/* @internal */ pragmas: PragmaMap;
26252625
/* @internal */ localJsxNamespace?: __String;
26262626
/* @internal */ localJsxFactory?: EntityName;
2627+
2628+
/*@internal*/ getExportedModulesFromDeclarationEmit?(): ExportedModulesFromDeclarationEmit;
2629+
}
2630+
2631+
/*@internal*/
2632+
export interface ExportedModulesFromDeclarationEmit {
2633+
exportedModuleSpecifiers: ReadonlyArray<StringLiteralLike>;
2634+
exportedModuleSymbolsUsingImportTypeNodes: ReadonlyArray<Symbol>;
26272635
}
26282636

26292637
export interface Bundle extends Node {
@@ -2866,6 +2874,7 @@ namespace ts {
28662874
diagnostics: ReadonlyArray<Diagnostic>;
28672875
emittedFiles?: string[]; // Array of files the compiler wrote to disk
28682876
/* @internal */ sourceMaps?: SourceMapData[]; // Array of sourceMapData if compiler emitted sourcemaps
2877+
/* @internal */ exportedModulesFromDeclarationEmit?: ExportedModulesFromDeclarationEmit;
28692878
}
28702879

28712880
/* @internal */
@@ -5324,6 +5333,7 @@ namespace ts {
53245333
reportInaccessibleUniqueSymbolError?(): void;
53255334
moduleResolverHost?: ModuleSpecifierResolutionHost;
53265335
trackReferencedAmbientModule?(decl: ModuleDeclaration, symbol: Symbol): void;
5336+
trackExternalModuleSymbolOfImportTypeNode?(symbol: Symbol): void;
53275337
}
53285338

53295339
export interface TextSpan {

0 commit comments

Comments
 (0)