<refactor> Refactor Node to standardised object#28
Merged
Conversation
Node class contains properties: - type - content - leftNode - rightNode - parent - commutative - precedence
Previously, expressionToComponentList wasn't able to use the new Node class, for a few reasons: - The Node class needs to be defined before it is accessed (see the Temporal Dead Zone https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#temporal_dead_zone_tdz) - Move Node class to a new file - Add private type and content properties, which are accessed by the Node getter and setters
- Change node type references to the enum, instead of string - Fix spelling errors
- Change references to node type to reference the enum
- Change references to node type to reference the enum
- Replace references to string literals in the function to use the NodeType and Operator enums
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request refactors the expression tree implementation to use a standardized Node class structure. Previously, nodes were created as plain JavaScript objects with inconsistent properties. The new implementation introduces enum-like classes (Operator and NodeType) and a Node class with defined properties and computed getters.
Changes:
- Introduces new
Node,Operator, andNodeTypeclasses to standardize node representation - Refactors all node creation and type checking throughout the codebase to use the new standardized objects
- Removes manual property assignment for precedence and commutativity in favor of computed getters
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 10 comments.
| File | Description |
|---|---|
| pages/index/tree.js | New file containing Operator, NodeType, and Node class definitions with enum-like patterns and computed properties |
| pages/index/script.js | Updates all node creation and type checking to use the new Node class and enum values instead of plain objects and string literals |
| index.html | Adds script tag to include the new tree.js file before script.js |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Owner
Author
|
Adding some documentation as to what each property means would be beneficial. |
Returning in a switch-case statement terminates the statement. Hence, a break after the return is just dead code, and unneeded.
Previously, a missing break caused the setting of content for any Operator Node to fall into the function case. e.g: This would set its content as '+', instead of OPERATOR.ADDITION. Currently, these two things are the same thing, but this could deviate in the future.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Error objects return their stack trace, unlike a raw string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Previously, nodes in the expression tree had no defined structure. Hence, it was hard to tell what a node needed, and what the correct syntax was for that property.
The standard Node class contains properties:
NON-COMPUTED:
COMPUTED:
All website functions now interact with nodes using instances of this Node object.
(*) - properties type and content (when dealing with operators) have enums that define the values they can take.