forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThisExpression.ts
71 lines (63 loc) · 2 KB
/
ThisExpression.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
import type MagicString from 'magic-string';
import { LOGLEVEL_WARN } from '../../utils/logging';
import { logThisIsUndefined } from '../../utils/logs';
import type { HasEffectsContext } from '../ExecutionContext';
import type { NodeInteraction } from '../NodeInteractions';
import { INTERACTION_ACCESSED } from '../NodeInteractions';
import ModuleScope from '../scopes/ModuleScope';
import type { ObjectPath, PathTracker } from '../utils/PathTracker';
import type Variable from '../variables/Variable';
import type * as NodeType from './NodeType';
import { NodeBase } from './shared/Node';
export default class ThisExpression extends NodeBase {
declare type: NodeType.tThisExpression;
declare variable: Variable;
private declare alias: string | null;
bind(): void {
this.variable = this.scope.findVariable('this');
}
deoptimizeArgumentsOnInteractionAtPath(
interaction: NodeInteraction,
path: ObjectPath,
recursionTracker: PathTracker
): void {
this.variable.deoptimizeArgumentsOnInteractionAtPath(interaction, path, recursionTracker);
}
deoptimizePath(path: ObjectPath): void {
this.variable.deoptimizePath(path);
}
hasEffectsOnInteractionAtPath(
path: ObjectPath,
interaction: NodeInteraction,
context: HasEffectsContext
): boolean {
if (path.length === 0) {
return interaction.type !== INTERACTION_ACCESSED;
}
return this.variable.hasEffectsOnInteractionAtPath(path, interaction, context);
}
include(): void {
if (!this.included) {
this.included = true;
this.scope.context.includeVariableInModule(this.variable);
}
}
initialise(): void {
super.initialise();
this.alias =
this.scope.findLexicalBoundary() instanceof ModuleScope
? this.scope.context.moduleContext
: null;
if (this.alias === 'undefined') {
this.scope.context.log(LOGLEVEL_WARN, logThisIsUndefined(), this.start);
}
}
render(code: MagicString): void {
if (this.alias !== null) {
code.overwrite(this.start, this.end, this.alias, {
contentOnly: false,
storeName: true
});
}
}
}