-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ClassNode.ts
56 lines (50 loc) · 1.67 KB
/
ClassNode.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
import { CallOptions } from '../../CallOptions';
import { HasEffectsContext } from '../../ExecutionContext';
import ChildScope from '../../scopes/ChildScope';
import Scope from '../../scopes/Scope';
import { EMPTY_PATH, ObjectPath } from '../../utils/PathTracker';
import ClassBody from '../ClassBody';
import Identifier from '../Identifier';
import MethodDefinition from '../MethodDefinition';
import { ExpressionNode, NodeBase } from './Node';
export default class ClassNode extends NodeBase {
body!: ClassBody;
id!: Identifier | null;
superClass!: ExpressionNode | null;
private classConstructor!: MethodDefinition | null;
createScope(parentScope: Scope) {
this.scope = new ChildScope(parentScope);
}
hasEffectsWhenAccessedAtPath(path: ObjectPath) {
if (path.length <= 1) return false;
return path.length > 2 || path[0] !== 'prototype';
}
hasEffectsWhenAssignedAtPath(path: ObjectPath) {
if (path.length <= 1) return false;
return path.length > 2 || path[0] !== 'prototype';
}
hasEffectsWhenCalledAtPath(
path: ObjectPath,
callOptions: CallOptions,
context: HasEffectsContext
) {
return !callOptions.withNew ||
path.length > 0 ||
(this.classConstructor !== null &&
this.classConstructor.hasEffectsWhenCalledAtPath(EMPTY_PATH, callOptions, context)) ||
(this.superClass !== null &&
this.superClass.hasEffectsWhenCalledAtPath(path, callOptions, context));
}
initialise() {
if (this.id !== null) {
this.id.declare('class', this);
}
for (const method of this.body.body) {
if (method instanceof MethodDefinition && method.kind === 'constructor') {
this.classConstructor = method;
return;
}
}
this.classConstructor = null;
}
}