forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdeclarations.ts
1328 lines (1243 loc) · 73.7 KB
/
declarations.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
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*@internal*/
namespace ts {
export function getDeclarationDiagnostics(host: EmitHost, resolver: EmitResolver, file: SourceFile | undefined): Diagnostic[] {
if (file && isSourceFileJavaScript(file)) {
return []; // No declaration diagnostics for js for now
}
const compilerOptions = host.getCompilerOptions();
const result = transformNodes(resolver, host, compilerOptions, file ? [file] : filter(host.getSourceFiles(), isSourceFileNotJavaScript), [transformDeclarations], /*allowDtsFiles*/ false);
return result.diagnostics;
}
const declarationEmitNodeBuilderFlags =
NodeBuilderFlags.MultilineObjectLiterals |
TypeFormatFlags.WriteClassExpressionAsTypeLiteral |
NodeBuilderFlags.UseTypeOfFunction |
NodeBuilderFlags.UseStructuralFallback |
NodeBuilderFlags.AllowEmptyTuple |
NodeBuilderFlags.GenerateNamesForShadowedTypeParams;
/**
* Transforms a ts file into a .d.ts file
* This process requires type information, which is retrieved through the emit resolver. Because of this,
* in many places this transformer assumes it will be operating on parse tree nodes directly.
* This means that _no transforms should be allowed to occur before this one_.
*/
export function transformDeclarations(context: TransformationContext) {
const throwDiagnostic = () => Debug.fail("Diagnostic emitted without context");
let getSymbolAccessibilityDiagnostic: GetSymbolAccessibilityDiagnostic = throwDiagnostic;
let needsDeclare = true;
let isBundledEmit = false;
let resultHasExternalModuleIndicator = false;
let needsScopeFixMarker = false;
let resultHasScopeMarker = false;
let enclosingDeclaration: Node;
let necessaryTypeRefernces: Map<true>;
let lateMarkedStatements: LateVisibilityPaintedStatement[];
let lateStatementReplacementMap: Map<VisitResult<LateVisibilityPaintedStatement>>;
let suppressNewDiagnosticContexts: boolean;
const symbolTracker: SymbolTracker = {
trackSymbol,
reportInaccessibleThisError,
reportInaccessibleUniqueSymbolError,
reportPrivateInBaseOfClassExpression
};
let errorNameNode: DeclarationName | undefined;
let currentSourceFile: SourceFile;
const resolver = context.getEmitResolver();
const options = context.getCompilerOptions();
const newLine = getNewLineCharacter(options);
const { noResolve, stripInternal } = options;
const host = context.getEmitHost();
return transformRoot;
function recordTypeReferenceDirectivesIfNecessary(typeReferenceDirectives: string[]): void {
if (!typeReferenceDirectives) {
return;
}
necessaryTypeRefernces = necessaryTypeRefernces || createMap<true>();
for (const ref of typeReferenceDirectives) {
necessaryTypeRefernces.set(ref, true);
}
}
function handleSymbolAccessibilityError(symbolAccessibilityResult: SymbolAccessibilityResult) {
if (symbolAccessibilityResult.accessibility === SymbolAccessibility.Accessible) {
// Add aliases back onto the possible imports list if they're not there so we can try them again with updated visibility info
if (symbolAccessibilityResult && symbolAccessibilityResult.aliasesToMakeVisible) {
if (!lateMarkedStatements) {
lateMarkedStatements = symbolAccessibilityResult.aliasesToMakeVisible;
}
else {
for (const ref of symbolAccessibilityResult.aliasesToMakeVisible) {
pushIfUnique(lateMarkedStatements, ref);
}
}
}
// TODO: Do all these accessibility checks inside/after the first pass in the checker when declarations are enabled, if possible
}
else {
// Report error
const errorInfo = getSymbolAccessibilityDiagnostic(symbolAccessibilityResult);
if (errorInfo) {
if (errorInfo.typeName) {
context.addDiagnostic(createDiagnosticForNode(symbolAccessibilityResult.errorNode || errorInfo.errorNode,
errorInfo.diagnosticMessage,
getTextOfNode(errorInfo.typeName),
symbolAccessibilityResult.errorSymbolName,
symbolAccessibilityResult.errorModuleName));
}
else {
context.addDiagnostic(createDiagnosticForNode(symbolAccessibilityResult.errorNode || errorInfo.errorNode,
errorInfo.diagnosticMessage,
symbolAccessibilityResult.errorSymbolName,
symbolAccessibilityResult.errorModuleName));
}
}
}
}
function trackSymbol(symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags) {
if (symbol.flags & SymbolFlags.TypeParameter) return;
handleSymbolAccessibilityError(resolver.isSymbolAccessible(symbol, enclosingDeclaration, meaning, /*shouldComputeAliasesToMakeVisible*/ true));
recordTypeReferenceDirectivesIfNecessary(resolver.getTypeReferenceDirectivesForSymbol(symbol, meaning));
}
function reportPrivateInBaseOfClassExpression(propertyName: string) {
if (errorNameNode) {
context.addDiagnostic(
createDiagnosticForNode(errorNameNode, Diagnostics.Property_0_of_exported_class_expression_may_not_be_private_or_protected, propertyName));
}
}
function reportInaccessibleUniqueSymbolError() {
if (errorNameNode) {
context.addDiagnostic(createDiagnosticForNode(errorNameNode, Diagnostics.The_inferred_type_of_0_references_an_inaccessible_1_type_A_type_annotation_is_necessary,
declarationNameToString(errorNameNode),
"unique symbol"));
}
}
function reportInaccessibleThisError() {
if (errorNameNode) {
context.addDiagnostic(createDiagnosticForNode(errorNameNode, Diagnostics.The_inferred_type_of_0_references_an_inaccessible_1_type_A_type_annotation_is_necessary,
declarationNameToString(errorNameNode),
"this"));
}
}
function transformRoot(node: Bundle): Bundle;
function transformRoot(node: SourceFile): SourceFile;
function transformRoot(node: SourceFile | Bundle): SourceFile | Bundle;
function transformRoot(node: SourceFile | Bundle) {
if (node.kind === SyntaxKind.SourceFile && (node.isDeclarationFile || isSourceFileJavaScript(node))) {
return node;
}
if (node.kind === SyntaxKind.Bundle) {
isBundledEmit = true;
const refs = createMap<SourceFile>();
let hasNoDefaultLib = false;
const bundle = createBundle(map(node.sourceFiles,
sourceFile => {
if (sourceFile.isDeclarationFile || isSourceFileJavaScript(sourceFile)) return; // Omit declaration files from bundle results, too
hasNoDefaultLib = hasNoDefaultLib || sourceFile.hasNoDefaultLib;
currentSourceFile = sourceFile;
enclosingDeclaration = sourceFile;
lateMarkedStatements = undefined;
suppressNewDiagnosticContexts = false;
lateStatementReplacementMap = createMap();
getSymbolAccessibilityDiagnostic = throwDiagnostic;
needsScopeFixMarker = false;
resultHasScopeMarker = false;
collectReferences(sourceFile, refs);
if (isExternalModule(sourceFile)) {
resultHasExternalModuleIndicator = false; // unused in external module bundle emit (all external modules are within module blocks, therefore are known to be modules)
needsDeclare = false;
const statements = visitNodes(sourceFile.statements, visitDeclarationStatements);
const newFile = updateSourceFileNode(sourceFile, [createModuleDeclaration(
[],
[createModifier(SyntaxKind.DeclareKeyword)],
createLiteral(getResolvedExternalModuleName(context.getEmitHost(), sourceFile)),
createModuleBlock(setTextRange(createNodeArray(transformAndReplaceLatePaintedStatements(statements)), sourceFile.statements))
)], /*isDeclarationFile*/ true, /*referencedFiles*/ [], /*typeReferences*/ [], /*hasNoDefaultLib*/ false);
return newFile;
}
needsDeclare = true;
const updated = visitNodes(sourceFile.statements, visitDeclarationStatements);
return updateSourceFileNode(sourceFile, transformAndReplaceLatePaintedStatements(updated), /*isDeclarationFile*/ true, /*referencedFiles*/ [], /*typeReferences*/ [], /*hasNoDefaultLib*/ false);
}
), mapDefined(node.prepends, prepend => {
if (prepend.kind === SyntaxKind.InputFiles) {
return createUnparsedSourceFile(prepend.declarationText);
}
}));
bundle.syntheticFileReferences = [];
bundle.syntheticTypeReferences = getFileReferencesForUsedTypeReferences();
bundle.hasNoDefaultLib = hasNoDefaultLib;
const outputFilePath = getDirectoryPath(normalizeSlashes(getOutputPathsFor(node, host, /*forceDtsPaths*/ true).declarationFilePath));
const referenceVisitor = mapReferencesIntoArray(bundle.syntheticFileReferences as FileReference[], outputFilePath);
refs.forEach(referenceVisitor);
return bundle;
}
// Single source file
needsDeclare = true;
needsScopeFixMarker = false;
resultHasScopeMarker = false;
enclosingDeclaration = node;
currentSourceFile = node;
getSymbolAccessibilityDiagnostic = throwDiagnostic;
isBundledEmit = false;
resultHasExternalModuleIndicator = false;
suppressNewDiagnosticContexts = false;
lateMarkedStatements = undefined;
lateStatementReplacementMap = createMap();
necessaryTypeRefernces = undefined;
const refs = collectReferences(currentSourceFile, createMap());
const references: FileReference[] = [];
const outputFilePath = getDirectoryPath(normalizeSlashes(getOutputPathsFor(node, host, /*forceDtsPaths*/ true).declarationFilePath));
const referenceVisitor = mapReferencesIntoArray(references, outputFilePath);
refs.forEach(referenceVisitor);
const statements = visitNodes(node.statements, visitDeclarationStatements);
let combinedStatements = setTextRange(createNodeArray(transformAndReplaceLatePaintedStatements(statements)), node.statements);
const emittedImports = filter(combinedStatements, isAnyImportSyntax);
if (isExternalModule(node) && (!resultHasExternalModuleIndicator || (needsScopeFixMarker && !resultHasScopeMarker))) {
combinedStatements = setTextRange(createNodeArray([...combinedStatements, createExportDeclaration(/*decorators*/ undefined, /*modifiers*/ undefined, createNamedExports([]), /*moduleSpecifier*/ undefined)]), combinedStatements);
}
const updated = updateSourceFileNode(node, combinedStatements, /*isDeclarationFile*/ true, references, getFileReferencesForUsedTypeReferences(), node.hasNoDefaultLib);
return updated;
function getFileReferencesForUsedTypeReferences() {
return necessaryTypeRefernces ? mapDefined(arrayFrom(necessaryTypeRefernces.keys()), getFileReferenceForTypeName) : [];
}
function getFileReferenceForTypeName(typeName: string): FileReference | undefined {
// Elide type references for which we have imports
for (const importStatement of emittedImports) {
if (isImportEqualsDeclaration(importStatement) && isExternalModuleReference(importStatement.moduleReference)) {
const expr = importStatement.moduleReference.expression;
if (isStringLiteralLike(expr) && expr.text === typeName) {
return undefined;
}
}
else if (isImportDeclaration(importStatement) && isStringLiteral(importStatement.moduleSpecifier) && importStatement.moduleSpecifier.text === typeName) {
return undefined;
}
}
return { fileName: typeName, pos: -1, end: -1 };
}
function mapReferencesIntoArray(references: FileReference[], outputFilePath: string): (file: SourceFile) => void {
return file => {
let declFileName: string;
if (file.isDeclarationFile) { // Neither decl files or js should have their refs changed
declFileName = file.fileName;
}
else {
if (isBundledEmit && contains((node as Bundle).sourceFiles, file)) return; // Omit references to files which are being merged
const paths = getOutputPathsFor(file, host, /*forceDtsPaths*/ true);
declFileName = paths.declarationFilePath || paths.jsFilePath;
}
if (declFileName) {
let fileName = getRelativePathToDirectoryOrUrl(
outputFilePath,
declFileName,
host.getCurrentDirectory(),
host.getCanonicalFileName,
/*isAbsolutePathAnUrl*/ false
);
if (startsWith(fileName, "./") && hasExtension(fileName)) {
fileName = fileName.substring(2);
}
references.push({ pos: -1, end: -1, fileName });
}
};
}
}
function collectReferences(sourceFile: SourceFile, ret: Map<SourceFile>) {
if (noResolve || isSourceFileJavaScript(sourceFile)) return ret;
forEach(sourceFile.referencedFiles, f => {
const elem = tryResolveScriptReference(host, sourceFile, f);
if (elem) {
ret.set("" + getOriginalNodeId(elem), elem);
}
});
return ret;
}
function filterBindingPatternInitializers(name: BindingName) {
if (name.kind === SyntaxKind.Identifier) {
return name;
}
else {
if (name.kind === SyntaxKind.ArrayBindingPattern) {
return updateArrayBindingPattern(name, visitNodes(name.elements, visitBindingElement));
}
else {
return updateObjectBindingPattern(name, visitNodes(name.elements, visitBindingElement));
}
}
function visitBindingElement<T extends ArrayBindingElement>(elem: T): T;
function visitBindingElement(elem: ArrayBindingElement): ArrayBindingElement {
if (elem.kind === SyntaxKind.OmittedExpression) {
return elem;
}
return updateBindingElement(elem, elem.dotDotDotToken, elem.propertyName, filterBindingPatternInitializers(elem.name), shouldPrintWithInitializer(elem) ? elem.initializer : undefined);
}
}
function ensureParameter(p: ParameterDeclaration, modifierMask?: ModifierFlags): ParameterDeclaration {
let oldDiag: typeof getSymbolAccessibilityDiagnostic;
if (!suppressNewDiagnosticContexts) {
oldDiag = getSymbolAccessibilityDiagnostic;
getSymbolAccessibilityDiagnostic = createGetSymbolAccessibilityDiagnosticForNode(p);
}
const newParam = updateParameter(
p,
/*decorators*/ undefined,
maskModifiers(p, modifierMask),
p.dotDotDotToken,
filterBindingPatternInitializers(p.name),
resolver.isOptionalParameter(p) ? (p.questionToken || createToken(SyntaxKind.QuestionToken)) : undefined,
ensureType(p, p.type, /*ignorePrivate*/ true), // Ignore private param props, since this type is going straight back into a param
ensureNoInitializer(p)
);
if (!suppressNewDiagnosticContexts) {
getSymbolAccessibilityDiagnostic = oldDiag;
}
return newParam;
}
function shouldPrintWithInitializer(node: Node) {
return canHaveLiteralInitializer(node) && resolver.isLiteralConstDeclaration(getParseTreeNode(node) as CanHaveLiteralInitializer); // TODO: Make safe
}
function ensureNoInitializer(node: CanHaveLiteralInitializer) {
if (shouldPrintWithInitializer(node)) {
return resolver.createLiteralConstValue(getParseTreeNode(node) as CanHaveLiteralInitializer); // TODO: Make safe
}
return undefined;
}
type HasInferredType =
| FunctionDeclaration
| MethodDeclaration
| GetAccessorDeclaration
| SetAccessorDeclaration
| BindingElement
| ConstructSignatureDeclaration
| VariableDeclaration
| MethodSignature
| CallSignatureDeclaration
| ParameterDeclaration
| PropertyDeclaration
| PropertySignature;
function ensureType(node: HasInferredType, type: TypeNode, ignorePrivate?: boolean): TypeNode {
if (!ignorePrivate && hasModifier(node, ModifierFlags.Private)) {
// Private nodes emit no types (except private parameter properties, whose parameter types are actually visible)
return;
}
if (shouldPrintWithInitializer(node)) {
// Literal const declarations will have an initializer ensured rather than a type
return;
}
const shouldUseResolverType = node.kind === SyntaxKind.Parameter &&
(resolver.isRequiredInitializedParameter(node) ||
resolver.isOptionalUninitializedParameterProperty(node));
if (type && !shouldUseResolverType) {
return visitNode(type, visitDeclarationSubtree);
}
if (!getParseTreeNode(node)) {
return type ? visitNode(type, visitDeclarationSubtree) : createKeywordTypeNode(SyntaxKind.AnyKeyword);
}
if (node.kind === SyntaxKind.SetAccessor) {
// Set accessors with no associated type node (from it's param or get accessor return) are `any` since they are never contextually typed right now
// (The inferred type here will be void, but the old declaration emitter printed `any`, so this replicates that)
return createKeywordTypeNode(SyntaxKind.AnyKeyword);
}
errorNameNode = node.name;
let oldDiag: typeof getSymbolAccessibilityDiagnostic;
if (!suppressNewDiagnosticContexts) {
oldDiag = getSymbolAccessibilityDiagnostic;
getSymbolAccessibilityDiagnostic = createGetSymbolAccessibilityDiagnosticForNode(node);
}
if (node.kind === SyntaxKind.VariableDeclaration || node.kind === SyntaxKind.BindingElement) {
return cleanup(resolver.createTypeOfDeclaration(node, enclosingDeclaration, declarationEmitNodeBuilderFlags, symbolTracker));
}
if (node.kind === SyntaxKind.Parameter
|| node.kind === SyntaxKind.PropertyDeclaration
|| node.kind === SyntaxKind.PropertySignature) {
if (!node.initializer) return cleanup(resolver.createTypeOfDeclaration(node, enclosingDeclaration, declarationEmitNodeBuilderFlags, symbolTracker, shouldUseResolverType));
return cleanup(resolver.createTypeOfDeclaration(node, enclosingDeclaration, declarationEmitNodeBuilderFlags, symbolTracker, shouldUseResolverType) || resolver.createTypeOfExpression(node.initializer, enclosingDeclaration, declarationEmitNodeBuilderFlags, symbolTracker));
}
return cleanup(resolver.createReturnTypeOfSignatureDeclaration(node, enclosingDeclaration, declarationEmitNodeBuilderFlags, symbolTracker));
function cleanup(returnValue: TypeNode | undefined) {
errorNameNode = undefined;
if (!suppressNewDiagnosticContexts) {
getSymbolAccessibilityDiagnostic = oldDiag;
}
return returnValue || createKeywordTypeNode(SyntaxKind.AnyKeyword);
}
}
function isDeclarationAndNotVisible(node: NamedDeclaration) {
node = getParseTreeNode(node) as NamedDeclaration;
switch (node.kind) {
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.ClassDeclaration:
case SyntaxKind.TypeAliasDeclaration:
case SyntaxKind.EnumDeclaration:
return !resolver.isDeclarationVisible(node);
// The following should be doing their own visibility checks based on filtering their members
case SyntaxKind.VariableDeclaration:
return !getBindingNameVisible(node as VariableDeclaration);
case SyntaxKind.ImportEqualsDeclaration:
case SyntaxKind.ImportDeclaration:
case SyntaxKind.ExportDeclaration:
case SyntaxKind.ExportAssignment:
return false;
}
return false;
}
function getBindingNameVisible(elem: BindingElement | VariableDeclaration | OmittedExpression): boolean {
if (isOmittedExpression(elem)) {
return false;
}
if (isBindingPattern(elem.name)) {
// If any child binding pattern element has been marked visible (usually by collect linked aliases), then this is visible
return forEach(elem.name.elements, getBindingNameVisible);
}
else {
return resolver.isDeclarationVisible(elem);
}
}
function updateParamsList(node: Node, params: NodeArray<ParameterDeclaration>, modifierMask?: ModifierFlags) {
if (hasModifier(node, ModifierFlags.Private)) {
return undefined;
}
const newParams = map(params, p => ensureParameter(p, modifierMask));
if (!newParams) {
return undefined;
}
return createNodeArray(newParams, params.hasTrailingComma);
}
function ensureTypeParams(node: Node, params: NodeArray<TypeParameterDeclaration>) {
return hasModifier(node, ModifierFlags.Private) ? undefined : visitNodes(params, visitDeclarationSubtree);
}
function isEnclosingDeclaration(node: Node) {
return isSourceFile(node)
|| isTypeAliasDeclaration(node)
|| isModuleDeclaration(node)
|| isClassDeclaration(node)
|| isInterfaceDeclaration(node)
|| isFunctionLike(node)
|| isIndexSignatureDeclaration(node)
|| isMappedTypeNode(node);
}
function checkEntityNameVisibility(entityName: EntityNameOrEntityNameExpression, enclosingDeclaration: Node) {
const visibilityResult = resolver.isEntityNameVisible(entityName, enclosingDeclaration);
handleSymbolAccessibilityError(visibilityResult);
recordTypeReferenceDirectivesIfNecessary(resolver.getTypeReferenceDirectivesForEntityName(entityName));
}
function preserveJsDoc<T extends Node>(updated: T, original: Node): T {
if (hasJSDocNodes(updated) && hasJSDocNodes(original)) {
updated.jsDoc = original.jsDoc;
}
return setCommentRange(updated, getCommentRange(original));
}
function rewriteModuleSpecifier<T extends Node>(parent: ImportEqualsDeclaration | ImportDeclaration | ExportDeclaration | ModuleDeclaration | ImportTypeNode, input: T): T | StringLiteral {
if (!input) return;
resultHasExternalModuleIndicator = resultHasExternalModuleIndicator || (parent.kind !== SyntaxKind.ModuleDeclaration && parent.kind !== SyntaxKind.ImportType);
if (input.kind === SyntaxKind.StringLiteral && isBundledEmit) {
const newName = getExternalModuleNameFromDeclaration(context.getEmitHost(), resolver, parent);
if (newName) {
return createLiteral(newName);
}
}
return input;
}
function transformImportEqualsDeclaration(decl: ImportEqualsDeclaration) {
if (!resolver.isDeclarationVisible(decl)) return;
if (decl.moduleReference.kind === SyntaxKind.ExternalModuleReference) {
// Rewrite external module names if necessary
const specifier = getExternalModuleImportEqualsDeclarationExpression(decl);
return updateImportEqualsDeclaration(
decl,
/*decorators*/ undefined,
decl.modifiers,
decl.name,
updateExternalModuleReference(decl.moduleReference, rewriteModuleSpecifier(decl, specifier))
);
}
else {
const oldDiag = getSymbolAccessibilityDiagnostic;
getSymbolAccessibilityDiagnostic = createGetSymbolAccessibilityDiagnosticForNode(decl);
checkEntityNameVisibility(decl.moduleReference, enclosingDeclaration);
getSymbolAccessibilityDiagnostic = oldDiag;
return decl;
}
}
function transformImportDeclaration(decl: ImportDeclaration) {
if (!decl.importClause) {
// import "mod" - possibly needed for side effects? (global interface patches, module augmentations, etc)
return updateImportDeclaration(
decl,
/*decorators*/ undefined,
decl.modifiers,
decl.importClause,
rewriteModuleSpecifier(decl, decl.moduleSpecifier)
);
}
// The `importClause` visibility corresponds to the default's visibility.
const visibleDefaultBinding = decl.importClause && decl.importClause.name && resolver.isDeclarationVisible(decl.importClause) ? decl.importClause.name : undefined;
if (!decl.importClause.namedBindings) {
// No named bindings (either namespace or list), meaning the import is just default or should be elided
return visibleDefaultBinding && updateImportDeclaration(decl, /*decorators*/ undefined, decl.modifiers, updateImportClause(
decl.importClause,
visibleDefaultBinding,
/*namedBindings*/ undefined
), rewriteModuleSpecifier(decl, decl.moduleSpecifier));
}
if (decl.importClause.namedBindings.kind === SyntaxKind.NamespaceImport) {
// Namespace import (optionally with visible default)
const namedBindings = resolver.isDeclarationVisible(decl.importClause.namedBindings) ? decl.importClause.namedBindings : /*namedBindings*/ undefined;
return visibleDefaultBinding || namedBindings ? updateImportDeclaration(decl, /*decorators*/ undefined, decl.modifiers, updateImportClause(
decl.importClause,
visibleDefaultBinding,
namedBindings
), rewriteModuleSpecifier(decl, decl.moduleSpecifier)) : undefined;
}
// Named imports (optionally with visible default)
const bindingList = mapDefined(decl.importClause.namedBindings.elements, b => resolver.isDeclarationVisible(b) ? b : undefined);
if ((bindingList && bindingList.length) || visibleDefaultBinding) {
return updateImportDeclaration(
decl,
/*decorators*/ undefined,
decl.modifiers,
updateImportClause(
decl.importClause,
visibleDefaultBinding,
bindingList && bindingList.length ? updateNamedImports(decl.importClause.namedBindings, bindingList) : undefined
),
rewriteModuleSpecifier(decl, decl.moduleSpecifier)
);
}
// Nothing visible
}
function transformAndReplaceLatePaintedStatements(statements: NodeArray<Statement>): NodeArray<Statement> {
// This is a `while` loop because `handleSymbolAccessibilityError` can see additional import aliases marked as visible during
// error handling which must now be included in the output and themselves checked for errors.
// For example:
// ```
// module A {
// export module Q {}
// import B = Q;
// import C = B;
// export import D = C;
// }
// ```
// In such a scenario, only Q and D are initially visible, but we don't consider imports as private names - instead we say they if they are referenced they must
// be recorded. So while checking D's visibility we mark C as visible, then we must check C which in turn marks B, completing the chain of
// dependent imports and allowing a valid declaration file output. Today, this dependent alias marking only happens for internal import aliases.
while (length(lateMarkedStatements)) {
const i = lateMarkedStatements.shift();
if (!isLateVisibilityPaintedStatement(i)) {
return Debug.fail(`Late replaced statement was found which is not handled by the declaration transformer!: ${(ts as any).SyntaxKind ? (ts as any).SyntaxKind[(i as any).kind] : (i as any).kind}`);
}
const result = transformTopLevelDeclaration(i, /*privateDeclaration*/ true);
lateStatementReplacementMap.set("" + getOriginalNodeId(i), result);
}
// And lastly, we need to get the final form of all those indetermine import declarations from before and add them to the output list
// (and remove them from the set to examine for outter declarations)
return visitNodes(statements, visitLateVisibilityMarkedStatements);
function visitLateVisibilityMarkedStatements(statement: Statement) {
if (isLateVisibilityPaintedStatement(statement)) {
const key = "" + getOriginalNodeId(statement);
if (lateStatementReplacementMap.has(key)) {
const result = lateStatementReplacementMap.get(key);
lateStatementReplacementMap.delete(key);
if (result && isSourceFile(statement.parent)) {
if (isArray(result) ? some(result, needsScopeMarker) : needsScopeMarker(result)) {
// Top-level declarations in .d.ts files are always considered exported even without a modifier unless there's an export assignment or specifier
needsScopeFixMarker = true;
}
if (isArray(result) ? some(result, isExternalModuleIndicator) : isExternalModuleIndicator(result)) {
resultHasExternalModuleIndicator = true;
}
}
return result;
}
}
return statement;
}
}
function isExternalModuleIndicator(result: LateVisibilityPaintedStatement) {
// Exported top-level member indicates moduleness
return isAnyImportOrReExport(result) || isExportAssignment(result) || hasModifier(result, ModifierFlags.Export);
}
function needsScopeMarker(result: LateVisibilityPaintedStatement) {
return !isAnyImportOrReExport(result) && !isExportAssignment(result) && !hasModifier(result, ModifierFlags.Export) && !isAmbientModule(result);
}
function visitDeclarationSubtree(input: Node): VisitResult<Node> {
if (shouldStripInternal(input)) return;
if (isDeclaration(input)) {
if (isDeclarationAndNotVisible(input)) return;
if (hasDynamicName(input) && !resolver.isLateBound(getParseTreeNode(input) as Declaration)) {
return;
}
}
// Elide implementation signatures from overload sets
if (isFunctionLike(input) && resolver.isImplementationOfOverload(input)) return;
// Elide semicolon class statements
if (isSemicolonClassElement(input)) return;
let previousEnclosingDeclaration: typeof enclosingDeclaration;
if (isEnclosingDeclaration(input)) {
previousEnclosingDeclaration = enclosingDeclaration;
enclosingDeclaration = input as Declaration;
}
const oldDiag = getSymbolAccessibilityDiagnostic;
// Emit methods which are private as properties with no type information
if (isMethodDeclaration(input) || isMethodSignature(input)) {
if (hasModifier(input, ModifierFlags.Private)) {
if (input.symbol && input.symbol.declarations && input.symbol.declarations[0] !== input) return; // Elide all but the first overload
return cleanup(createProperty(/*decorators*/undefined, ensureModifiers(input), input.name, /*questionToken*/ undefined, /*type*/ undefined, /*initializer*/ undefined));
}
}
const canProdiceDiagnostic = canProduceDiagnostics(input);
if (canProdiceDiagnostic && !suppressNewDiagnosticContexts) {
getSymbolAccessibilityDiagnostic = createGetSymbolAccessibilityDiagnosticForNode(input as DeclarationDiagnosticProducing);
}
if (isTypeQueryNode(input)) {
checkEntityNameVisibility(input.exprName, enclosingDeclaration);
}
const oldWithinObjectLiteralType = suppressNewDiagnosticContexts;
let shouldEnterSuppressNewDiagnosticsContextContext = ((input.kind === SyntaxKind.TypeLiteral || input.kind === SyntaxKind.MappedType) && input.parent.kind !== SyntaxKind.TypeAliasDeclaration);
if (shouldEnterSuppressNewDiagnosticsContextContext) {
// We stop making new diagnostic contexts within object literal types. Unless it's an object type on the RHS of a type alias declaration. Then we do.
suppressNewDiagnosticContexts = true;
}
if (isProcessedComponent(input)) {
switch (input.kind) {
case SyntaxKind.ExpressionWithTypeArguments: {
if ((isEntityName(input.expression) || isEntityNameExpression(input.expression))) {
checkEntityNameVisibility(input.expression, enclosingDeclaration);
}
const node = visitEachChild(input, visitDeclarationSubtree, context);
return cleanup(updateExpressionWithTypeArguments(node, parenthesizeTypeParameters(node.typeArguments), node.expression));
}
case SyntaxKind.TypeReference: {
checkEntityNameVisibility(input.typeName, enclosingDeclaration);
const node = visitEachChild(input, visitDeclarationSubtree, context);
return cleanup(updateTypeReferenceNode(node, node.typeName, parenthesizeTypeParameters(node.typeArguments)));
}
case SyntaxKind.ConstructSignature:
return cleanup(updateConstructSignature(
input,
ensureTypeParams(input, input.typeParameters),
updateParamsList(input, input.parameters),
ensureType(input, input.type)
));
case SyntaxKind.Constructor: {
const isPrivate = hasModifier(input, ModifierFlags.Private);
// A constructor declaration may not have a type annotation
const ctor = createSignatureDeclaration(
SyntaxKind.Constructor,
isPrivate ? undefined : ensureTypeParams(input, input.typeParameters),
isPrivate ? undefined : updateParamsList(input, input.parameters, ModifierFlags.None),
/*type*/ undefined
);
ctor.modifiers = createNodeArray(ensureModifiers(input));
return cleanup(ctor);
}
case SyntaxKind.MethodDeclaration: {
const sig = createSignatureDeclaration(
SyntaxKind.MethodSignature,
ensureTypeParams(input, input.typeParameters),
updateParamsList(input, input.parameters),
ensureType(input, input.type)
) as MethodSignature;
sig.name = input.name;
sig.modifiers = createNodeArray(ensureModifiers(input));
sig.questionToken = input.questionToken;
return cleanup(sig);
}
case SyntaxKind.GetAccessor: {
const newNode = ensureAccessor(input);
return cleanup(newNode);
}
case SyntaxKind.SetAccessor: {
const newNode = ensureAccessor(input);
return cleanup(newNode);
}
case SyntaxKind.PropertyDeclaration:
return cleanup(updateProperty(
input,
/*decorators*/ undefined,
ensureModifiers(input),
input.name,
input.questionToken,
!hasModifier(input, ModifierFlags.Private) ? ensureType(input, input.type) : undefined,
ensureNoInitializer(input)
));
case SyntaxKind.PropertySignature:
return cleanup(updatePropertySignature(
input,
ensureModifiers(input),
input.name,
input.questionToken,
!hasModifier(input, ModifierFlags.Private) ? ensureType(input, input.type) : undefined,
ensureNoInitializer(input)
));
case SyntaxKind.MethodSignature: {
return cleanup(updateMethodSignature(
input,
ensureTypeParams(input, input.typeParameters),
updateParamsList(input, input.parameters),
ensureType(input, input.type),
input.name,
input.questionToken
));
}
case SyntaxKind.CallSignature: {
return cleanup(updateCallSignature(
input,
ensureTypeParams(input, input.typeParameters),
updateParamsList(input, input.parameters),
ensureType(input, input.type)
));
}
case SyntaxKind.IndexSignature: {
return cleanup(updateIndexSignature(
input,
/*decorators*/ undefined,
ensureModifiers(input),
updateParamsList(input, input.parameters),
visitNode(input.type, visitDeclarationSubtree) || createKeywordTypeNode(SyntaxKind.AnyKeyword)
));
}
case SyntaxKind.VariableDeclaration: {
if (isBindingPattern(input.name)) {
return recreateBindingPattern(input.name);
}
shouldEnterSuppressNewDiagnosticsContextContext = true;
suppressNewDiagnosticContexts = true; // Variable declaration types also suppress new diagnostic contexts, provided the contexts wouldn't be made for binding pattern types
return cleanup(updateVariableDeclaration(input, input.name, ensureType(input, input.type), ensureNoInitializer(input)));
}
case SyntaxKind.TypeParameter: {
if (isPrivateMethodTypeParameter(input) && (input.default || input.constraint)) {
return cleanup(updateTypeParameterDeclaration(input, input.name, /*constraint*/ undefined, /*defaultType*/ undefined));
}
return cleanup(visitEachChild(input, visitDeclarationSubtree, context));
}
case SyntaxKind.ConditionalType: {
// We have to process conditional types in a special way because for visibility purposes we need to push a new enclosingDeclaration
// just for the `infer` types in the true branch. It's an implicit declaration scope that only applies to _part_ of the type.
const checkType = visitNode(input.checkType, visitDeclarationSubtree);
const extendsType = visitNode(input.extendsType, visitDeclarationSubtree);
const oldEnclosingDecl = enclosingDeclaration;
enclosingDeclaration = input.trueType;
const trueType = visitNode(input.trueType, visitDeclarationSubtree);
enclosingDeclaration = oldEnclosingDecl;
const falseType = visitNode(input.falseType, visitDeclarationSubtree);
return cleanup(updateConditionalTypeNode(input, checkType, extendsType, trueType, falseType));
}
case SyntaxKind.FunctionType: {
return cleanup(updateFunctionTypeNode(input, visitNodes(input.typeParameters, visitDeclarationSubtree), updateParamsList(input, input.parameters), visitNode(input.type, visitDeclarationSubtree)));
}
case SyntaxKind.ConstructorType: {
return cleanup(updateConstructorTypeNode(input, visitNodes(input.typeParameters, visitDeclarationSubtree), updateParamsList(input, input.parameters), visitNode(input.type, visitDeclarationSubtree)));
}
case SyntaxKind.ImportType: {
if (!isLiteralImportTypeNode(input)) return cleanup(input);
return cleanup(updateImportTypeNode(
input,
updateLiteralTypeNode(input.argument, rewriteModuleSpecifier(input, input.argument.literal)),
input.qualifier,
visitNodes(input.typeArguments, visitDeclarationSubtree, isTypeNode),
input.isTypeOf
));
}
default: Debug.assertNever(input, `Attempted to process unhandled node kind: ${(ts as any).SyntaxKind[(input as any).kind]}`);
}
}
return cleanup(visitEachChild(input, visitDeclarationSubtree, context));
function cleanup<T extends Node>(returnValue: T | undefined): T {
if (returnValue && canProdiceDiagnostic && hasDynamicName(input as Declaration)) {
checkName(input as DeclarationDiagnosticProducing);
}
if (isEnclosingDeclaration(input)) {
enclosingDeclaration = previousEnclosingDeclaration;
}
if (canProdiceDiagnostic && !suppressNewDiagnosticContexts) {
getSymbolAccessibilityDiagnostic = oldDiag;
}
if (shouldEnterSuppressNewDiagnosticsContextContext) {
suppressNewDiagnosticContexts = oldWithinObjectLiteralType;
}
if (returnValue === input) {
return returnValue;
}
return returnValue && setOriginalNode(preserveJsDoc(returnValue, input), input);
}
}
function isPrivateMethodTypeParameter(node: TypeParameterDeclaration) {
return node.parent.kind === SyntaxKind.MethodDeclaration && hasModifier(node.parent, ModifierFlags.Private);
}
function visitDeclarationStatements(input: Node): VisitResult<Node> {
if (!isPreservedDeclarationStatement(input)) {
// return undefined for unmatched kinds to omit them from the tree
return;
}
if (shouldStripInternal(input)) return;
switch (input.kind) {
case SyntaxKind.ExportDeclaration: {
if (isSourceFile(input.parent)) {
resultHasExternalModuleIndicator = true;
resultHasScopeMarker = true;
}
// Always visible if the parent node isn't dropped for being not visible
// Rewrite external module names if necessary
return updateExportDeclaration(input, /*decorators*/ undefined, input.modifiers, input.exportClause, rewriteModuleSpecifier(input, input.moduleSpecifier));
}
case SyntaxKind.ExportAssignment: {
// Always visible if the parent node isn't dropped for being not visible
if (isSourceFile(input.parent)) {
resultHasExternalModuleIndicator = true;
resultHasScopeMarker = true;
}
if (input.expression.kind === SyntaxKind.Identifier) {
return input;
}
else {
const newId = createOptimisticUniqueName("_default");
getSymbolAccessibilityDiagnostic = () => ({
diagnosticMessage: Diagnostics.Default_export_of_the_module_has_or_is_using_private_name_0,
errorNode: input
});
const varDecl = createVariableDeclaration(newId, resolver.createTypeOfExpression(input.expression, input, declarationEmitNodeBuilderFlags, symbolTracker), /*initializer*/ undefined);
const statement = createVariableStatement(needsDeclare ? [createModifier(SyntaxKind.DeclareKeyword)] : [], createVariableDeclarationList([varDecl], NodeFlags.Const));
return [statement, updateExportAssignment(input, input.decorators, input.modifiers, newId)];
}
}
}
const result = transformTopLevelDeclaration(input);
// Don't actually transform yet; just leave as original node - will be elided/swapped by late pass
lateStatementReplacementMap.set("" + getOriginalNodeId(input), result);
return input;
}
function transformTopLevelDeclaration(input: LateVisibilityPaintedStatement, isPrivate?: boolean) {
if (shouldStripInternal(input)) return;
switch (input.kind) {
case SyntaxKind.ImportEqualsDeclaration: {
return transformImportEqualsDeclaration(input);
}
case SyntaxKind.ImportDeclaration: {
return transformImportDeclaration(input);
}
}
if (isDeclaration(input) && isDeclarationAndNotVisible(input)) return;
// Elide implementation signatures from overload sets
if (isFunctionLike(input) && resolver.isImplementationOfOverload(input)) return;
let previousEnclosingDeclaration: typeof enclosingDeclaration;
if (isEnclosingDeclaration(input)) {
previousEnclosingDeclaration = enclosingDeclaration;
enclosingDeclaration = input as Declaration;
}
const canProdiceDiagnostic = canProduceDiagnostics(input);
const oldDiag = getSymbolAccessibilityDiagnostic;
if (canProdiceDiagnostic) {
getSymbolAccessibilityDiagnostic = createGetSymbolAccessibilityDiagnosticForNode(input as DeclarationDiagnosticProducing);
}
const previousNeedsDeclare = needsDeclare;
switch (input.kind) {
case SyntaxKind.TypeAliasDeclaration: // Type aliases get `declare`d if need be (for legacy support), but that's all
return cleanup(updateTypeAliasDeclaration(
input,
/*decorators*/ undefined,
ensureModifiers(input, isPrivate),
input.name,
visitNodes(input.typeParameters, visitDeclarationSubtree, isTypeParameterDeclaration),
visitNode(input.type, visitDeclarationSubtree, isTypeNode)
));
case SyntaxKind.InterfaceDeclaration: {
return cleanup(updateInterfaceDeclaration(
input,
/*decorators*/ undefined,
ensureModifiers(input, isPrivate),
input.name,
ensureTypeParams(input, input.typeParameters),
transformHeritageClauses(input.heritageClauses),
visitNodes(input.members, visitDeclarationSubtree)
));
}
case SyntaxKind.FunctionDeclaration: {
// Generators lose their generator-ness, excepting their return type
return cleanup(updateFunctionDeclaration(
input,
/*decorators*/ undefined,
ensureModifiers(input, isPrivate),
/*asteriskToken*/ undefined,
input.name,
ensureTypeParams(input, input.typeParameters),
updateParamsList(input, input.parameters),
ensureType(input, input.type),
/*body*/ undefined
));
}
case SyntaxKind.ModuleDeclaration: {
needsDeclare = false;
const inner = input.body;
if (inner && inner.kind === SyntaxKind.ModuleBlock) {
const statements = visitNodes(inner.statements, visitDeclarationStatements);
const body = updateModuleBlock(inner, transformAndReplaceLatePaintedStatements(statements));
needsDeclare = previousNeedsDeclare;
const mods = ensureModifiers(input, isPrivate);
return cleanup(updateModuleDeclaration(
input,
/*decorators*/ undefined,
mods,
isExternalModuleAugmentation(input) ? rewriteModuleSpecifier(input, input.name) : input.name,
body
));
}
else {
needsDeclare = previousNeedsDeclare;
const mods = ensureModifiers(input, isPrivate);
needsDeclare = false;
visitNode(inner, visitDeclarationStatements);
// eagerly transform nested namespaces (the nesting doesn't need any elision or painting done)
const id = "" + getOriginalNodeId(inner);
const body = lateStatementReplacementMap.get(id);
lateStatementReplacementMap.delete(id);
return cleanup(updateModuleDeclaration(
input,
/*decorators*/ undefined,
mods,
input.name,
body as ModuleBody
));
}
}
case SyntaxKind.ClassDeclaration: {
const modifiers = createNodeArray(ensureModifiers(input, isPrivate));
const typeParameters = ensureTypeParams(input, input.typeParameters);
const ctor = getFirstConstructorWithBody(input);
let parameterProperties: PropertyDeclaration[];
if (ctor) {
const oldDiag = getSymbolAccessibilityDiagnostic;
parameterProperties = compact(flatMap(ctor.parameters, param => {
if (!hasModifier(param, ModifierFlags.ParameterPropertyModifier)) return;
getSymbolAccessibilityDiagnostic = createGetSymbolAccessibilityDiagnosticForNode(param);
if (param.name.kind === SyntaxKind.Identifier) {
return preserveJsDoc(createProperty(
/*decorators*/ undefined,
ensureModifiers(param),
param.name,
param.questionToken,
ensureType(param, param.type),
ensureNoInitializer(param)), param);
}
else {
// Pattern - this is currently an error, but we emit declarations for it somewhat correctly
return walkBindingPattern(param.name);
}
function walkBindingPattern(pattern: BindingPattern) {
let elems: PropertyDeclaration[];
for (const elem of pattern.elements) {
if (isOmittedExpression(elem)) continue;
if (isBindingPattern(elem.name)) {
elems = concatenate(elems, walkBindingPattern(elem.name));
}
elems = elems || [];
elems.push(createProperty(
/*decorators*/ undefined,
ensureModifiers(param),