forked from dsherret/ts-morph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImportEqualsDeclaration.ts
65 lines (59 loc) · 2.75 KB
/
ImportEqualsDeclaration.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
import * as errors from "../../../errors";
import { ts } from "../../../typescript";
import { TypeGuards } from "../../../utils";
import { ModuleReference } from "../aliases";
import { JSDocableNode, NamedNode } from "../base";
import { Statement } from "../statement";
import { SourceFile } from "./SourceFile";
export const ImportEqualsDeclarationBase = JSDocableNode(NamedNode(Statement));
export class ImportEqualsDeclaration extends ImportEqualsDeclarationBase<ts.ImportEqualsDeclaration> {
/**
* Gets the module reference of the import equals declaration.
*/
getModuleReference(): ModuleReference {
return this._getNodeFromCompilerNode(this.compilerNode.moduleReference);
}
/**
* Gets if the external module reference is relative.
*/
isExternalModuleReferenceRelative() {
const moduleReference = this.getModuleReference();
if (!TypeGuards.isExternalModuleReference(moduleReference))
return false;
return moduleReference.isRelative();
}
/**
* Sets the external module reference.
* @param externalModuleReference - External module reference as a string.
*/
setExternalModuleReference(externalModuleReference: string): this;
/**
* Sets the external module reference.
* @param sourceFile - Source file to set the external module reference to.
*/
setExternalModuleReference(sourceFile: SourceFile): this;
setExternalModuleReference(textOrSourceFile: string | SourceFile) {
const text = typeof textOrSourceFile === "string" ? textOrSourceFile : this._sourceFile.getRelativePathAsModuleSpecifierTo(textOrSourceFile);
const moduleReference = this.getModuleReference();
if (TypeGuards.isExternalModuleReference(moduleReference) && moduleReference.getExpression() != null)
moduleReference.getExpressionOrThrow().replaceWithText(writer => writer.quote(text));
else
moduleReference.replaceWithText(writer => writer.write("require(").quote(text).write(")"));
return this;
}
/**
* Gets the source file referenced in the external module reference or throws if it doesn't exist.
*/
getExternalModuleReferenceSourceFileOrThrow() {
return errors.throwIfNullOrUndefined(this.getExternalModuleReferenceSourceFile(), "Expected to find an external module reference's referenced source file.");
}
/**
* Gets the source file referenced in the external module reference or returns undefined if it doesn't exist.
*/
getExternalModuleReferenceSourceFile() {
const moduleReference = this.getModuleReference();
if (!TypeGuards.isExternalModuleReference(moduleReference))
return undefined;
return moduleReference.getReferencedSourceFile();
}
}