-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
bundle-generator.ts
847 lines (709 loc) · 31.8 KB
/
bundle-generator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
import * as ts from 'typescript';
import { compileDts } from './compile-dts';
import { TypesUsageEvaluator } from './types-usage-evaluator';
import {
ExportType,
getActualSymbol,
getClosestModuleLikeNode,
getClosestSourceFileLikeNode,
getDeclarationsForSymbol,
getExportsForSourceFile,
getExportsForStatement,
getImportModuleName,
getNodeName,
getNodeSymbol,
getRootSourceFile,
hasNodeModifier,
isAmbientModule,
isDeclareGlobalStatement,
isDeclareModule,
isNodeNamedDeclaration,
SourceFileExport,
splitTransientSymbol,
} from './helpers/typescript';
import {
getFileModuleInfo,
getModuleLikeModuleInfo,
getReferencedModuleInfo,
ModuleCriteria,
ModuleInfo,
ModuleType,
} from './module-info';
import { generateOutput, ModuleImportsSet, OutputParams } from './generate-output';
import {
normalLog,
verboseLog,
} from './logger';
import { CollisionsResolver } from './collisions-resolver';
export interface CompilationOptions {
/**
* EXPERIMENTAL!
* Allows disable resolving of symlinks to the original path.
* By default following is enabled.
* @see https://github.com/timocov/dts-bundle-generator/issues/39
*/
followSymlinks?: boolean;
/**
* Path to the tsconfig file that will be used for the compilation.
*/
preferredConfigPath?: string;
}
export interface OutputOptions {
/**
* Sort output nodes in ascendant order.
*/
sortNodes?: boolean;
/**
* Name of the UMD module.
* If specified then `export as namespace ModuleName;` will be emitted.
*/
umdModuleName?: string;
/**
* Enables inlining of `declare global` statements contained in files which should be inlined (all local files and packages from inlined libraries).
*/
inlineDeclareGlobals?: boolean;
/**
* Enables inlining of `declare module` statements of the global modules
* (e.g. `declare module 'external-module' {}`, but NOT `declare module './internal-module' {}`)
* contained in files which should be inlined (all local files and packages from inlined libraries)
*/
inlineDeclareExternals?: boolean;
/**
* Allows remove "Generated by dts-bundle-generator" comment from the output
*/
noBanner?: boolean;
/**
* Enables stripping the `const` keyword from every direct-exported (or re-exported) from entry file `const enum`.
* This allows you "avoid" the issue described in https://github.com/microsoft/TypeScript/issues/37774.
*/
respectPreserveConstEnum?: boolean;
/**
* By default all interfaces, types and const enums are marked as exported even if they aren't exported directly.
* This option allows you to disable this behavior so a node will be exported if it is exported from root source file only.
*/
exportReferencedTypes?: boolean;
}
export interface LibrariesOptions {
/**
* Array of package names from node_modules to inline typings from.
* Used types will be inlined into the output file.
*/
inlinedLibraries?: string[];
/**
* Array of package names from node_modules to import typings from.
* Used types will be imported using `import { First, Second } from 'library-name';`.
* By default all libraries will be imported (except inlined libraries and libraries from @types).
*/
importedLibraries?: string[];
/**
* Array of package names from @types to import typings from via the triple-slash reference directive.
* By default all packages are allowed and will be used according to their usages.
*/
allowedTypesLibraries?: string[];
}
export interface EntryPointConfig {
/**
* Path to input file.
*/
filePath: string;
libraries?: LibrariesOptions;
/**
* Fail if generated dts contains class declaration.
*/
failOnClass?: boolean;
output?: OutputOptions;
}
export function generateDtsBundle(entries: readonly EntryPointConfig[], options: CompilationOptions = {}): string[] {
normalLog('Compiling input files...');
const { program, rootFilesRemapping } = compileDts(entries.map((entry: EntryPointConfig) => entry.filePath), options.preferredConfigPath, options.followSymlinks);
const typeChecker = program.getTypeChecker();
const typeRoots = ts.getEffectiveTypeRoots(program.getCompilerOptions(), {});
const sourceFiles = program.getSourceFiles().filter((file: ts.SourceFile) => {
return !program.isSourceFileDefaultLibrary(file);
});
verboseLog(`Input source files:\n ${sourceFiles.map((file: ts.SourceFile) => file.fileName).join('\n ')}`);
const typesUsageEvaluator = new TypesUsageEvaluator(sourceFiles, typeChecker);
return entries.map((entryConfig: EntryPointConfig) => {
normalLog(`Processing ${entryConfig.filePath}`);
const newRootFilePath = rootFilesRemapping.get(entryConfig.filePath);
if (newRootFilePath === undefined) {
throw new Error(`Cannot remap root source file ${entryConfig.filePath}`);
}
const rootSourceFile = getRootSourceFile(program, newRootFilePath);
const rootSourceFileSymbol = typeChecker.getSymbolAtLocation(rootSourceFile);
if (rootSourceFileSymbol === undefined) {
throw new Error(`Symbol for root source file ${newRootFilePath} not found`);
}
const librariesOptions: LibrariesOptions = entryConfig.libraries || {};
const criteria: ModuleCriteria = {
allowedTypesLibraries: librariesOptions.allowedTypesLibraries,
importedLibraries: librariesOptions.importedLibraries,
inlinedLibraries: librariesOptions.inlinedLibraries || [],
typeRoots,
};
const rootFileExports = getExportsForSourceFile(typeChecker, rootSourceFileSymbol);
const rootFileExportSymbols = rootFileExports.map((exp: SourceFileExport) => exp.symbol);
interface CollectingResult {
typesReferences: Set<string>;
imports: Map<string, ModuleImportsSet>;
statements: ts.Statement[];
renamedExports: OutputParams['renamedExports'];
}
const collectionResult: CollectingResult = {
typesReferences: new Set(),
imports: new Map(),
statements: [],
renamedExports: new Map(),
};
const outputOptions: OutputOptions = entryConfig.output || {};
const inlineDeclareGlobals = Boolean(outputOptions.inlineDeclareGlobals);
const collisionsResolver = new CollisionsResolver(typeChecker);
function updateResultForAnyModule(statements: readonly ts.Statement[], currentModule: ModuleInfo): void {
// contains a set of modules that were visited already
// can be used to prevent infinite recursion in updating results in re-exports
const visitedModules = new Set<string>();
function updateResultForExternalExport(exportAssignment: ts.ExportAssignment | ts.ExportDeclaration): void {
// if we have `export =` or `export * from` somewhere so we can decide that every declaration of exported symbol in this way
// is "part of the exported module" and we need to update result according every member of each declaration
// but treat they as current module (we do not need to update module info)
for (const declaration of getDeclarationsForExportedValues(exportAssignment)) {
let exportedDeclarations: readonly ts.Statement[] = [];
if (ts.isExportDeclaration(exportAssignment) && ts.isSourceFile(declaration)) {
const referencedModule = getReferencedModuleInfo(exportAssignment, criteria, typeChecker);
if (referencedModule !== null) {
if (visitedModules.has(referencedModule.fileName)) {
continue;
}
visitedModules.add(referencedModule.fileName);
}
exportedDeclarations = declaration.statements;
} else if (ts.isModuleDeclaration(declaration)) {
if (declaration.body !== undefined && ts.isModuleBlock(declaration.body)) {
const referencedModule = getReferencedModuleInfo(declaration, criteria, typeChecker);
if (referencedModule !== null) {
if (visitedModules.has(referencedModule.fileName)) {
continue;
}
visitedModules.add(referencedModule.fileName);
}
exportedDeclarations = declaration.body.statements;
}
} else {
exportedDeclarations = [declaration as unknown as ts.Statement];
}
updateResultImpl(exportedDeclarations);
}
}
// eslint-disable-next-line complexity
function updateResultImpl(statementsToProcess: readonly ts.Statement[]): void {
for (const statement of statementsToProcess) {
// we should skip import statements
if (statement.kind === ts.SyntaxKind.ImportDeclaration || statement.kind === ts.SyntaxKind.ImportEqualsDeclaration) {
continue;
}
if (isDeclareModule(statement)) {
updateResultForModuleDeclaration(statement, currentModule);
// if a statement is `declare module "module" {}` then don't process it below
// as it is handled already in `updateResultForModuleDeclaration`
// but if it is `declare module Module {}` then it can be used in types and imports
// so in this case it needs to be checked for "usages" below
if (ts.isStringLiteral(statement.name)) {
continue;
}
}
if (currentModule.type === ModuleType.ShouldBeUsedForModulesOnly) {
continue;
}
if (isDeclareGlobalStatement(statement) && inlineDeclareGlobals && currentModule.type === ModuleType.ShouldBeInlined) {
collectionResult.statements.push(statement);
continue;
}
if (ts.isExportDeclaration(statement)) {
if (currentModule.type === ModuleType.ShouldBeInlined) {
continue;
}
// `export * from`
if (statement.exportClause === undefined) {
updateResultForExternalExport(statement);
continue;
}
// `export { val }`
if (ts.isNamedExports(statement.exportClause) && currentModule.type === ModuleType.ShouldBeImported) {
updateImportsForStatement(statement);
continue;
}
}
if (ts.isExportAssignment(statement) && statement.isExportEquals && currentModule.isExternal) {
updateResultForExternalExport(statement);
continue;
}
if (!isNodeUsed(statement)) {
continue;
}
switch (currentModule.type) {
case ModuleType.ShouldBeReferencedAsTypes:
addTypesReference(currentModule.typesLibraryName);
break;
case ModuleType.ShouldBeImported:
updateImportsForStatement(statement);
break;
case ModuleType.ShouldBeInlined:
if (ts.isVariableStatement(statement)) {
for (const variableDeclaration of statement.declarationList.declarations) {
if (ts.isIdentifier(variableDeclaration.name)) {
collisionsResolver.addTopLevelIdentifier(variableDeclaration.name);
}
// it seems that the compiler doesn't produce anything else (e.g. binding elements) in declaration files
// but it is still possible to write such code manually
// this feels like quite rare case so no support for now
}
} else if (isNodeNamedDeclaration(statement)) {
const statementName = getNodeName(statement);
if (statementName !== undefined) {
collisionsResolver.addTopLevelIdentifier(statementName as ts.Identifier | ts.DefaultKeyword);
}
}
collectionResult.statements.push(statement);
break;
}
}
}
updateResultImpl(statements);
}
function updateResultForRootModule(statements: readonly ts.Statement[], currentModule: ModuleInfo): void {
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
function isReExportFromImportableModule(statement: ts.ExportDeclaration): boolean {
return getReferencedModuleInfo(statement, criteria, typeChecker)?.type === ModuleType.ShouldBeImported;
}
updateResultForAnyModule(statements, currentModule);
// add skipped by `updateResult` exports
for (const statement of statements) {
// `export * from 'importable-package'`
if (ts.isExportDeclaration(statement) && isReExportFromImportableModule(statement) && statement.exportClause === undefined) {
collectionResult.statements.push(statement);
continue;
}
if (ts.isExportAssignment(statement)) {
// `"export ="` or `export default 123` or `export default "str"`
if (statement.isExportEquals || !ts.isIdentifier(statement.expression)) {
collectionResult.statements.push(statement);
}
continue;
}
}
}
function updateResultForModuleDeclaration(moduleDecl: ts.ModuleDeclaration, currentModule: ModuleInfo): void {
if (moduleDecl.body === undefined || !ts.isModuleBlock(moduleDecl.body)) {
return;
}
const referencedModuleInfo = getReferencedModuleInfo(moduleDecl, criteria, typeChecker);
if (referencedModuleInfo === null) {
return;
}
// if we have declaration of external module inside internal one
if (!currentModule.isExternal && referencedModuleInfo.isExternal) {
// if it's allowed - we need to just add it to result without any processing
if (outputOptions.inlineDeclareExternals) {
collectionResult.statements.push(moduleDecl);
}
return;
}
updateResultForAnyModule(moduleDecl.body.statements, referencedModuleInfo);
}
function addTypesReference(library: string): void {
if (!collectionResult.typesReferences.has(library)) {
normalLog(`Library "${library}" will be added via reference directive`);
collectionResult.typesReferences.add(library);
}
}
function updateImportsForStatement(statement: ts.Statement | ts.SourceFile | ts.ExportSpecifier): void {
const statementsToImport = ts.isVariableStatement(statement)
? statement.declarationList.declarations
: ts.isExportDeclaration(statement) && statement.exportClause !== undefined
? ts.isNamespaceExport(statement.exportClause)
? [statement.exportClause]
: statement.exportClause.elements
: [statement];
for (const statementToImport of statementsToImport) {
if (shouldNodeBeImported(statementToImport as ts.DeclarationStatement)) {
addImport(statementToImport as ts.DeclarationStatement);
// if we're going to add import of any statement in the bundle
// we should check whether the library of that statement
// could be referenced via triple-slash reference-types directive
// because the project which will use bundled declaration file
// can have `types: []` in the tsconfig and it'll fail
// this is especially related to the types packages
// which declares different modules in their declarations
// e.g. @types/node has declaration for "packages" events, fs, path and so on
const sourceFile = statementToImport.getSourceFile();
const moduleInfo = getFileModuleInfo(sourceFile.fileName, criteria);
if (moduleInfo.type === ModuleType.ShouldBeReferencedAsTypes) {
addTypesReference(moduleInfo.typesLibraryName);
}
}
}
}
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
function getDeclarationUsagesSourceFiles(declaration: ts.NamedDeclaration): Set<ts.SourceFile | ts.ModuleDeclaration> {
return new Set(
getExportedSymbolsUsingStatement(declaration)
.map((symbol: ts.Symbol) => getDeclarationsForSymbol(symbol))
.reduce((acc: ts.Declaration[], val: ts.Declaration[]) => acc.concat(val), [])
.map(getClosestModuleLikeNode)
);
}
function addImport(statement: ts.DeclarationStatement | ts.SourceFile): void {
if (!ts.isSourceFile(statement) && statement.name === undefined) {
throw new Error(`Import/usage unnamed declaration: ${statement.getText()}`);
}
getDeclarationUsagesSourceFiles(statement).forEach((sourceFile: ts.SourceFile | ts.ModuleDeclaration) => {
if (getModuleLikeModuleInfo(sourceFile, criteria, typeChecker).type !== ModuleType.ShouldBeInlined) {
// we should ignore source files that aren't inlined
return;
}
const sourceFileStatements = ts.isSourceFile(sourceFile)
? sourceFile.statements
: (sourceFile.body as ts.ModuleBlock).statements;
// eslint-disable-next-line complexity
sourceFileStatements.forEach((st: ts.Statement) => {
if (!ts.isImportEqualsDeclaration(st) && !ts.isImportDeclaration(st) && !ts.isExportDeclaration(st)) {
return;
}
const importModuleSpecifier = getImportModuleName(st);
if (importModuleSpecifier === null) {
return;
}
const referencedModuleInfo = getReferencedModuleInfo(st, criteria, typeChecker);
// if a referenced module should be inlined we can just ignore it
if (referencedModuleInfo === null || referencedModuleInfo.type !== ModuleType.ShouldBeImported) {
return;
}
let importItem = collectionResult.imports.get(importModuleSpecifier);
if (importItem === undefined) {
importItem = {
defaultImports: new Set(),
namedImports: new Map(),
starImport: null,
requireImports: new Set(),
};
collectionResult.imports.set(importModuleSpecifier, importItem);
}
if (ts.isImportEqualsDeclaration(st)) {
if (areDeclarationSame(statement, st)) {
importItem.requireImports.add(collisionsResolver.addTopLevelIdentifier(st.name));
}
return;
}
if (ts.isExportDeclaration(st)) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const exportClause = st.exportClause!;
if (ts.isNamedExports(exportClause)) {
// export { El1, El2 as ExportedName } from 'module';
exportClause.elements
.filter(areDeclarationSame.bind(null, statement))
.forEach((specifier: ts.ExportSpecifier) => {
const newLocalName = collisionsResolver.addTopLevelIdentifier(specifier.name);
const importedName = (specifier.propertyName || specifier.name).text;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
importItem!.namedImports.set(newLocalName, importedName);
});
} else {
// export * as name from 'module';
if (importItem.starImport === null && isNodeUsed(exportClause)) {
importItem.starImport = collisionsResolver.addTopLevelIdentifier(exportClause.name);
}
}
} else {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const importClause = st.importClause!;
if (importClause.name !== undefined && areDeclarationSame(statement, importClause)) {
// import name from 'module';
importItem.defaultImports.add(collisionsResolver.addTopLevelIdentifier(importClause.name));
}
if (importClause.namedBindings !== undefined) {
if (ts.isNamedImports(importClause.namedBindings)) {
// import { El1, El2 as ImportedName } from 'module';
importClause.namedBindings.elements
.filter(areDeclarationSame.bind(null, statement))
.forEach((specifier: ts.ImportSpecifier) => {
const newLocalName = collisionsResolver.addTopLevelIdentifier(specifier.name);
const importedName = (specifier.propertyName || specifier.name).text;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
importItem!.namedImports.set(newLocalName, importedName);
});
} else {
// import * as name from 'module';
if (importItem.starImport === null && isNodeUsed(importClause)) {
importItem.starImport = collisionsResolver.addTopLevelIdentifier(importClause.namedBindings.name);
}
}
}
}
});
});
}
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
function getGlobalSymbolsUsingSymbol(symbol: ts.Symbol): ts.Symbol[] {
return Array.from(typesUsageEvaluator.getSymbolsUsingSymbol(symbol) ?? []).filter((usedInSymbol: ts.Symbol) => {
if (usedInSymbol.escapedName !== ts.InternalSymbolName.Global) {
return false;
}
return getDeclarationsForSymbol(usedInSymbol).some((decl: ts.Declaration) => {
const closestModuleLike = getClosestSourceFileLikeNode(decl);
const moduleInfo = getModuleLikeModuleInfo(closestModuleLike, criteria, typeChecker);
return moduleInfo.type === ModuleType.ShouldBeInlined;
});
});
}
function isNodeUsed(node: ts.Node): boolean {
if (isNodeNamedDeclaration(node) || ts.isSourceFile(node)) {
const nodeSymbol = getNodeSymbol(node, typeChecker);
if (nodeSymbol === null) {
return false;
}
const nodeUsedByDirectExports = rootFileExportSymbols.some((rootExport: ts.Symbol) => typesUsageEvaluator.isSymbolUsedBySymbol(nodeSymbol, rootExport));
if (nodeUsedByDirectExports) {
return true;
}
return inlineDeclareGlobals && getGlobalSymbolsUsingSymbol(nodeSymbol).length !== 0;
} else if (ts.isVariableStatement(node)) {
return node.declarationList.declarations.some((declaration: ts.VariableDeclaration) => {
return isNodeUsed(declaration);
});
} else if (ts.isExportDeclaration(node) && node.exportClause !== undefined && ts.isNamespaceExport(node.exportClause)) {
return isNodeUsed(node.exportClause);
} else if (ts.isImportClause(node) && node.namedBindings !== undefined) {
return isNodeUsed(node.namedBindings);
}
return false;
}
function shouldNodeBeImported(node: ts.NamedDeclaration): boolean {
const nodeSymbol = getNodeSymbol(node, typeChecker);
if (nodeSymbol === null) {
return false;
}
return shouldSymbolBeImported(nodeSymbol);
}
function shouldSymbolBeImported(nodeSymbol: ts.Symbol): boolean {
const isSymbolDeclaredInDefaultLibrary = getDeclarationsForSymbol(nodeSymbol).some(
(declaration: ts.Declaration) => program.isSourceFileDefaultLibrary(declaration.getSourceFile())
);
if (isSymbolDeclaredInDefaultLibrary) {
// we shouldn't import a node declared in the default library (such dom, es2015)
// yeah, actually we should check that node is declared only in the default lib
// but it seems we can check that at least one declaration is from default lib
// to treat the node as un-importable
// because we can't re-export declared somewhere else node with declaration merging
// also, if some lib file will not be added to the project
// for example like it is described in the react declaration file (e.g. React Native)
// then here we still have a bug with "importing global declaration from a package"
// (see https://github.com/timocov/dts-bundle-generator/issues/71)
// but I don't think it is a big problem for now
// and it's possible that it will be fixed in https://github.com/timocov/dts-bundle-generator/issues/59
return false;
}
const symbolsDeclarations = getDeclarationsForSymbol(nodeSymbol);
// if all declarations of the symbol are in modules that should be inlined then this symbol must be inlined, not imported
const shouldSymbolBeInlined = symbolsDeclarations.every(
(decl: ts.Declaration) => getModuleLikeModuleInfo(
getClosestSourceFileLikeNode(decl),
criteria,
typeChecker
).type === ModuleType.ShouldBeInlined
);
if (shouldSymbolBeInlined) {
return false;
}
return getExportedSymbolsUsingSymbol(nodeSymbol).length !== 0;
}
function getExportedSymbolsUsingStatement(node: ts.NamedDeclaration): readonly ts.Symbol[] {
const nodeSymbol = getNodeSymbol(node, typeChecker);
if (nodeSymbol === null) {
return [];
}
return getExportedSymbolsUsingSymbol(nodeSymbol);
}
function getExportedSymbolsUsingSymbol(nodeSymbol: ts.Symbol): readonly ts.Symbol[] {
const symbolsUsingNode = typesUsageEvaluator.getSymbolsUsingSymbol(nodeSymbol);
if (symbolsUsingNode === null) {
throw new Error(`Something went wrong - getSymbolsUsingSymbol returned null but expected to be a set of symbols (symbol=${nodeSymbol.name})`);
}
return [
...(rootFileExportSymbols.includes(nodeSymbol) ? [nodeSymbol] : []),
// symbols which are used in types directly
...Array.from(symbolsUsingNode).filter((symbol: ts.Symbol) => {
const symbolsDeclarations = getDeclarationsForSymbol(symbol);
if (symbolsDeclarations.length === 0 || symbolsDeclarations.every((decl: ts.Declaration) => {
// we need to make sure that at least 1 declaration is inlined
return getModuleLikeModuleInfo(getClosestSourceFileLikeNode(decl), criteria, typeChecker).type !== ModuleType.ShouldBeInlined;
})) {
return false;
}
return rootFileExportSymbols.some((rootSymbol: ts.Symbol) => typesUsageEvaluator.isSymbolUsedBySymbol(symbol, rootSymbol));
}),
// symbols which are used in global types i.e. in `declare global`s
...(inlineDeclareGlobals ? getGlobalSymbolsUsingSymbol(nodeSymbol) : []),
];
}
function areDeclarationSame(left: ts.NamedDeclaration, right: ts.NamedDeclaration): boolean {
const leftSymbols = splitTransientSymbol(getNodeSymbol(left, typeChecker) as ts.Symbol, typeChecker);
const rightSymbols = splitTransientSymbol(getNodeSymbol(right, typeChecker) as ts.Symbol, typeChecker);
for (const leftSymbol of leftSymbols) {
if (rightSymbols.has(leftSymbol)) {
return true;
}
}
return false;
}
function getDeclarationsForExportedValues(exp: ts.ExportAssignment | ts.ExportDeclaration): ts.Declaration[] {
const nodeForSymbol = ts.isExportAssignment(exp) ? exp.expression : exp.moduleSpecifier;
if (nodeForSymbol === undefined) {
return [];
}
const symbolForExpression = typeChecker.getSymbolAtLocation(nodeForSymbol);
if (symbolForExpression === undefined) {
return [];
}
const symbol = getActualSymbol(symbolForExpression, typeChecker);
return getDeclarationsForSymbol(symbol);
}
function syncExports(): void {
for (const exp of rootFileExports) {
if (exp.type === ExportType.CommonJS) {
// commonjs will be handled separately where we handle root source files
// as the only way of adding it is to add an explicit export statement in the root source file
continue;
}
const symbolKnownNames = collisionsResolver.namesForSymbol(exp.symbol);
if (symbolKnownNames.size === 0) {
// that's fine if a symbol doesn't exists in collisions resolver because it operates on top-level symbols only
// in some cases a symbol can be exported but not added to the top-level scope
// for instance in case of re-export from a library `export { Foo } from 'bar'`
// in this case we'll add this re-export differently
continue;
}
if (symbolKnownNames.has(exp.exportedName)) {
// an exported symbol is already known with its "exported" name so nothing to do at this point
// but if it is re-exported from imported library then we assume it was previously imported so we should re-export it anyway
if (shouldSymbolBeImported(exp.symbol)) {
collectionResult.renamedExports.set(exp.exportedName, exp.exportedName);
}
continue;
}
// in case if this symbol isn't known yet we need to add it via renamed export
// we assume that all known names are equal so we can use any (first)
// usually all "local" names should have only one known name
// but having multiple names is possible with imports - you can import the same node with different names
// and we want to preserve the source input as much as we can that's why we re-use them
collectionResult.renamedExports.set(exp.exportedName, Array.from(symbolKnownNames)[0]);
}
}
for (const sourceFile of sourceFiles) {
verboseLog(`\n\n======= Preparing file: ${sourceFile.fileName} =======`);
const updateFn = sourceFile === rootSourceFile ? updateResultForRootModule : updateResultForAnyModule;
const currentModule = getFileModuleInfo(sourceFile.fileName, criteria);
updateFn(sourceFile.statements, currentModule);
// handle `import * as module` usage if it's used as whole module
if (currentModule.type === ModuleType.ShouldBeImported && isNodeUsed(sourceFile)) {
updateImportsForStatement(sourceFile);
}
}
if (entryConfig.failOnClass) {
const classes = collectionResult.statements.filter(ts.isClassDeclaration);
if (classes.length !== 0) {
const classesNames = classes.map((c: ts.ClassDeclaration) => c.name === undefined ? 'anonymous class' : c.name.text);
throw new Error(`${classes.length} class statement(s) are found in generated dts: ${classesNames.join(', ')}`);
}
}
syncExports();
// by default this option should be enabled
const exportReferencedTypes = outputOptions.exportReferencedTypes !== false;
return generateOutput(
{
...collectionResult,
resolveIdentifierName: (identifier: ts.Identifier | ts.QualifiedName | ts.PropertyAccessEntityNameExpression): string | null => {
if (ts.isIdentifier(identifier)) {
return collisionsResolver.resolveReferencedIdentifier(identifier);
} else {
return collisionsResolver.resolveReferencedQualifiedName(identifier);
}
},
shouldStatementHasExportKeyword: (statement: ts.Statement) => {
const statementExports = getExportsForStatement(rootFileExports, typeChecker, statement);
// If true, then no direct export was found. That means that node might have
// an export keyword (like interface, type, etc) otherwise, if there are
// only re-exports with renaming (like export { foo as bar }) we don't need
// to put export keyword for this statement because we'll re-export it in the way
// const hasStatementDefaultKeyword = hasNodeModifier(statement, ts.SyntaxKind.DefaultKeyword);
let result = statementExports.length === 0 || statementExports.find((exp: SourceFileExport) => {
if (ts.isVariableStatement(statement)) {
for (const variableDeclaration of statement.declarationList.declarations) {
if (ts.isIdentifier(variableDeclaration.name)) {
const resolvedName = collisionsResolver.resolveReferencedIdentifier(variableDeclaration.name);
if (exp.exportedName === resolvedName) {
return true;
}
}
// it seems that the compiler doesn't produce anything else (e.g. binding elements) in declaration files
// but it is still possible to write such code manually
// this feels like quite rare case so no support for now
}
return false;
}
if (isNodeNamedDeclaration(statement)) {
const nodeName = getNodeName(statement);
if (nodeName === undefined) {
throw new Error(`Cannot find node name ${statement.getText()}`);
}
const resolvedName = collisionsResolver.resolveReferencedIdentifier(nodeName as ts.Identifier);
return exp.exportedName === resolvedName;
}
return false;
}) !== undefined;
// "direct export" means export from the root source file
// e.g. classes/functions/etc must be exported from the root source file to have an "export" keyword
// by default interfaces/types are exported even if they aren't directly exported (e.g. when they are referenced by other types)
// but if `exportReferencedTypes` option is disabled we have to check direct export for them either
const onlyDirectlyExportedShouldBeExported = !exportReferencedTypes
|| ts.isClassDeclaration(statement)
|| (ts.isEnumDeclaration(statement) && !hasNodeModifier(statement, ts.SyntaxKind.ConstKeyword))
|| ts.isFunctionDeclaration(statement)
|| ts.isVariableStatement(statement)
|| ts.isModuleDeclaration(statement);
if (onlyDirectlyExportedShouldBeExported) {
// "valuable" statements must be re-exported from root source file
// to having export keyword in declaration file
result = result && statementExports.length !== 0;
} else if (isAmbientModule(statement) || ts.isExportDeclaration(statement)) {
result = false;
}
return result;
},
needStripConstFromConstEnum: (constEnum: ts.EnumDeclaration) => {
if (!program.getCompilerOptions().preserveConstEnums || !outputOptions.respectPreserveConstEnum) {
return false;
}
const enumSymbol = getNodeSymbol(constEnum, typeChecker);
if (enumSymbol === null) {
return false;
}
return rootFileExportSymbols.includes(enumSymbol);
},
needStripImportFromImportTypeNode: (node: ts.ImportTypeNode) => {
if (node.qualifier === undefined) {
return false;
}
if (!ts.isLiteralTypeNode(node.argument) || !ts.isStringLiteral(node.argument.literal)) {
return false;
}
return getReferencedModuleInfo(node, criteria, typeChecker)?.type === ModuleType.ShouldBeInlined;
},
},
{
sortStatements: outputOptions.sortNodes,
umdModuleName: outputOptions.umdModuleName,
noBanner: outputOptions.noBanner,
}
);
});
}