-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathSourceFile.ts
1038 lines (914 loc) · 36.1 KB
/
SourceFile.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
import {
ArrayUtils,
errors,
EventContainer,
FileUtils,
LanguageVariant,
libFolderInMemoryPath,
Memoize,
ScriptKind,
ScriptTarget,
StandardizedFilePath,
StringUtils,
ts,
} from "@ts-morph/common";
import { Directory } from "../../../fileSystem";
import {
getTextFromTextChanges,
insertIntoTextRange,
replaceNodeText,
replaceSourceFileForFilePathMove,
replaceSourceFileTextForFormatting,
} from "../../../manipulation";
import { getNextMatchingPos, getPreviousMatchingPos } from "../../../manipulation/textSeek";
import { ProjectContext } from "../../../ProjectContext";
import { SourceFileSpecificStructure, SourceFileStructure, StructureKind } from "../../../structures";
import { Constructor } from "../../../types";
import { CharCodes, ModuleUtils, SourceFileReferenceContainer, SourceFileReferencingNodes } from "../../../utils";
import { Diagnostic, EmitOptionsBase, EmitOutput, EmitResult, FormatCodeSettings, TextChange, UserPreferences } from "../../tools";
import { ModuledNode, TextInsertableNode } from "../base";
import { callBaseGetStructure } from "../callBaseGetStructure";
import { callBaseSet } from "../callBaseSet";
import { Node, TextRange } from "../common";
import { StringLiteral } from "../literal";
import { StatementedNode } from "../statement";
import { FileReference, FileSystemRefreshResult } from "./results";
export interface SourceFileCopyOptions {
overwrite?: boolean;
}
export interface SourceFileMoveOptions {
overwrite?: boolean;
}
/**
* Options for emitting a source file.
*/
export interface SourceFileEmitOptions extends EmitOptionsBase {
}
/** @internal */
export interface SourceFileReferences {
literalReferences: [StringLiteral, SourceFile][];
referencingLiterals: StringLiteral[];
}
// todo: not sure why I need to explicitly type this in order to get TS to not complain... (TS 2.4.1)
export const SourceFileBase: Constructor<ModuledNode> & Constructor<StatementedNode> & Constructor<TextInsertableNode> & typeof Node = ModuledNode(
TextInsertableNode(StatementedNode(Node)),
);
export class SourceFile extends SourceFileBase<ts.SourceFile> {
#isSaved = false;
readonly #modifiedEventContainer = new EventContainer<SourceFile>();
readonly #preModifiedEventContainer = new EventContainer<SourceFile>();
/** @internal */
readonly _referenceContainer = new SourceFileReferenceContainer(this);
#referencedFiles: FileReference[] | undefined;
#libReferenceDirectives: FileReference[] | undefined;
#typeReferenceDirectives: FileReference[] | undefined;
/** @internal */
_hasBom: true | undefined;
/**
* Initializes a new instance.
* @private
* @param context - Project context.
* @param node - Underlying node.
*/
constructor(
context: ProjectContext,
node: ts.SourceFile,
) {
super(context, node, undefined);
// typescript doesn't allow calling `super` with `this`, so set this after
this.__sourceFile = this;
// store this before a modification happens to the file
const onPreModified = () => {
this.isFromExternalLibrary(); // memoize
this.#preModifiedEventContainer.unsubscribe(onPreModified);
};
this.#preModifiedEventContainer.subscribe(onPreModified);
}
/**
* @internal
*
* WARNING: This should only be called by the compiler factory!
*/
_replaceCompilerNodeFromFactory(compilerNode: ts.SourceFile) {
super._replaceCompilerNodeFromFactory(compilerNode);
this._context.resetProgram(); // make sure the program has the latest source file
this.#isSaved = false;
this.#modifiedEventContainer.fire(this);
}
/** @internal */
protected _clearInternals() {
super._clearInternals();
clearTextRanges(this.#referencedFiles);
clearTextRanges(this.#typeReferenceDirectives);
clearTextRanges(this.#libReferenceDirectives);
this.#referencedFiles = undefined;
this.#typeReferenceDirectives = undefined;
this.#libReferenceDirectives = undefined;
function clearTextRanges(textRanges: ReadonlyArray<TextRange> | undefined) {
textRanges?.forEach(r => r._forget());
}
}
/**
* Gets the file path.
*/
getFilePath() {
return this.compilerNode.fileName as StandardizedFilePath;
}
/**
* Gets the file path's base name.
*/
getBaseName() {
return FileUtils.getBaseName(this.getFilePath());
}
/**
* Gets the file path's base name without the extension.
*/
getBaseNameWithoutExtension() {
const baseName = this.getBaseName();
const extension = this.getExtension();
return baseName.substring(0, baseName.length - extension.length);
}
/**
* Gets the file path's extension.
*/
getExtension() {
return FileUtils.getExtension(this.getFilePath());
}
/**
* Gets the directory that the source file is contained in.
*/
getDirectory(): Directory {
return this._context.compilerFactory.getDirectoryFromCache(this.getDirectoryPath())!;
}
/**
* Gets the directory path that the source file is contained in.
*/
getDirectoryPath(): StandardizedFilePath {
return this._context.fileSystemWrapper.getStandardizedAbsolutePath(FileUtils.getDirPath(this.compilerNode.fileName));
}
/**
* Gets the full text with leading trivia.
*/
getFullText() {
// return the string instead of letting Node.getFullText() do a substring to prevent an extra allocation
return this.compilerNode.text;
}
/**
* Gets the line and column number at the provided position (1-indexed).
* @param pos - Position in the source file.
*/
getLineAndColumnAtPos(pos: number) {
const fullText = this.getFullText();
return {
line: StringUtils.getLineNumberAtPos(fullText, pos),
column: StringUtils.getLengthFromLineStartAtPos(fullText, pos) + 1,
};
}
/**
* Gets the character count from the start of the line to the provided position.
* @param pos - Position.
*/
getLengthFromLineStartAtPos(pos: number) {
return StringUtils.getLengthFromLineStartAtPos(this.getFullText(), pos);
}
/**
* Copies this source file to the specified directory.
*
* This will modify the module specifiers in the new file, if necessary.
* @param dirPathOrDirectory Directory path or directory object to copy the file to.
* @param options Options for copying.
* @returns The source file the copy was made to.
*/
copyToDirectory(dirPathOrDirectory: string | Directory, options?: SourceFileCopyOptions) {
const dirPath = typeof dirPathOrDirectory === "string" ? dirPathOrDirectory : dirPathOrDirectory.getPath();
return this.copy(FileUtils.pathJoin(dirPath, this.getBaseName()), options);
}
/**
* Copy this source file to a new file.
*
* This will modify the module specifiers in the new file, if necessary.
* @param filePath - New file path. Can be relative to the original file or an absolute path.
* @param options - Options for copying.
*/
copy(filePath: string, options: SourceFileCopyOptions = {}): SourceFile {
this._throwIfIsInMemoryLibFile();
const result = this._copyInternal(filePath, options);
if (result === false)
return this;
const copiedSourceFile = result;
if (copiedSourceFile.getDirectoryPath() !== this.getDirectoryPath())
copiedSourceFile._updateReferencesForCopyInternal(this._getReferencesForCopyInternal());
return copiedSourceFile;
}
/** @internal */
_copyInternal(fileAbsoluteOrRelativePath: string, options: SourceFileCopyOptions = {}) {
const { overwrite = false } = options;
const { compilerFactory, fileSystemWrapper } = this._context;
const standardizedFilePath = fileSystemWrapper.getStandardizedAbsolutePath(fileAbsoluteOrRelativePath, this.getDirectoryPath());
if (standardizedFilePath === this.getFilePath())
return false;
return getCopiedSourceFile(this);
function getCopiedSourceFile(currentFile: SourceFile) {
try {
return compilerFactory.createSourceFileFromText(standardizedFilePath, currentFile.getFullText(), { overwrite, markInProject: getShouldBeInProject() });
} catch (err) {
if (err instanceof errors.InvalidOperationError)
throw new errors.InvalidOperationError(`Did you mean to provide the overwrite option? ` + err.message);
else
throw err;
}
function getShouldBeInProject() {
if (currentFile._isInProject())
return true;
const destinationFile = compilerFactory.getSourceFileFromCacheFromFilePath(standardizedFilePath);
return destinationFile != null && destinationFile._isInProject();
}
}
}
/** @internal */
_getReferencesForCopyInternal(): [StringLiteral, SourceFile][] {
return Array.from(this._referenceContainer.getLiteralsReferencingOtherSourceFilesEntries());
}
/** @internal */
_updateReferencesForCopyInternal(literalReferences: ReadonlyArray<[StringLiteral, SourceFile]>) {
// update the nodes in this list to point to the nodes in this copied source file
for (const reference of literalReferences)
reference[0] = this.getChildSyntaxListOrThrow().getDescendantAtStartWithWidth(reference[0].getStart(), reference[0].getWidth())! as StringLiteral;
// update the string literals in the copied file
updateStringLiteralReferences(literalReferences);
}
/**
* Copy this source file to a new file and immediately saves it to the file system asynchronously.
*
* This will modify the module specifiers in the new file, if necessary.
* @param filePath - New file path. Can be relative to the original file or an absolute path.
* @param options - Options for copying.
*/
async copyImmediately(filePath: string, options?: SourceFileCopyOptions): Promise<SourceFile> {
const newSourceFile = this.copy(filePath, options);
await newSourceFile.save();
return newSourceFile;
}
/**
* Copy this source file to a new file and immediately saves it to the file system synchronously.
*
* This will modify the module specifiers in the new file, if necessary.
* @param filePath - New file path. Can be relative to the original file or an absolute path.
* @param options - Options for copying.
*/
copyImmediatelySync(filePath: string, options?: SourceFileCopyOptions): SourceFile {
const newSourceFile = this.copy(filePath, options);
newSourceFile.saveSync();
return newSourceFile;
}
/**
* Moves this source file to the specified directory.
*
* This will modify the module specifiers in other files that specify this file and the module specifiers in the current file, if necessary.
* @param dirPathOrDirectory Directory path or directory object to move the file to.
* @param options Options for moving.
*/
moveToDirectory(dirPathOrDirectory: string | Directory, options?: SourceFileMoveOptions) {
const dirPath = typeof dirPathOrDirectory === "string" ? dirPathOrDirectory : dirPathOrDirectory.getPath();
return this.move(FileUtils.pathJoin(dirPath, this.getBaseName()), options);
}
/**
* Moves this source file to a new file.
*
* This will modify the module specifiers in other files that specify this file and the module specifiers in the current file, if necessary.
* @param filePath - New file path. Can be relative to the original file or an absolute path.
* @param options - Options for moving.
*/
move(filePath: string, options: SourceFileMoveOptions = {}): SourceFile {
this._throwIfIsInMemoryLibFile();
const oldDirPath = this.getDirectoryPath();
const sourceFileReferences = this._getReferencesForMoveInternal();
const oldFilePath = this.getFilePath();
if (!this._moveInternal(filePath, options))
return this;
this._context.fileSystemWrapper.queueFileDelete(oldFilePath);
this._updateReferencesForMoveInternal(sourceFileReferences, oldDirPath);
// ignore any modifications in other source files
this._context.lazyReferenceCoordinator.clearDirtySourceFiles();
// need to add the current source file as being dirty because it was removed and added to the cache in the move
this._context.lazyReferenceCoordinator.addDirtySourceFile(this);
return this;
}
/** @internal */
_moveInternal(fileRelativeOrAbsolutePath: string, options: SourceFileMoveOptions = {}) {
const { overwrite = false } = options;
const filePath = this._context.fileSystemWrapper.getStandardizedAbsolutePath(fileRelativeOrAbsolutePath, this.getDirectoryPath());
if (filePath === this.getFilePath())
return false;
let markAsInProject = false;
if (overwrite) {
// remove the past file if it exists
const existingSourceFile = this._context.compilerFactory.getSourceFileFromCacheFromFilePath(filePath);
if (existingSourceFile != null) {
markAsInProject = existingSourceFile._isInProject();
existingSourceFile.forget();
}
} else {
this._context.compilerFactory.throwIfFileExists(filePath, "Did you mean to provide the overwrite option?");
}
replaceSourceFileForFilePathMove({
newFilePath: filePath,
sourceFile: this,
});
if (markAsInProject)
this._markAsInProject();
if (this._isInProject())
this.getDirectory()._markAsInProject();
return true;
}
/** @internal */
_getReferencesForMoveInternal(): SourceFileReferences {
return {
literalReferences: Array.from(this._referenceContainer.getLiteralsReferencingOtherSourceFilesEntries()),
referencingLiterals: Array.from(this._referenceContainer.getReferencingLiteralsInOtherSourceFiles()),
};
}
/** @internal */
_updateReferencesForMoveInternal(sourceFileReferences: SourceFileReferences, oldDirPath: string) {
const { literalReferences, referencingLiterals } = sourceFileReferences;
// update the literals in this file if the directory has changed
if (oldDirPath !== this.getDirectoryPath())
updateStringLiteralReferences(literalReferences);
// update the string literals in other files
updateStringLiteralReferences(referencingLiterals.map(node => [node, this] as [StringLiteral, SourceFile]));
}
/**
* Moves this source file to a new file and asynchronously updates the file system immediately.
*
* This will modify the module specifiers in other files that specify this file and the module specifiers in the current file, if necessary.
* @param filePath - New file path. Can be relative to the original file or an absolute path.
* @param options - Options for moving.
*/
async moveImmediately(filePath: string, options?: SourceFileMoveOptions): Promise<SourceFile> {
const oldFilePath = this.getFilePath();
const newFilePath = this._context.fileSystemWrapper.getStandardizedAbsolutePath(filePath, this.getDirectoryPath());
this.move(filePath, options);
if (oldFilePath !== newFilePath) {
await this._context.fileSystemWrapper.moveFileImmediately(oldFilePath, newFilePath, this.getFullText());
this.#isSaved = true;
} else {
await this.save();
}
return this;
}
/**
* Moves this source file to a new file and synchronously updates the file system immediately.
*
* This will modify the module specifiers in other files that specify this file and the module specifiers in the current file, if necessary.
* @param filePath - New file path. Can be relative to the original file or an absolute path.
* @param options - Options for moving.
*/
moveImmediatelySync(filePath: string, options?: SourceFileMoveOptions): SourceFile {
const oldFilePath = this.getFilePath();
const newFilePath = this._context.fileSystemWrapper.getStandardizedAbsolutePath(filePath, this.getDirectoryPath());
this.move(filePath, options);
if (oldFilePath !== newFilePath) {
this._context.fileSystemWrapper.moveFileImmediatelySync(oldFilePath, newFilePath, this.getFullText());
this.#isSaved = true;
} else {
this.saveSync();
}
return this;
}
/**
* Queues a deletion of the file to the file system.
*
* The file will be deleted when you call ast.save(). If you wish to immediately delete the file, then use deleteImmediately().
*/
delete() {
this._throwIfIsInMemoryLibFile();
const filePath = this.getFilePath();
this.forget();
this._context.fileSystemWrapper.queueFileDelete(filePath);
}
/**
* Asynchronously deletes the file from the file system.
*/
async deleteImmediately() {
this._throwIfIsInMemoryLibFile();
const filePath = this.getFilePath();
this.forget();
await this._context.fileSystemWrapper.deleteFileImmediately(filePath);
}
/**
* Synchronously deletes the file from the file system.
*/
deleteImmediatelySync() {
this._throwIfIsInMemoryLibFile();
const filePath = this.getFilePath();
this.forget();
this._context.fileSystemWrapper.deleteFileImmediatelySync(filePath);
}
/**
* Asynchronously saves this file with any changes.
*/
async save() {
if (this._isLibFileInMemory())
return;
await this._context.fileSystemWrapper.writeFile(this.getFilePath(), this.#getTextForSave());
this.#isSaved = true;
}
/**
* Synchronously saves this file with any changes.
*/
saveSync() {
if (this._isLibFileInMemory())
return;
this._context.fileSystemWrapper.writeFileSync(this.getFilePath(), this.#getTextForSave());
this.#isSaved = true;
}
/** @internal */
#getTextForSave() {
const text = this.getFullText();
return this._hasBom ? "\uFEFF" + text : text;
}
/**
* Gets any `/// <reference path="..." />` comments.
*/
getPathReferenceDirectives() {
if (this.#referencedFiles == null) {
this.#referencedFiles = (this.compilerNode.referencedFiles || [])
.map(f => new FileReference(f, this));
}
return this.#referencedFiles;
}
/**
* Gets any `/// <reference types="..." />` comments.
*/
getTypeReferenceDirectives() {
if (this.#typeReferenceDirectives == null) {
this.#typeReferenceDirectives = (this.compilerNode.typeReferenceDirectives || [])
.map(f => new FileReference(f, this));
}
return this.#typeReferenceDirectives;
}
/**
* Gets any `/// <reference lib="..." />` comments.
*/
getLibReferenceDirectives() {
if (this.#libReferenceDirectives == null) {
this.#libReferenceDirectives = (this.compilerNode.libReferenceDirectives || [])
.map(f => new FileReference(f, this));
}
return this.#libReferenceDirectives;
}
/**
* Gets any source files that reference this source file.
*/
getReferencingSourceFiles() {
return Array.from(this._referenceContainer.getDependentSourceFiles());
}
/**
* Gets the import and exports in other source files that reference this source file.
*/
getReferencingNodesInOtherSourceFiles() {
const literals = this.getReferencingLiteralsInOtherSourceFiles();
return Array.from(getNodes());
function* getNodes(): Iterable<SourceFileReferencingNodes> {
for (const literal of literals)
yield getReferencingNodeFromStringLiteral(literal);
}
}
/**
* Gets the string literals in other source files that reference this source file.
*/
getReferencingLiteralsInOtherSourceFiles() {
return Array.from(this._referenceContainer.getReferencingLiteralsInOtherSourceFiles());
}
/**
* Gets the source files this source file references in string literals.
*/
getReferencedSourceFiles() {
const entries = this._referenceContainer.getLiteralsReferencingOtherSourceFilesEntries();
return Array.from(new Set<SourceFile>(getSourceFilesFromEntries()).values());
function* getSourceFilesFromEntries(): Iterable<SourceFile> {
for (const [, sourceFile] of entries)
yield sourceFile;
}
}
/**
* Gets the nodes that reference other source files in string literals.
*/
getNodesReferencingOtherSourceFiles() {
const entries = this._referenceContainer.getLiteralsReferencingOtherSourceFilesEntries();
return Array.from(getNodes());
function* getNodes(): Iterable<SourceFileReferencingNodes> {
for (const [literal] of entries)
yield getReferencingNodeFromStringLiteral(literal);
}
}
/**
* Gets the string literals in this source file that references other source files.
* @remarks This is similar to `getImportStringLiterals()`, but `getImportStringLiterals()`
* will return import string literals that may not be referencing another source file
* or have not been able to be resolved.
*/
getLiteralsReferencingOtherSourceFiles() {
const entries = this._referenceContainer.getLiteralsReferencingOtherSourceFilesEntries();
return Array.from(getLiteralsFromEntries());
function* getLiteralsFromEntries(): Iterable<StringLiteral> {
for (const [literal] of entries)
yield literal;
}
}
/**
* Gets all the descendant string literals that reference a module.
*/
getImportStringLiterals() {
this._ensureBound();
const literals = ((this.compilerNode as any).imports || []) as ts.StringLiteral[];
// exclude import helpers
return literals.filter(l => l.pos !== -1).map(l => this._getNodeFromCompilerNode(l));
}
/**
* Gets the script target of the source file.
*/
getLanguageVersion(): ScriptTarget {
return this.compilerNode.languageVersion;
}
/**
* Gets the language variant of the source file.
*/
getLanguageVariant(): LanguageVariant {
return this.compilerNode.languageVariant;
}
/**
* Gets the script kind of the source file.
*/
getScriptKind(): ScriptKind {
// todo: open issue on typescript repo about making this not internal?
// otherwise, store a collection of what each source file should be.
return (this.compilerNode as any).scriptKind;
}
/**
* Gets if this is a declaration file.
*/
isDeclarationFile() {
return this.compilerNode.isDeclarationFile;
}
/**
* Gets if the source file was discovered while loading an external library.
*/
@Memoize
isFromExternalLibrary() {
// This needs to be memoized and stored before modification because the TypeScript
// compiler does the following code:
//
// function isSourceFileFromExternalLibrary(file: SourceFile): boolean {
// return !!sourceFilesFoundSearchingNodeModules.get(file.path);
// }
//
// So the compiler node will become out of date after a manipulation occurs and
// this will return false.
// do not create the program if not created before... if the program is
// not created then we know this source file wasn't discovered by the program
if (!this._context.program._isCompilerProgramCreated())
return false;
const compilerProgram = this._context.program.compilerObject;
return compilerProgram.isSourceFileFromExternalLibrary(this.compilerNode);
}
/**
* Gets if the source file is a descendant of a node_modules directory.
*/
isInNodeModules() {
return this.getFilePath().indexOf("/node_modules/") >= 0;
}
/**
* Gets if this source file has been saved or if the latest changes have been saved.
*/
isSaved() {
return this.#isSaved && !this._isLibFileInMemory();
}
/**
* Sets if this source file has been saved.
* @internal
*/
_setIsSaved(value: boolean) {
this.#isSaved = value;
}
/**
* Gets the pre-emit diagnostics of the specified source file.
*/
getPreEmitDiagnostics(): Diagnostic[] {
return this._context.getPreEmitDiagnostics(this);
}
/**
* Deindents the line at the specified position.
* @param pos - Position.
* @param times - Times to unindent. Specify a negative value to indent.
*/
unindent(pos: number, times?: number): this;
/**
* Deindents the lines within the specified range.
* @param positionRange - Position range.
* @param times - Times to unindent. Specify a negative value to indent.
*/
unindent(positionRange: [number, number], times?: number): this;
/**
* @internal
*/
unindent(positionRangeOrPos: [number, number] | number, times?: number): this;
unindent(positionRangeOrPos: [number, number] | number, times = 1) {
return this.indent(positionRangeOrPos, times * -1);
}
/**
* Indents the line at the specified position.
* @param pos - Position.
* @param times - Times to indent. Specify a negative value to unindent.
*/
indent(pos: number, times?: number): this;
/**
* Indents the lines within the specified range.
* @param positionRange - Position range.
* @param times - Times to indent. Specify a negative value to unindent.
*/
indent(positionRange: [number, number], times?: number): this;
/**
* @internal
*/
indent(positionRangeOrPos: [number, number] | number, times?: number): this;
indent(positionRangeOrPos: [number, number] | number, times = 1) {
if (times === 0)
return this;
const sourceFileText = this.getFullText();
const positionRange = typeof positionRangeOrPos === "number" ? [positionRangeOrPos, positionRangeOrPos] as [number, number] : positionRangeOrPos;
errors.throwIfRangeOutOfRange(positionRange, [0, sourceFileText.length], "positionRange");
const startLinePos = getPreviousMatchingPos(sourceFileText, positionRange[0], char => char === CharCodes.NEWLINE);
const endLinePos = getNextMatchingPos(sourceFileText, positionRange[1], char => char === CharCodes.CARRIAGE_RETURN || char === CharCodes.NEWLINE);
const correctedText = StringUtils.indent(sourceFileText.substring(startLinePos, endLinePos), times, {
indentText: this._context.manipulationSettings.getIndentationText(),
indentSizeInSpaces: this._context.manipulationSettings._getIndentSizeInSpaces(),
isInStringAtPos: pos => this.isInStringAtPos(pos + startLinePos),
});
replaceSourceFileTextForFormatting({
sourceFile: this,
newText: sourceFileText.substring(0, startLinePos) + correctedText + sourceFileText.substring(endLinePos),
});
return this;
}
/**
* Asynchronously emits the source file as a JavaScript file.
*/
emit(options?: SourceFileEmitOptions): Promise<EmitResult> {
return this._context.program.emit({ targetSourceFile: this, ...options });
}
/**
* Synchronously emits the source file as a JavaScript file.
*/
emitSync(options?: SourceFileEmitOptions): EmitResult {
return this._context.program.emitSync({ targetSourceFile: this, ...options });
}
/**
* Gets the emit output of this source file.
* @param options - Emit options.
*/
getEmitOutput(options: { emitOnlyDtsFiles?: boolean } = {}): EmitOutput {
return this._context.languageService.getEmitOutput(this, options.emitOnlyDtsFiles || false);
}
/**
* Formats the source file text using the internal TypeScript formatting API.
* @param settings - Format code settings.
*/
formatText(settings: FormatCodeSettings = {}) {
replaceSourceFileTextForFormatting({
sourceFile: this,
newText: this._context.languageService.getFormattedDocumentText(this.getFilePath(), settings),
});
}
/**
* Refresh the source file from the file system.
*
* WARNING: When updating from the file system, this will "forget" any previously navigated nodes.
* @returns What action ended up taking place.
*/
async refreshFromFileSystem(): Promise<FileSystemRefreshResult> {
const fileReadResult = await this._context.fileSystemWrapper.readFileOrNotExists(this.getFilePath(), this._context.getEncoding());
return this.#refreshFromFileSystemInternal(fileReadResult);
}
/**
* Synchronously refreshes the source file from the file system.
*
* WARNING: When updating from the file system, this will "forget" any previously navigated nodes.
* @returns What action ended up taking place.
*/
refreshFromFileSystemSync(): FileSystemRefreshResult {
const fileReadResult = this._context.fileSystemWrapper.readFileOrNotExistsSync(this.getFilePath(), this._context.getEncoding());
return this.#refreshFromFileSystemInternal(fileReadResult);
}
/**
* Gets the relative path to the specified path.
* @param fileOrDirPath - The file or directory path.
*/
getRelativePathTo(fileOrDirPath: string): string;
/**
* Gets the relative path to another source file.
* @param sourceFile - Source file.
*/
getRelativePathTo(sourceFile: SourceFile): string;
/**
* Gets the relative path to another directory.
* @param directory - Directory.
*/
getRelativePathTo(directory: Directory): string;
getRelativePathTo(sourceFileDirOrPath: SourceFile | Directory | string) {
return this.getDirectory().getRelativePathTo(sourceFileDirOrPath);
}
/**
* Gets the relative path to the specified file path as a module specifier.
* @param filePath - File path.
* @remarks To get to a directory, provide `path/to/directory/index.ts`.
*/
getRelativePathAsModuleSpecifierTo(filePath: string): string;
/**
* Gets the relative path to the specified source file as a module specifier.
* @param sourceFile - Source file.
*/
getRelativePathAsModuleSpecifierTo(sourceFile: SourceFile): string;
/**
* Gets the relative path to the specified directory as a module specifier.
* @param directory - Directory.
*/
getRelativePathAsModuleSpecifierTo(directory: Directory): string;
getRelativePathAsModuleSpecifierTo(sourceFileDirOrFilePath: SourceFile | Directory | string) {
return this.getDirectory().getRelativePathAsModuleSpecifierTo(sourceFileDirOrFilePath);
}
/**
* Subscribe to when the source file is modified.
* @param subscription - Subscription.
* @param subscribe - Optional and defaults to true. Use an explicit false to unsubscribe.
*/
onModified(subscription: (sender: SourceFile) => void, subscribe = true) {
if (subscribe)
this.#modifiedEventContainer.subscribe(subscription);
else
this.#modifiedEventContainer.unsubscribe(subscription);
return this;
}
/**
* Do an action the next time the source file is modified.
* @param action - Action to run.
* @internal
*/
_doActionPreNextModification(action: () => void) {
const wrappedSubscription = () => {
action();
this.#preModifiedEventContainer.unsubscribe(wrappedSubscription);
};
this.#preModifiedEventContainer.subscribe(wrappedSubscription);
return this;
}
/** @internal */
_firePreModified() {
this.#preModifiedEventContainer.fire(this);
}
/**
* Organizes the imports in the file.
*
* WARNING! This will forget all the nodes in the file! It's best to do this after you're all done with the file.
* @param formatSettings - Format code settings.
* @param userPreferences - User preferences for refactoring.
*/
organizeImports(formatSettings: FormatCodeSettings = {}, userPreferences: UserPreferences = {}) {
this._context.languageService.organizeImports(this, formatSettings, userPreferences).forEach(fileTextChanges => fileTextChanges.applyChanges());
return this;
}
/**
* Removes all unused declarations like interfaces, classes, enums, functions, variables, parameters,
* methods, properties, imports, etc. from this file.
*
* Tip: For optimal results, sometimes this method needs to be called more than once. There could be nodes
* that are only referenced in unused declarations and in this case, another call will also remove them.
*
* WARNING! This will forget all the nodes in the file! It's best to do this after you're all done with the file.
* @param formatSettings - Format code settings.
* @param userPreferences - User preferences for refactoring.
*/
fixUnusedIdentifiers(formatSettings: FormatCodeSettings = {}, userPreferences: UserPreferences = {}) {
this._context.languageService.getCombinedCodeFix(this, "unusedIdentifier_delete", formatSettings, userPreferences).applyChanges();
this._context.languageService.getCombinedCodeFix(this, "unusedIdentifier_deleteImports", formatSettings, userPreferences).applyChanges();
return this;
}
/**
* Code fix to add import declarations for identifiers that are referenced, but not imported in the source file.
* @param formatSettings - Format code settings.
* @param userPreferences - User preferences for refactoring.
*/
fixMissingImports(formatSettings: FormatCodeSettings = {}, userPreferences: UserPreferences = {}) {
const combinedCodeFix = this._context.languageService.getCombinedCodeFix(this, "fixMissingImport", formatSettings, userPreferences);
const sourceFile = this;
for (const fileTextChanges of combinedCodeFix.getChanges()) {
const changes = fileTextChanges.getTextChanges();
removeUnnecessaryDoubleBlankLines(changes);
applyTextChanges(changes);
}
return this;
function removeUnnecessaryDoubleBlankLines(changes: TextChange[]) {
changes.sort((a, b) => a.getSpan().getStart() - b.getSpan().getStart());
// when a file has no imports, it will add a double newline to every change
// so remove them except for the last change
for (let i = 0; i < changes.length - 1; i++) { // skip last change
const { compilerObject } = changes[i];
compilerObject.newText = compilerObject.newText.replace(/(\r?)\n\r?\n$/, "$1\n");
}
}
function applyTextChanges(changes: ReadonlyArray<TextChange>) {
// group all the changes by their start position and insert them into the file
const groups = ArrayUtils.groupBy(changes, change => change.getSpan().getStart());
let addedLength = 0;
for (const group of groups) {
// these should all be import declarations so it should be safe
const insertPos = group[0].getSpan().getStart() + addedLength;
const newText = group.map(item => item.getNewText()).join("");
insertIntoTextRange({
sourceFile,
insertPos,
newText,
});
addedLength += newText.length;
}
}
}
/**
* Applies the text changes to the source file.
*
* WARNING! This will forget all the nodes in the file! It's best to do this after you're all done with the file.
* @param textChanges - Text changes.
*/
applyTextChanges(textChanges: ReadonlyArray<ts.TextChange | TextChange>) {
// do nothing if no changes
if (textChanges.length === 0)
return this;
this.forgetDescendants();
replaceNodeText({
sourceFile: this._sourceFile,
start: 0,
replacingLength: this.getFullWidth(),
newText: getTextFromTextChanges(this, textChanges),
});
return this;
}
/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
*/
set(structure: Partial<SourceFileStructure>) {
callBaseSet(SourceFileBase.prototype, this, structure);
return this;
}
/**
* Gets the structure equivalent to this node.
*/
getStructure(): SourceFileStructure {
return callBaseGetStructure<SourceFileSpecificStructure>(SourceFileBase.prototype, this, {
kind: StructureKind.SourceFile,
});
}
#refreshFromFileSystemInternal(fileReadResult: string | false): FileSystemRefreshResult {
if (fileReadResult === false) {
this.forget();
return FileSystemRefreshResult.Deleted;
}
const fileText = fileReadResult;
if (fileText === this.getFullText())
return FileSystemRefreshResult.NoChange;
this.replaceText([0, this.getEnd()], fileText);
this._setIsSaved(true); // saved when loaded from file system
return FileSystemRefreshResult.Updated;
}