refactor: apply internal improvements to editor classes#726
Conversation
Improve null management, types and JSDoc in `Editor` and `EditorPopupMenu`. Also fix the default value in the JSDoc of `CellStateStyle.segment`.
WalkthroughThis pull request updates core editor and type files to improve type safety and code clarity. It introduces the Changes
Possibly related PRs
Suggested labels
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms (1)
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
packages/core/src/editor/Editor.ts(37 hunks)packages/core/src/editor/EditorPopupMenu.ts(11 hunks)packages/core/src/types.ts(1 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
packages/core/src/editor/Editor.ts (2)
packages/core/src/util/Utils.ts (1)
isNullish(60-61)packages/core/src/view/cell/Cell.ts (1)
Cell(73-886)
🪛 Biome (1.9.4)
packages/core/src/editor/EditorPopupMenu.ts
[error] 197-197: eval() exposes to security risks and performance issues.
See the MDN web docs for more details.
Refactor the code so that it doesn't need to call eval().
(lint/security/noGlobalEval)
[error] 320-320: eval() exposes to security risks and performance issues.
See the MDN web docs for more details.
Refactor the code so that it doesn't need to call eval().
(lint/security/noGlobalEval)
🔇 Additional comments (26)
packages/core/src/types.ts (1)
656-656: JSDoc default value reference updated forsegmentproperty.The JSDoc
@defaultreference has been updated fromENTITY_SEGMENTtoEntityRelationConnectorConfig.segment, which better represents the actual source of the default value.packages/core/src/editor/Editor.ts (15)
60-61: Added proper type imports to improve type safety.These imports support the PR's goal of improving type definitions, especially for the
createPopupMenumethod.
65-70: Improved documentation formatting.The JSDoc comments have been reformatted for better readability by removing unnecessary colons and improving spacing.
1246-1251: Replaced direct null check withisNullishutility.Using the
isNullishfunction standardizes null checks across the codebase, improving code consistency and safety by checking for bothnullandundefined.
1254-1259: Replaced direct null check withisNullishutility.Consistent with the pattern of using
isNullishthroughout the codebase for better null handling.
1262-1267: Improved null check logic withisNullish.Note the slightly different logic here compared to the previous examples - this checks
if (isNullish(editor.outline))rather than negating the check, which aligns with the intended flow control.
2438-2440: Enhanced type safety forcreatePopupMenumethod.The method signature has been updated to use the
MaxPopupMenutype instead ofany, improving type safety.
2439-2439: Use of non-null assertion withpopupHandler!.The code assumes
popupHandleris not null here. This is likely safe based on initialization in the constructor, but it would be worth verifying that this property is always initialized before this method is called.Can you confirm that
popupHandleris always initialized beforecreatePopupMenuis called? You may want to check the constructor and any potential early calls to this method.
2488-2495: Simplified conditional expression using&&for better readability.The code now uses a cleaner chained logical AND expression which improves readability while maintaining the same logic.
2503-2511: Improved null checking incycleAttributemethod.Replaced direct null check with
isNullishutility for both thecycleAttributeNameand the returned value fromconsumeCycleAttribute.
2524-2526: Simplified loop condition.The loop condition has been simplified while maintaining the same functionality.
2528-2533: Use of nullish coalescing operator for cleaner code.The code now uses the nullish coalescing operator (
??=) which is more concise than an if-statement for providing a default value. The use of non-null assertion with!ongetGeometry()assumes this method never returns null for this object.
2536-2538: Improved conditional logic readability.The condition has been simplified while maintaining the same functionality.
2542-2564: Enhanced null handling in state check.The code now uses simplified conditional structure with better checking of potential null/undefined values.
2576-2578: Simplified conditional logic for parent handling.The code has been simplified while maintaining the same behavior of setting the parent to the default parent.
2589-2593: Improved null check condition.The code now uses a simplified check for the vertex being non-null before proceeding with operations on it.
packages/core/src/editor/EditorPopupMenu.ts (10)
26-26: Improved code quality with isNullish utility importAdding the utility function for checking null/undefined values standardizes and centralizes this logic across the codebase, improving consistency and maintainability.
125-126: Documentation improvementCorrectly specifying the type as
Cellin the JSDoc comment improves type clarity and documentation accuracy.
151-153: Better type documentation in JSDocThe parameter documentation now correctly specifies
MaxPopupMenuandCelltypes, which aligns with the actual implementation and improves code understanding.
161-161: Enhanced null checking with isNullish utilityReplacing the direct null check with the
isNullishutility improves code consistency and makes the null/undefined handling more robust.
171-174: Improved parameter documentationThe JSDoc comments now properly reference the correct types, which enhances documentation accuracy and helps developers understand the API better.
184-184: Enhanced type safety with Record<string, boolean>Replacing the
anytype with a more specificRecord<string, boolean>type improves type safety and code clarity, making it explicit what kind of data structure is expected.
194-194: Consistent null checking with isNullishUsing the
isNullishutility function for all null checks across the codebase results in more consistent and maintainable code.Also applies to: 202-202, 209-209
278-280: Improved null checking in clickHandlerReplacing the direct null check with the
isNullishutility maintains consistency with the rest of the codebase.
296-296: Better type information with explicit return typeAdding an explicit return type
Record<string, boolean>to thecreateConditionsmethod provides better type information and makes the method's contract clearer.
317-326: Improved code readability with modern JavaScript constructsRefactoring the iteration over condition nodes to use a
for...ofloop withArray.fromimproves code readability and maintainability. UsingisNullishfor the name check maintains consistency.🧰 Tools
🪛 Biome (1.9.4)
[error] 320-320: eval() exposes to security risks and performance issues.
See the MDN web docs for more details.
Refactor the code so that it doesn't need to call eval().(lint/security/noGlobalEval)
| if (isNullish(condition) || conditions[condition]) { | ||
| let as = item.getAttribute('as')!; | ||
| as = Translations.get(as) || as; | ||
| const funct = eval(getTextContent(<Text>(<unknown>item))); |
There was a problem hiding this comment.
💡 Verification agent
🧩 Analysis chain
Security issue: Use of eval() poses security risks
The use of eval() to execute code from text content exposes the application to security risks and performance issues. This isn't a new issue introduced in this PR, but should be addressed in future work.
Consider refactoring to avoid using eval() by implementing a safer alternative like:
- const funct = eval(getTextContent(<Text>(<unknown>item)));
+ // Parse the function content safely or use a function registry approach
+ const functionContent = getTextContent(<Text>(<unknown>item));
+ const funct = new Function('editor', 'cell', 'evt', functionContent);Note that even new Function() has security implications but is generally safer than eval(). A better approach would be to use a predefined function registry or a more structured configuration format that doesn't require dynamic code evaluation.
Also applies to: 320-320
🏁 Script executed:
#!/bin/bash
# Check for usage of eval() in the EditorPopupMenu.ts file around the reported lines (197 and 320)
rg -n "eval\(getTextContent" packages/core/src/editor/EditorPopupMenu.tsLength of output: 220
Attention: Mitigate eval() Security Vulnerability in EditorPopupMenu.ts
It has been verified that eval() is used in two locations in packages/core/src/editor/EditorPopupMenu.ts:
- Line 197:
const funct = eval(getTextContent(<Text>(<unknown>item)));
- Line 320:
const funct = eval(getTextContent(<Text>(<unknown>condNode)));
The dynamic evaluation of code via eval() poses significant security and performance risks. While replacing eval() with new Function() is an improvement—since it avoids some of the pitfalls of eval()—it still carries inherent risks. A more robust solution would be to remove dynamic code execution entirely in favor of a predefined function registry or a structured configuration that safely handles dynamic behavior.
For example, consider refactoring as follows:
- const funct = eval(getTextContent(<Text>(<unknown>item)));
+ // Safely parse function content or use a function registry approach
+ const functionContent = getTextContent(<Text>(<unknown>item));
+ const funct = new Function('editor', 'cell', 'evt', functionContent);(Apply a similar approach to the code at line 320.)
This issue remains critical and should be prioritized for future work to ensure the application’s security.
🧰 Tools
🪛 Biome (1.9.4)
[error] 197-197: eval() exposes to security risks and performance issues.
See the MDN web docs for more details.
Refactor the code so that it doesn't need to call eval().
(lint/security/noGlobalEval)
There was a problem hiding this comment.
For now, we are going to keep the "eval" call as the implementation is supposed to be loaded from a XML configuration.
This will be later review to see if it is possible to find alternatives. See #722 which tracks this problem.
There was a problem hiding this comment.
@tbouffard I understand your decision to keep the eval() calls for now given the XML configuration loading requirements. This is a reasonable approach to handle the refactoring incrementally, focusing first on null management improvements. I'll make a note that finding alternatives to eval() is planned for future work.
✏️ Learnings added
Learnt from: tbouffard
PR: maxGraph/maxGraph#726
File: packages/core/src/editor/EditorPopupMenu.ts:197-197
Timestamp: 2025-03-25T14:13:25.214Z
Learning: The team has decided to keep using `eval()` in EditorPopupMenu.ts as the implementation is loaded from XML configuration. Finding alternatives to `eval()` for improved security is planned for future work.
Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.
|



Improve null management, types and JSDoc in
EditorandEditorPopupMenu.Also fix the default value in the JSDoc of
CellStateStyle.segment.Summary by CodeRabbit
segmentproperty inCellStateStyle.