-
Notifications
You must be signed in to change notification settings - Fork 199
refactor: migrate the "AutoLayout" story to TypeScript #638
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
Conversation
Rationale: - Ease the maintenance - Detect the errors earlier
|
Warning Rate limit exceeded@tbouffard has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 9 minutes and 28 seconds before requesting another review. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📒 Files selected for processing (1)
WalkthroughThe pull request introduces modifications to enhance type safety and documentation clarity in two files: Changes
Possibly related PRs
Suggested labels
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (3)
packages/html/stories/AutoLayout.stories.ts (3)
71-98: Improve event type safety and reduce code duplication.
- The event type for pointerdown/mousedown should be more specific
- The touchstart event handler duplicates the pointerdown logic
- (evt: MouseEvent | KeyboardEvent) => { + (evt: MouseEvent) => { overlay.fireEvent(new EventObject('pointerdown', { event: evt, state })); } ); if (!Client.IS_POINTER && Client.IS_TOUCH) { - InternalEvent.addListener( - shape.node, - 'touchstart', - (evt: MouseEvent | KeyboardEvent) => { - overlay.fireEvent(new EventObject('pointerdown', { event: evt, state })); - } - ); + InternalEvent.addListener(shape.node, 'touchstart', handlePointerDown); }
130-134: Consider using a regular method instead of an arrow function.Converting
resizeCellto an arrow function changes its binding behavior and prevents proper method overriding in derived classes.- resizeCell = (cell: Cell, bounds: Rectangle, recurse?: boolean): Cell => { - const resizedCell = super.resizeCell(cell, bounds, recurse); - executeLayout(); - return resizedCell; - }; + resizeCell(cell: Cell, bounds: Rectangle, recurse?: boolean): Cell { + const resizedCell = super.resizeCell(cell, bounds, recurse); + executeLayout(); + return resizedCell; + }
249-253: Remove commented-out code and TODO comment.The commented-out code appears to be obsolete since the
resizeCellmethod has been reimplemented above. This commented section should be removed.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
packages/core/src/view/handler/EdgeHandler.ts(1 hunks)packages/html/stories/AutoLayout.stories.ts(4 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
packages/html/stories/AutoLayout.stories.ts
[error] 184-184: Don't use 'Function' as a type.
Prefer explicitly define the function shape. This type accepts any function-like value, which can be a common source of bugs.
(lint/complexity/noBannedTypes)
🔇 Additional comments (3)
packages/html/stories/AutoLayout.stories.ts (2)
Line range hint
1-70: LGTM! TypeScript migration for imports and story configuration is well done.The migration includes proper type imports and story configuration setup.
100-112: LGTM! Well-typed EdgeHandler implementation.The MyCustomEdgeHandler class is properly typed with TypeScript annotations.
packages/core/src/view/handler/EdgeHandler.ts (1)
1687-1700: LGTM! Good use of TypeScript parameter naming convention.The renaming of parameters to
_isCloneand_mefollows the TypeScript convention for indicating unused parameters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (1)
packages/html/stories/AutoLayout.stories.ts (1)
89-95: Consider consolidating duplicate event handling logic.The touch event handling duplicates the event firing logic. Consider extracting the common logic into a shared function.
class MyCustomCellRenderer extends CellRenderer { installCellOverlayListeners(state: CellState, overlay: CellOverlay, shape: Shape) { super.installCellOverlayListeners(state, overlay, shape); + + const firePointerdownEvent = (evt: MouseEvent | KeyboardEvent) => { + overlay.fireEvent(new EventObject('pointerdown', { event: evt, state })); + }; InternalEvent.addListener( shape.node, Client.IS_POINTER ? 'pointerdown' : 'mousedown', - (evt: MouseEvent | KeyboardEvent) => { - overlay.fireEvent(new EventObject('pointerdown', { event: evt, state })); - } + firePointerdownEvent ); if (!Client.IS_POINTER && Client.IS_TOUCH) { InternalEvent.addListener( shape.node, 'touchstart', - (evt: MouseEvent | KeyboardEvent) => { - overlay.fireEvent(new EventObject('pointerdown', { event: evt, state })); - } + firePointerdownEvent ); } }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
packages/html/stories/AutoLayout.stories.ts(4 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
packages/html/stories/AutoLayout.stories.ts
[error] 184-184: Don't use 'Function' as a type.
Prefer explicitly define the function shape. This type accepts any function-like value, which can be a common source of bugs.
(lint/complexity/noBannedTypes)
⏰ Context from checks skipped due to timeout of 90000ms (4)
- GitHub Check: build (windows-2022)
- GitHub Check: build
- GitHub Check: build (macos-14)
- GitHub Check: build (ubuntu-22.04)
🔇 Additional comments (2)
packages/html/stories/AutoLayout.stories.ts (2)
27-42: LGTM! Well-organized type imports.The TypeScript types are properly imported and organized, which enhances code clarity and type safety.
108-110: Verify executeLayout function scope.The
executeLayoutfunction is called before it's defined. Consider moving the function definition before its usage or declaring its type upfront to ensure proper scoping and type safety.
|
|
||
| // Installs a handler for clicks on the overlay | ||
| overlay.addListener(InternalEvent.CLICK, (sender, evt2) => { | ||
| overlay.addListener(InternalEvent.CLICK, (_name: string, _funct: Function) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace 'Function' type with a specific function signature.
The static analysis tool correctly flags the use of the Function type. This type is too broad and can lead to type-safety issues.
- overlay.addListener(InternalEvent.CLICK, (_name: string, _funct: Function) => {
+ overlay.addListener(InternalEvent.CLICK, (_name: string, _funct: (sender: EventTarget, evt: EventObject) => void) => {Committable suggestion skipped: line range outside the PR's diff.
🧰 Tools
🪛 Biome (1.9.4)
[error] 184-184: Don't use 'Function' as a type.
Prefer explicitly define the function shape. This type accepts any function-like value, which can be a common source of bugs.
(lint/complexity/noBannedTypes)
| const geo = cell.getGeometry(); | ||
| vertex = graph.insertVertex({ | ||
| parent, | ||
| value: 'World!', | ||
| position: [geo.x, geo.y], | ||
| position: [geo!.x, geo!.y], | ||
| size: [80, 30], | ||
| }); | ||
| addOverlay(v2); | ||
| graph.view.refresh(v2); | ||
| const e1 = graph.insertEdge({ | ||
| addOverlay(vertex); | ||
| graph.view.refresh(); | ||
| graph.insertEdge({ | ||
| parent, | ||
| source: cell, | ||
| target: v2, | ||
| target: vertex, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add proper null checks instead of non-null assertions.
Replace the non-null assertion (!) with proper null checks to handle the case where geometry might be undefined.
- const geo = cell.getGeometry();
- vertex = graph.insertVertex({
- parent,
- value: 'World!',
- position: [geo!.x, geo!.y],
- size: [80, 30],
- });
+ const geo = cell.getGeometry();
+ if (!geo) {
+ throw new Error('Cell geometry is required');
+ }
+ vertex = graph.insertVertex({
+ parent,
+ value: 'World!',
+ position: [geo.x, geo.y],
+ size: [80, 30],
+ });📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| const geo = cell.getGeometry(); | |
| vertex = graph.insertVertex({ | |
| parent, | |
| value: 'World!', | |
| position: [geo.x, geo.y], | |
| position: [geo!.x, geo!.y], | |
| size: [80, 30], | |
| }); | |
| addOverlay(v2); | |
| graph.view.refresh(v2); | |
| const e1 = graph.insertEdge({ | |
| addOverlay(vertex); | |
| graph.view.refresh(); | |
| graph.insertEdge({ | |
| parent, | |
| source: cell, | |
| target: v2, | |
| target: vertex, | |
| const geo = cell.getGeometry(); | |
| if (!geo) { | |
| throw new Error('Cell geometry is required'); | |
| } | |
| vertex = graph.insertVertex({ | |
| parent, | |
| value: 'World!', | |
| position: [geo.x, geo.y], | |
| size: [80, 30], | |
| }); | |
| addOverlay(vertex); | |
| graph.view.refresh(); | |
| graph.insertEdge({ | |
| parent, | |
| source: cell, | |
| target: vertex, |
Rationale:
Summary by CodeRabbit
Documentation
EdgeHandlerclass, making them more concise.Refactor
AutoLayout.stories.tsby adding TypeScript type annotations.Style