/
ThisExpression.ts
54 lines (47 loc) · 1.63 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
import MagicString from 'magic-string';
import { RenderOptions } from '../../utils/renderHelpers';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import ModuleScope from '../scopes/ModuleScope';
import { ObjectPath } from '../values';
import ThisVariable from '../variables/ThisVariable';
import * as NodeType from './NodeType';
import { NodeBase } from './shared/Node';
export default class ThisExpression extends NodeBase {
type: NodeType.tThisExpression;
variable: ThisVariable;
private alias: string | null;
bind() {
super.bind();
this.variable = <ThisVariable>this.scope.findVariable('this');
}
hasEffectsWhenAccessedAtPath(path: ObjectPath, options: ExecutionPathOptions): boolean {
return path.length > 0 && this.variable.hasEffectsWhenAccessedAtPath(path, options);
}
hasEffectsWhenAssignedAtPath(path: ObjectPath, options: ExecutionPathOptions): boolean {
return this.variable.hasEffectsWhenAssignedAtPath(path, options);
}
initialise() {
this.included = false;
this.variable = null;
this.alias =
this.scope.findLexicalBoundary() instanceof ModuleScope ? this.context.moduleContext : null;
if (this.alias === 'undefined') {
this.context.warn(
{
code: 'THIS_IS_UNDEFINED',
message: `The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten`,
url: `https://rollupjs.org/guide/en#error-this-is-undefined`
},
this.start
);
}
}
render(code: MagicString, _options: RenderOptions) {
if (this.alias !== null) {
code.overwrite(this.start, this.end, this.alias, {
storeName: true,
contentOnly: false
});
}
}
}