-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ExportDefaultVariable.ts
81 lines (68 loc) · 2.4 KB
/
ExportDefaultVariable.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
import { AstContext } from '../../Module';
import ClassDeclaration from '../nodes/ClassDeclaration';
import ExportDefaultDeclaration from '../nodes/ExportDefaultDeclaration';
import FunctionDeclaration from '../nodes/FunctionDeclaration';
import Identifier from '../nodes/Identifier';
import * as NodeType from '../nodes/NodeType';
import LocalVariable from './LocalVariable';
import Variable from './Variable';
export function isExportDefaultVariable(variable: Variable): variable is ExportDefaultVariable {
return variable.isDefault;
}
export default class ExportDefaultVariable extends LocalVariable {
isDefault: true;
hasId: boolean;
// Not initialised during construction
private originalId: Identifier | null = null;
constructor(
name: string,
exportDefaultDeclaration: ExportDefaultDeclaration,
context: AstContext
) {
super(name, exportDefaultDeclaration, exportDefaultDeclaration.declaration, context);
const declaration = exportDefaultDeclaration.declaration;
if (
(declaration.type === NodeType.FunctionDeclaration ||
declaration.type === NodeType.ClassDeclaration) &&
(<FunctionDeclaration | ClassDeclaration>declaration).id
) {
this.hasId = true;
this.originalId = (<FunctionDeclaration | ClassDeclaration>declaration).id;
} else if (declaration.type === NodeType.Identifier) {
this.originalId = <Identifier>declaration;
}
}
addReference(identifier: Identifier) {
if (!this.hasId) {
this.name = identifier.name;
}
}
getName() {
return this.referencesOriginal() ? this.originalId.variable.getName() : super.getName();
}
getOriginalVariable(): Variable | null {
return (this.originalId && this.originalId.variable) || null;
}
getOriginalVariableName(): string | null {
return (this.originalId && this.originalId.name) || null;
}
referencesOriginal() {
return this.originalId && (this.hasId || !this.originalId.variable.isReassigned);
}
setRenderNames(baseName: string | null, name: string | null) {
if (this.referencesOriginal()) {
this.originalId.variable.setRenderNames(baseName, name);
} else {
super.setRenderNames(baseName, name);
}
}
setSafeName(name: string | null) {
if (this.referencesOriginal()) {
this.originalId.variable.setSafeName(name);
} else {
super.setSafeName(name);
}
}
}
ExportDefaultVariable.prototype.getBaseVariableName = ExportDefaultVariable.prototype.getName;
ExportDefaultVariable.prototype.isDefault = true;