-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
SyntheticNamedExportVariable.ts
38 lines (32 loc) · 1.08 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
import Module, { AstContext } from '../../Module';
import { InclusionContext } from '../ExecutionContext';
import ExportDefaultVariable from './ExportDefaultVariable';
import Variable from './Variable';
export default class SyntheticNamedExportVariable extends Variable {
context: AstContext;
defaultVariable: ExportDefaultVariable;
module: Module;
constructor(context: AstContext, name: string, defaultVariable: ExportDefaultVariable) {
super(name);
this.context = context;
this.module = context.module;
this.defaultVariable = defaultVariable;
}
getName(): string {
const name = this.name;
const renderBaseName = this.defaultVariable.getName();
return `${renderBaseName}${getPropertyAccess(name)}`;
}
getOriginalVariable(): Variable {
return this.defaultVariable.getOriginalVariable();
}
include(context: InclusionContext) {
if (!this.included) {
this.included = true;
this.context.includeVariable(context, this.defaultVariable);
}
}
}
const getPropertyAccess = (name: string) => {
return /^(?!\d)[\w$]+$/.test(name) ? `.${name}` : `[${JSON.stringify(name)}]`;
};