Skip to content
Merged
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
20 changes: 15 additions & 5 deletions src/DiffEditor/DiffEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,21 +194,31 @@ function DiffEditor({
}, [isEditorReady]);

useEffect(() => {
!isMonacoMounting && !isEditorReady && createEditor();
if (!isMonacoMounting && !isEditorReady) {
createEditor();
}
}, [isMonacoMounting, isEditorReady, createEditor]);

function disposeEditor(): void {
const models = editorRef.current?.getModel();
const editor = editorRef.current;
const models = editor?.getModel();

// oxlint-disable-next-line unicorn/no-null -- Monaco detaches diff models with null.
editor?.setModel(null);
editor?.dispose();

if (!keepCurrentOriginalModel) {
if (!keepCurrentOriginalModel && !models?.original.isDisposed()) {
models?.original?.dispose();
}
Comment on lines +210 to 212
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The expression models?.original.isDisposed() will throw a TypeError if models is null or undefined, because it attempts to access the .isDisposed() method on an undefined value. Since editor?.getModel() can return null if no model is set, you should verify that models.original exists before checking its disposal state.

Suggested change
if (!keepCurrentOriginalModel && !models?.original.isDisposed()) {
models?.original?.dispose();
}
if (!keepCurrentOriginalModel && models?.original && !models.original.isDisposed()) {
models.original.dispose();
}


if (!keepCurrentModifiedModel) {
if (!keepCurrentModifiedModel && !models?.modified.isDisposed()) {
models?.modified?.dispose();
}
Comment on lines +214 to 216
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Similar to the original model check, models?.modified.isDisposed() will crash if models is null. Ensure safe access to the modified model before calling isDisposed().

Suggested change
if (!keepCurrentModifiedModel && !models?.modified.isDisposed()) {
models?.modified?.dispose();
}
if (!keepCurrentModifiedModel && models?.modified && !models.modified.isDisposed()) {
models.modified.dispose();
}


editorRef.current?.dispose();
// oxlint-disable-next-line unicorn/no-null -- React refs use null after unmount.
editorRef.current = null;
preventCreation.current = false;
setIsEditorReady(false);
}

return (
Expand Down
Loading