-
Notifications
You must be signed in to change notification settings - Fork 195
/
ts-morph-bootstrap.d.ts
397 lines (385 loc) · 16.4 KB
/
ts-morph-bootstrap.d.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
import { RuntimeDirEntry, ts } from "@ts-morph/common";
/**
* Holds the compiler options.
*/
export declare class CompilerOptionsContainer extends SettingsContainer<ts.CompilerOptions> {
constructor(defaultSettings?: ts.CompilerOptions);
/**
* Sets one or all of the compiler options.
*
* WARNING: Setting the compiler options will cause a complete reparse of all the source files.
* @param settings - Compiler options to set.
*/
set(settings: Partial<ts.CompilerOptions>): void;
/**
* Gets the encoding from the compiler options or returns utf-8.
*/
getEncoding(): string;
}
/**
* Represents a file system that can be interacted with.
*/
export interface FileSystemHost {
/** Gets if this file system is case sensitive. */
isCaseSensitive(): boolean;
/** Asynchronously deletes the specified file or directory. */
delete(path: string): Promise<void>;
/** Synchronously deletes the specified file or directory */
deleteSync(path: string): void;
/**
* Reads all the child directories and files.
* @remarks Implementers should have this return the full file path.
*/
readDirSync(dirPath: string): RuntimeDirEntry[];
/** Asynchronously reads a file at the specified path. */
readFile(filePath: string, encoding?: string): Promise<string>;
/** Synchronously reads a file at the specified path. */
readFileSync(filePath: string, encoding?: string): string;
/** Asynchronously writes a file to the file system. */
writeFile(filePath: string, fileText: string): Promise<void>;
/** Synchronously writes a file to the file system. */
writeFileSync(filePath: string, fileText: string): void;
/** Asynchronously creates a directory at the specified path. */
mkdir(dirPath: string): Promise<void>;
/** Synchronously creates a directory at the specified path. */
mkdirSync(dirPath: string): void;
/** Asynchronously moves a file or directory. */
move(srcPath: string, destPath: string): Promise<void>;
/** Synchronously moves a file or directory. */
moveSync(srcPath: string, destPath: string): void;
/** Asynchronously copies a file or directory. */
copy(srcPath: string, destPath: string): Promise<void>;
/** Synchronously copies a file or directory. */
copySync(srcPath: string, destPath: string): void;
/** Asynchronously checks if a file exists.
* @remarks Implementers should throw an `errors.FileNotFoundError` when it does not exist.
*/
fileExists(filePath: string): Promise<boolean>;
/** Synchronously checks if a file exists.
* @remarks Implementers should throw an `errors.FileNotFoundError` when it does not exist.
*/
fileExistsSync(filePath: string): boolean;
/** Asynchronously checks if a directory exists. */
directoryExists(dirPath: string): Promise<boolean>;
/** Synchronously checks if a directory exists. */
directoryExistsSync(dirPath: string): boolean;
/** See https://nodejs.org/api/fs.html#fs_fs_realpathsync_path_options */
realpathSync(path: string): string;
/** Gets the current directory of the environment. */
getCurrentDirectory(): string;
/** Uses pattern matching to find files or directories. */
glob(patterns: ReadonlyArray<string>): Promise<string[]>;
/** Synchronously uses pattern matching to find files or directories. */
globSync(patterns: ReadonlyArray<string>): string[];
}
/** An implementation of a file system that exists in memory only. */
export declare class InMemoryFileSystemHost implements FileSystemHost {
#private;
/**
* Constructor.
*/
constructor();
/** @inheritdoc */
isCaseSensitive(): boolean;
/** @inheritdoc */
delete(path: string): Promise<void>;
/** @inheritdoc */
deleteSync(path: string): void;
/** @inheritdoc */
readDirSync(dirPath: string): RuntimeDirEntry[];
/** @inheritdoc */
readFile(filePath: string, encoding?: string): Promise<string>;
/** @inheritdoc */
readFileSync(filePath: string, encoding?: string): string;
/** @inheritdoc */
writeFile(filePath: string, fileText: string): Promise<void>;
/** @inheritdoc */
writeFileSync(filePath: string, fileText: string): void;
/** @inheritdoc */
mkdir(dirPath: string): Promise<void>;
/** @inheritdoc */
mkdirSync(dirPath: string): void;
/** @inheritdoc */
move(srcPath: string, destPath: string): Promise<void>;
/** @inheritdoc */
moveSync(srcPath: string, destPath: string): void;
/** @inheritdoc */
copy(srcPath: string, destPath: string): Promise<void>;
/** @inheritdoc */
copySync(srcPath: string, destPath: string): void;
/** @inheritdoc */
fileExists(filePath: string): Promise<boolean>;
/** @inheritdoc */
fileExistsSync(filePath: string): boolean;
/** @inheritdoc */
directoryExists(dirPath: string): Promise<boolean>;
/** @inheritdoc */
directoryExistsSync(dirPath: string): boolean;
/** @inheritdoc */
realpathSync(path: string): string;
/** @inheritdoc */
getCurrentDirectory(): string;
/** @inheritdoc */
glob(patterns: ReadonlyArray<string>): Promise<string[]>;
/** @inheritdoc */
globSync(patterns: ReadonlyArray<string>): string[];
}
/** Host for implementing custom module and/or type reference directive resolution. */
export interface ResolutionHost {
resolveModuleNames?: ts.LanguageServiceHost["resolveModuleNames"];
getResolvedModuleWithFailedLookupLocationsFromCache?: ts.LanguageServiceHost["getResolvedModuleWithFailedLookupLocationsFromCache"];
resolveTypeReferenceDirectives?: ts.LanguageServiceHost["resolveTypeReferenceDirectives"];
}
/**
* Factory used to create a resolution host.
* @remarks The compiler options are retrieved via a function in order to get the project's current compiler options.
*/
export type ResolutionHostFactory = (moduleResolutionHost: ts.ModuleResolutionHost, getCompilerOptions: () => ts.CompilerOptions) => ResolutionHost;
/** Collection of reusable resolution hosts. */
export declare const ResolutionHosts: {
deno: ResolutionHostFactory;
};
export declare abstract class SettingsContainer<T extends object> {
#private;
protected _settings: T;
/**
* Constructor.
* @param defaultSettings - The settings to use by default.
*/
constructor(defaultSettings: T);
/**
* Resets the settings to the default.
*/
reset(): void;
/**
* Gets a copy of the settings as an object.
*/
get(): T;
/**
* Sets one or all of the settings.
* @param settings - Settings to set.
*/
set(settings: Partial<T>): void;
/**
* Subscribe to modifications in the settings container.
* @param action - Action to execute when the settings change.
*/
onModified(action: () => void): void;
}
/**
* Asynchronously creates a new collection of source files to analyze.
* @param options Options for creating the project.
*/
export declare function createProject(options?: ProjectOptions): Promise<Project>;
/**
* Synchronously creates a new collection of source files to analyze.
* @param options Options for creating the project.
*/
export declare function createProjectSync(options?: ProjectOptions): Project;
/** Options for creating a project. */
export interface ProjectOptions {
/** Compiler options */
compilerOptions?: ts.CompilerOptions;
/** File path to the tsconfig.json file. */
tsConfigFilePath?: string;
/** Whether to skip adding source files from the specified tsconfig.json. @default false */
skipAddingFilesFromTsConfig?: boolean;
/** Skip resolving file dependencies when providing a ts config file path and adding the files from tsconfig. @default false */
skipFileDependencyResolution?: boolean;
/**
* Skip loading the lib files. Unlike the compiler API, ts-morph does not load these
* from the node_modules folder, but instead loads them from some other JS code
* and uses a fake path for their existence. If you want to use a custom lib files
* folder path, then provide one using the libFolderPath options.
* @default false
*/
skipLoadingLibFiles?: boolean;
/** The folder to use for loading lib files. */
libFolderPath?: string;
/** Whether to use an in-memory file system. */
useInMemoryFileSystem?: boolean;
/**
* Optional file system host. Useful for mocking access to the file system.
* @remarks Consider using `useInMemoryFileSystem` instead.
*/
fileSystem?: FileSystemHost;
/** Creates a resolution host for specifying custom module and/or type reference directive resolution. */
resolutionHost?: ResolutionHostFactory;
/**
* Unstable and will probably be removed in the future.
* I believe this option should be internal to the library and if you know how to achieve
* that then please consider submitting a PR.
*/
isKnownTypesPackageName?: ts.LanguageServiceHost["isKnownTypesPackageName"];
}
/** Project that holds source files. */
export declare class Project {
#private;
private constructor();
/** Gets the compiler options for modification. */
readonly compilerOptions: CompilerOptionsContainer;
/** Gets the file system host used for this project. */
readonly fileSystem: FileSystemHost;
/**
* Asynchronously adds an existing source file from a file path or throws if it doesn't exist.
*
* Will return the source file if it was already added.
* @param filePath - File path to get the file from.
* @param options - Options for adding the file.
* @throws FileNotFoundError when the file is not found.
*/
addSourceFileAtPath(filePath: string, options?: {
scriptKind?: ts.ScriptKind;
}): Promise<ts.SourceFile>;
/**
* Synchronously adds an existing source file from a file path or throws if it doesn't exist.
*
* Will return the source file if it was already added.
* @param filePath - File path to get the file from.
* @param options - Options for adding the file.
* @throws FileNotFoundError when the file is not found.
*/
addSourceFileAtPathSync(filePath: string, options?: {
scriptKind?: ts.ScriptKind;
}): ts.SourceFile;
/**
* Asynchronously adds a source file from a file path if it exists or returns undefined.
*
* Will return the source file if it was already added.
* @param filePath - File path to get the file from.
* @param options - Options for adding the file.
* @skipOrThrowCheck
*/
addSourceFileAtPathIfExists(filePath: string, options?: {
scriptKind?: ts.ScriptKind;
}): Promise<ts.SourceFile | undefined>;
/**
* Synchronously adds a source file from a file path if it exists or returns undefined.
*
* Will return the source file if it was already added.
* @param filePath - File path to get the file from.
* @param options - Options for adding the file.
* @skipOrThrowCheck
*/
addSourceFileAtPathIfExistsSync(filePath: string, options?: {
scriptKind?: ts.ScriptKind;
}): ts.SourceFile | undefined;
/**
* Asynchronously adds source files based on file globs.
* @param fileGlobs - File glob or globs to add files based on.
* @returns The matched source files.
*/
addSourceFilesByPaths(fileGlobs: string | ReadonlyArray<string>): Promise<ts.SourceFile[]>;
/**
* Synchronously adds source files based on file globs.
* @param fileGlobs - File glob or globs to add files based on.
* @returns The matched source files.
* @remarks This is much slower than the asynchronous version.
*/
addSourceFilesByPathsSync(fileGlobs: string | ReadonlyArray<string>): ts.SourceFile[];
/**
* Asynchronously adds all the source files from the specified tsconfig.json.
*
* Note that this is done by default when specifying a tsconfig file in the constructor and not explicitly setting the
* `skipAddingSourceFilesFromTsConfig` option to `true`.
* @param tsConfigFilePath - File path to the tsconfig.json file.
*/
addSourceFilesFromTsConfig(tsConfigFilePath: string): Promise<ts.SourceFile[]>;
/**
* Synchronously adds all the source files from the specified tsconfig.json.
*
* Note that this is done by default when specifying a tsconfig file in the constructor and not explicitly setting the
* `skipAddingSourceFilesFromTsConfig` option to `true`.
* @param tsConfigFilePath - File path to the tsconfig.json file.
*/
addSourceFilesFromTsConfigSync(tsConfigFilePath: string): ts.SourceFile[];
/**
* Creates a source file at the specified file path with the specified text.
*
* Note: The file will not be created and saved to the file system until .save() is called on the source file.
* @param filePath - File path of the source file.
* @param sourceFileText - Text to use for the source file.
* @param options - Options.
* @throws - InvalidOperationError if a source file already exists at the provided file path.
*/
createSourceFile(filePath: string, sourceFileText?: string, options?: {
scriptKind?: ts.ScriptKind;
}): ts.SourceFile;
/**
* Updates the source file stored in the project at the specified path.
* @param filePath - File path of the source file.
* @param sourceFileText - Text of the source file.
* @param options - Options for updating the source file.
*/
updateSourceFile(filePath: string, sourceFileText: string, options?: {
scriptKind?: ts.ScriptKind;
}): ts.SourceFile;
/**
* Updates the source file stored in the project. The `fileName` of the source file object is used to tell which file to update.
* @param newSourceFile - The new source file.
*/
updateSourceFile(newSourceFile: ts.SourceFile): ts.SourceFile;
/**
* Removes the source file at the provided file path.
* @param filePath - File path of the source file.
*/
removeSourceFile(filePath: string): void;
/**
* Removes the provided source file based on its `fileName`.
* @param sourceFile - Source file to remove.
*/
removeSourceFile(sourceFile: ts.SourceFile): void;
/**
* Adds the source files the project's source files depend on to the project.
* @remarks
* * This should be done after source files are added to the project, preferably once to
* avoid doing more work than necessary.
* * This is done by default when creating a Project and providing a tsconfig.json and
* not specifying to not add the source files.
*/
resolveSourceFileDependencies(): void;
/**
* Creates a new program.
* Note: You should get a new program any time source files are added, removed, or changed.
*/
createProgram(options?: ts.CreateProgramOptions): ts.Program;
/**
* Gets the language service.
*/
getLanguageService(): ts.LanguageService;
/**
* Gets a source file by a file name or file path. Throws an error if it doesn't exist.
* @param fileNameOrPath - File name or path that the path could end with or equal.
*/
getSourceFileOrThrow(fileNameOrPath: string): ts.SourceFile;
/**
* Gets a source file by a search function. Throws an error if it doesn't exist.
* @param searchFunction - Search function.
*/
getSourceFileOrThrow(searchFunction: (file: ts.SourceFile) => boolean): ts.SourceFile;
/**
* Gets a source file by a file name or file path. Returns undefined if none exists.
* @param fileNameOrPath - File name or path that the path could end with or equal.
*/
getSourceFile(fileNameOrPath: string): ts.SourceFile | undefined;
/**
* Gets a source file by a search function. Returns undefined if none exists.
* @param searchFunction - Search function.
*/
getSourceFile(searchFunction: (file: ts.SourceFile) => boolean): ts.SourceFile | undefined;
/** Gets the source files in the project. */
getSourceFiles(): ts.SourceFile[];
/**
* Formats an array of diagnostics with their color and context into a string.
* @param diagnostics - Diagnostics to get a string of.
* @param options - Collection of options. For example, the new line character to use (defaults to the OS' new line character).
*/
formatDiagnosticsWithColorAndContext(diagnostics: ReadonlyArray<ts.Diagnostic>, opts?: {
newLineChar?: "\n" | "\r\n";
}): string;
/**
* Gets a ts.ModuleResolutionHost for the project.
*/
getModuleResolutionHost(): ts.ModuleResolutionHost;
}
export { ts };