forked from dsherret/ts-morph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExportSpecifier.ts
201 lines (172 loc) · 6.19 KB
/
ExportSpecifier.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
import * as errors from "../../../errors";
import { insertIntoParentTextRange, removeCommaSeparatedChild, removeChildren } from "../../../manipulation";
import { SyntaxKind, ts } from "../../../typescript";
import { StringUtils } from "../../../utils";
import { LocalTargetDeclarations } from "../aliases";
import { Node } from "../common";
import { Symbol } from "../../symbols";
import { callBaseGetStructure } from "../callBaseGetStructure";
import { callBaseSet } from "../callBaseSet";
import { ExportSpecifierStructure, ExportSpecifierSpecificStructure, StructureKind } from "../../../structures";
// todo: There's a lot of common code that could be shared with ImportSpecifier. It could be moved to a mixin.
export const ExportSpecifierBase = Node;
export class ExportSpecifier extends ExportSpecifierBase<ts.ExportSpecifier> {
/**
* Sets the name of what's being exported.
*/
setName(name: string) {
const nameNode = this.getNameNode();
if (nameNode.getText() === name)
return this;
nameNode.replaceWithText(name);
return this;
}
/**
* Gets the name of the export specifier.
*/
getName() {
return this.getNameNode().getText();
}
/**
* Gets the name node of what's being exported.
*/
getNameNode() {
return this._getNodeFromCompilerNode(this.compilerNode.propertyName || this.compilerNode.name);
}
/**
* Sets the alias for the name being exported 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 export 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 export 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 export declaration associated with this export specifier.
*/
getExportDeclaration() {
return this.getFirstAncestorByKindOrThrow(SyntaxKind.ExportDeclaration);
}
/**
* Gets the local target symbol of the export specifier or throws if it doesn't exist.
*/
getLocalTargetSymbolOrThrow() {
return errors.throwIfNullOrUndefined(this.getLocalTargetSymbol(), `The export specifier's local target symbol was expected.`);
}
/**
* Gets the local target symbol of the export specifier or undefined if it doesn't exist.
*/
getLocalTargetSymbol(): Symbol | undefined {
return this._context.typeChecker.getExportSpecifierLocalTargetSymbol(this);
}
/**
* Gets all the declarations referenced by the export specifier.
*/
getLocalTargetDeclarations(): LocalTargetDeclarations[] {
const symbol = this.getLocalTargetSymbol();
return symbol == null ? [] : symbol.getDeclarations() as LocalTargetDeclarations[];
}
/**
* Removes the export specifier.
*/
remove() {
const exportDeclaration = this.getExportDeclaration();
const exports = exportDeclaration.getNamedExports();
if (exports.length > 1)
removeCommaSeparatedChild(this);
else if (exportDeclaration.hasModuleSpecifier())
exportDeclaration.toNamespaceExport();
else
exportDeclaration.remove();
}
/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
*/
set(structure: Partial<ExportSpecifierStructure>) {
callBaseSet(ExportSpecifierBase.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(): ExportSpecifierStructure {
const alias = this.getAliasNode();
return callBaseGetStructure<ExportSpecifierSpecificStructure>(Node.prototype, this, {
kind: StructureKind.ExportSpecifier,
alias: alias ? alias.getText() : undefined,
name: this.getNameNode().getText()
});
}
}