forked from dsherret/ts-morph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImportSpecifier.ts
174 lines (148 loc) · 5.09 KB
/
ImportSpecifier.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
import { insertIntoParentTextRange, removeCommaSeparatedChild, removeChildren } from "../../../manipulation";
import { SyntaxKind, ts } from "../../../typescript";
import { StringUtils } from "../../../utils";
import { Node } from "../common";
import { callBaseGetStructure } from "../callBaseGetStructure";
import { callBaseSet } from "../callBaseSet";
import { ImportSpecifierStructure } from "../../../structures";
// todo: There's a lot of common code that could be shared with ExportSpecifier. It could be moved to a mixin.
export const ImportSpecifierBase = Node;
export class ImportSpecifier extends ImportSpecifierBase<ts.ImportSpecifier> {
/**
* Sets the identifier being imported.
* @param name - Name being imported.
*/
setName(name: string) {
const nameNode = this.getNameNode();
if (nameNode.getText() === name)
return this;
nameNode.replaceWithText(name);
return this;
}
/**
* Gets the name of the import specifier.
*/
getName() {
return this.getNameNode().getText();
}
/**
* Gets the name node of what's being imported.
*/
getNameNode() {
return this._getNodeFromCompilerNode(this.compilerNode.propertyName || this.compilerNode.name);
}
/**
* Sets the alias for the name being imported and renames all the usages.
* @param alias - Alias to set.
*/
renameAlias(alias: string) {
if (StringUtils.isNullOrWhitespace(alias)) {
this.removeAliasWithRename();
return this;
}
let aliasIdentifier = this.getAliasNode();
if (aliasIdentifier == null) {
// trick is to insert an alias with the same name, then rename the alias. TS compiler will take care of the rest.
this.setAlias(this.getName());
aliasIdentifier = this.getAliasNode()!;
}
aliasIdentifier.rename(alias);
return this;
}
/**
* Sets the alias without renaming all the usages.
* @param alias - Alias to set.
*/
setAlias(alias: string) {
if (StringUtils.isNullOrWhitespace(alias)) {
this.removeAlias();
return this;
}
const aliasIdentifier = this.getAliasNode();
if (aliasIdentifier == null) {
insertIntoParentTextRange({
insertPos: this.getNameNode().getEnd(),
parent: this,
newText: ` as ${alias}`
});
}
else
aliasIdentifier.replaceWithText(alias);
return this;
}
/**
* Removes the alias without renaming.
* @remarks Use removeAliasWithRename() if you want it to rename any usages to the name of the import specifier.
*/
removeAlias() {
const aliasIdentifier = this.getAliasNode();
if (aliasIdentifier == null)
return this;
removeChildren({
children: [this.getFirstChildByKindOrThrow(SyntaxKind.AsKeyword), aliasIdentifier],
removePrecedingSpaces: true,
removePrecedingNewLines: true
});
return this;
}
/**
* Removes the alias and renames any usages to the name of the import specifier.
*/
removeAliasWithRename() {
const aliasIdentifier = this.getAliasNode();
if (aliasIdentifier == null)
return this;
aliasIdentifier.rename(this.getName());
this.removeAlias();
return this;
}
/**
* Gets the alias identifier, if it exists.
*/
getAliasNode() {
if (this.compilerNode.propertyName == null)
return undefined;
return this._getNodeFromCompilerNode(this.compilerNode.name);
}
/**
* Gets the import declaration associated with this import specifier.
*/
getImportDeclaration() {
return this.getFirstAncestorByKindOrThrow(SyntaxKind.ImportDeclaration);
}
/**
* Remove the import specifier.
*/
remove() {
const importDeclaration = this.getImportDeclaration();
const namedImports = importDeclaration.getNamedImports();
if (namedImports.length > 1)
removeCommaSeparatedChild(this);
else
importDeclaration.removeNamedImports();
}
/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
*/
set(structure: Partial<ImportSpecifierStructure>) {
callBaseSet(ImportSpecifierBase.prototype, this, structure);
if (structure.name != null)
this.setName(structure.name);
if (structure.alias != null)
this.setAlias(structure.alias);
else if (structure.hasOwnProperty(nameof(structure.alias)))
this.removeAlias();
return this;
}
/**
* Gets the structure equivalent to this node.
*/
getStructure() {
const alias = this.getAliasNode();
return callBaseGetStructure<ImportSpecifierStructure>(ImportSpecifierBase.prototype, this, {
name: this.getName(),
alias: alias ? alias.getText() : undefined
}) as ImportSpecifierStructure;
}
}