/
SyntheticNamedExportVariable.ts
44 lines (38 loc) · 1.2 KB
/
SyntheticNamedExportVariable.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
import Module, { AstContext } from '../../Module';
import ExportDefaultVariable from './ExportDefaultVariable';
import Variable from './Variable';
export default class SyntheticNamedExportVariable extends Variable {
context: AstContext;
defaultVariable: Variable;
module: Module;
constructor(context: AstContext, name: string, defaultVariable: Variable) {
super(name);
this.context = context;
this.module = context.module;
this.defaultVariable = defaultVariable;
}
getBaseVariable(): Variable {
let baseVariable = this.defaultVariable;
if (baseVariable instanceof ExportDefaultVariable) {
baseVariable = baseVariable.getOriginalVariable();
}
if (baseVariable instanceof SyntheticNamedExportVariable) {
baseVariable = baseVariable.getBaseVariable();
}
return baseVariable;
}
getName(): string {
const name = this.name;
const renderBaseName = this.defaultVariable.getName();
return `${renderBaseName}${getPropertyAccess(name)}`;
}
include() {
if (!this.included) {
this.included = true;
this.context.includeVariable(this.defaultVariable);
}
}
}
const getPropertyAccess = (name: string) => {
return /^(?!\d)[\w$]+$/.test(name) ? `.${name}` : `[${JSON.stringify(name)}]`;
};