Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
8 changes: 6 additions & 2 deletions src/components/Editor/Editor.data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ foo1();`,
{
title: 'microtasks',
code: `console.log(1);
setTimeout(() => console.log(2), 0);
queueMicrotask(() => console.log(3));
console.log(2-1);
Copy link
Owner

@vault-developer vault-developer Nov 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 suggestion:
Adding 5 additional binary expression examples to the "microtasks" example may create confusion as they are not related to microtasks.
What do you think about adding some of them to "everything" instead?

console.log(4*8);
console.log(9/3);
console.log(10%5);
setTimeout(() => console.log(2+4), 0);
queueMicrotask(() => console.log(3+9));
Promise.resolve().then(() => {
console.log(4);
});
Expand Down
31 changes: 31 additions & 0 deletions src/utils/nodes/BinaryExpression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { NodeClass, NodeClassConstructor } from './Node.abstract';

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

serialize = () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const binaryExpression = this.node as any;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const binaryExpression = this.node as any;
import {BinaryExpression} from "acorn";
const binaryExpression = this.node as BinaryExpression;

const left = binaryExpression.left.value;
const right = binaryExpression.right.value;
const operator = binaryExpression.operator;
switch (operator) {
Copy link
Owner

@vault-developer vault-developer Nov 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 suggestion:
I suggest to handle all possible operators, you can check types in acorn:
export type BinaryOperator = "==" | "!=" | "===" | "!==" | "<" | "<=" | ">" | ">=" | "<<" | ">>" | ">>>" | "+" | "-" | "*" | "/" | "%" | "|" | "^" | "&" | "in" | "instanceof" | "**"

case '+':
return left + right;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔨 issue(blocking):
There is no guarantee that binaryExpression left and right value is a number.
For example, this expression will send NaN to console:
console.log(2+(8+3));

image

case '-':
return left - right;
case '/':
return left / right;
case '*':
return left * right;
case '%':
return left % right;
default:
return 'operator not supported';
}
};

traverse = () => {};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 suggestion(blocking):
I suggest to traverse binaryExpression members further.
Otherwise, they won't be properly managed by the event loop.
Example:
console.log(1 + console.log(1));

}
15 changes: 9 additions & 6 deletions src/utils/nodes/factory.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { ExpressionStatementClass } from './ExpressionStatement.ts';
import { ArrowFunctionExpressionClass } from './ArrowFunctionExpression.ts';
import { AssignmentExpressionClass } from './AssignmentExpression.ts';
import { BinaryExpressionClass } from './BinaryExpression.ts';
import { BlockStatementClass } from './BlockStatement.ts';
import { CallExpressionClass } from './CallExpression.ts';
import { ExpressionStatementClass } from './ExpressionStatement.ts';
import { FunctionDeclarationClass } from './FunctionDeclaration.ts';
import { IdentifierClass } from './Identifier.ts';
import { NotImplementedNodeClass } from './NotImplemented.ts';
import { MemberExpressionClass } from './MemberExpression.ts';
import { LiteralClass } from './Literal.ts';
import { ArrowFunctionExpressionClass } from './ArrowFunctionExpression.ts';
import { MemberExpressionClass } from './MemberExpression.ts';
import { NodeClass, NodeClassConstructor } from './Node.abstract.ts';
import { BlockStatementClass } from './BlockStatement.ts';
import { NotImplementedNodeClass } from './NotImplemented.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 @@ -36,6 +37,8 @@ export const nodeFactory = (params: NodeClassConstructor): NodeClass => {
return new VariableDeclarationClass(params);
case 'AssignmentExpression':
return new AssignmentExpressionClass(params);
case 'BinaryExpression':
return new BinaryExpressionClass(params);
default:
return new NotImplementedNodeClass(params);
}
Expand Down
Loading