/
ModuleScope.ts
59 lines (52 loc) · 1.98 KB
/
ModuleScope.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
import { AstContext } from '../../Module';
import { InternalModuleFormat } from '../../rollup/types';
import ExportDefaultDeclaration from '../nodes/ExportDefaultDeclaration';
import { UNDEFINED_EXPRESSION } from '../values';
import ExportDefaultVariable from '../variables/ExportDefaultVariable';
import GlobalVariable from '../variables/GlobalVariable';
import LocalVariable from '../variables/LocalVariable';
import Variable from '../variables/Variable';
import ChildScope from './ChildScope';
import GlobalScope from './GlobalScope';
export default class ModuleScope extends ChildScope {
context: AstContext;
declare parent: GlobalScope;
constructor(parent: GlobalScope, context: AstContext) {
super(parent);
this.context = context;
this.variables.set('this', new LocalVariable('this', null, UNDEFINED_EXPRESSION, context));
}
addExportDefaultDeclaration(
name: string,
exportDefaultDeclaration: ExportDefaultDeclaration,
context: AstContext
): ExportDefaultVariable {
const variable = new ExportDefaultVariable(name, exportDefaultDeclaration, context);
this.variables.set('default', variable);
return variable;
}
addNamespaceMemberAccess(): void {}
deconflict(
format: InternalModuleFormat,
exportNamesByVariable: ReadonlyMap<Variable, readonly string[]>,
accessedGlobalsByScope: ReadonlyMap<ChildScope, ReadonlySet<string>>
): void {
// all module level variables are already deconflicted when deconflicting the chunk
for (const scope of this.children)
scope.deconflict(format, exportNamesByVariable, accessedGlobalsByScope);
}
findLexicalBoundary(): this {
return this;
}
findVariable(name: string): Variable {
const knownVariable = this.variables.get(name) || this.accessedOutsideVariables.get(name);
if (knownVariable) {
return knownVariable;
}
const variable = this.context.traceVariable(name) || this.parent.findVariable(name);
if (variable instanceof GlobalVariable) {
this.accessedOutsideVariables.set(name, variable);
}
return variable;
}
}