Skip to content

Commit

Permalink
Move logic from ClassBody into ClassNode
Browse files Browse the repository at this point in the history
So that it sits in one place and is easier to extend.
  • Loading branch information
marijnh committed Mar 26, 2021
1 parent 8ad9bd7 commit c3b76f7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 33 deletions.
27 changes: 0 additions & 27 deletions src/ast/nodes/ClassBody.ts
@@ -1,8 +1,5 @@
import { CallOptions } from '../CallOptions';
import { HasEffectsContext } from '../ExecutionContext';
import ClassBodyScope from '../scopes/ClassBodyScope';
import Scope from '../scopes/Scope';
import { EMPTY_PATH, ObjectPath } from '../utils/PathTracker';
import MethodDefinition from './MethodDefinition';
import * as NodeType from './NodeType';
import PropertyDefinition from './PropertyDefinition';
Expand All @@ -12,31 +9,7 @@ export default class ClassBody extends NodeBase {
body!: (MethodDefinition | PropertyDefinition)[];
type!: NodeType.tClassBody;

private classConstructor!: MethodDefinition | null;

createScope(parentScope: Scope) {
this.scope = new ClassBodyScope(parentScope);
}

hasEffectsWhenCalledAtPath(
path: ObjectPath,
callOptions: CallOptions,
context: HasEffectsContext
) {
if (path.length > 0) return true;
return (
this.classConstructor !== null &&
this.classConstructor.hasEffectsWhenCalledAtPath(EMPTY_PATH, callOptions, context)
);
}

initialise() {
for (const method of this.body) {
if (method instanceof MethodDefinition && method.kind === 'constructor') {
this.classConstructor = method;
return;
}
}
this.classConstructor = null;
}
}
21 changes: 15 additions & 6 deletions src/ast/nodes/shared/ClassNode.ts
Expand Up @@ -2,15 +2,17 @@ import { CallOptions } from '../../CallOptions';
import { HasEffectsContext } from '../../ExecutionContext';
import ChildScope from '../../scopes/ChildScope';
import Scope from '../../scopes/Scope';
import { ObjectPath } from '../../utils/PathTracker';
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);
Expand All @@ -31,17 +33,24 @@ export default class ClassNode extends NodeBase {
callOptions: CallOptions,
context: HasEffectsContext
) {
if (!callOptions.withNew) return true;
return (
this.body.hasEffectsWhenCalledAtPath(path, callOptions, context) ||
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))
);
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;
}
}

0 comments on commit c3b76f7

Please sign in to comment.