Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add variable declaration node #16

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/components/EventLoop/useProcessEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ const processTask = async ({
const expression = nodeFactory({
node: (node.node as ArrowFunctionExpression).body,
context: {
...node.context,
actions: [],
functions: node.context.functions,
},
params: node.params,
});
Expand Down Expand Up @@ -77,8 +77,8 @@ const processMicroTask = async ({
const expression = nodeFactory({
node: (node.node as ArrowFunctionExpression).body,
context: {
...node.context,
actions: [],
functions: node.context.functions,
},
params: node.params,
});
Expand Down Expand Up @@ -118,8 +118,8 @@ const processRender = async ({
const expression = nodeFactory({
node: (node.node as ArrowFunctionExpression).body,
context: {
...node.context,
actions: [],
functions: node.context.functions,
},
params: node.params,
});
Expand Down
21 changes: 21 additions & 0 deletions src/utils/nodes/AssignmentExpression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { AssignmentExpression, Identifier } from 'acorn';
import { NodeClass, NodeClassConstructor } from './Node.abstract.ts';

export class AssignmentExpressionClass extends NodeClass {
constructor(params: NodeClassConstructor) {
super(params);
}

serialize = () => {
console.log(
'Serialize method is not implemented for the Assignment expressions'
);
return '';
};

traverse = () => {
const assignmentExpression = this.node as AssignmentExpression;
this.context.variables[(assignmentExpression.left as Identifier).name] =
assignmentExpression.right;
};
}
9 changes: 9 additions & 0 deletions src/utils/nodes/Identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ export class IdentifierClass extends NodeClass {
}),
});
return;
} else if (this.context.variables[identifier.name]) {
const variableValue = this.context.variables[identifier.name];
if (!variableValue) return;
const blockStatement = nodeFactory({
node: variableValue,
context: this.context,
});
blockStatement.traverse();
return;
} else if (this.context.functions[identifier.name]) {
const customFunction = this.context.functions[
identifier.name
Expand Down
24 changes: 17 additions & 7 deletions src/utils/nodes/MemberExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,23 @@ export class MemberExpressionClass extends NodeClass {
type: 'push',
value:
this.args
?.map((arg) =>
nodeFactory({
node: arg,
context: this.context,
params: this.params,
}).serialize()
)
?.map((arg) => {
if (arg.type === 'Identifier') {
const variableValue = this.context.variables[arg.name];
if (!variableValue) return '';
vault-developer marked this conversation as resolved.
Show resolved Hide resolved
const literal = nodeFactory({
node: variableValue,
context: this.context,
}).serialize();
return literal;
} else {
return nodeFactory({
node: arg,
context: this.context,
params: this.params,
}).serialize();
}
})
.join(',') ?? '',
});
}
Expand Down
22 changes: 22 additions & 0 deletions src/utils/nodes/VariableDeclaration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { VariableDeclaration, Identifier } from 'acorn';
import { NodeClass, NodeClassConstructor } from './Node.abstract.ts';

export class VariableDeclarationClass extends NodeClass {
constructor(params: NodeClassConstructor) {
super(params);
}

serialize = () => {
console.log(
'Serialize method is not implemented for the Variable declarations'
);
return '';
};

traverse = () => {
const variableDeclaration = this.node as VariableDeclaration;
this.context.variables[
(variableDeclaration.declarations[0].id as Identifier).name
] = variableDeclaration.declarations[0].init;
};
}
6 changes: 6 additions & 0 deletions src/utils/nodes/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { ArrowFunctionExpressionClass } from './ArrowFunctionExpression.ts';
import { NodeClass, NodeClassConstructor } from './Node.abstract.ts';
import { BlockStatementClass } from './BlockStatement.ts';
import { ProgramClass } from './Program.ts';
import { VariableDeclarationClass } from './VariableDeclaration.ts';
import { AssignmentExpressionClass } from './AssignmentExpression.ts';

export const nodeFactory = (params: NodeClassConstructor): NodeClass => {
switch (params.node.type) {
Expand All @@ -30,6 +32,10 @@ export const nodeFactory = (params: NodeClassConstructor): NodeClass => {
return new BlockStatementClass(params);
case 'Program':
return new ProgramClass(params);
case 'VariableDeclaration':
return new VariableDeclarationClass(params);
case 'AssignmentExpression':
return new AssignmentExpressionClass(params);
default:
return new NotImplementedNodeClass(params);
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const parse = (code: string): NodeClass => {
const context: ParseContextInterface = {
actions: [],
functions: {},
variables: {},
};
const parsed = acornParse(code, { ecmaVersion: 2020 });
return nodeFactory({ node: parsed, context });
Expand Down
1 change: 1 addition & 0 deletions src/utils/parse.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ActionInterface } from '../store/store.types.ts';
export interface ParseContextInterface {
actions: ActionInterface[];
functions: Record<string, FunctionDeclaration>;
variables: Record<string, Expression | null | undefined>;
}

export type AcornArgument = Expression | SpreadElement;
Expand Down