Skip to content

Don't throw when removing a property whose parent doesn't exist - #153

Draft
Federico Brancasi (federicobrancasi) wants to merge 1 commit into
mainfrom
fbrancasi/modify-delete-missing-parent
Draft

Federico Brancasi (federicobrancasi) wants to merge 1 commit into
mainfrom
fbrancasi/modify-delete-missing-parent

Conversation

@federicobrancasi

Copy link
Copy Markdown
Member

Removing a property whose parent object doesn't exist throws "Can not delete in empty document", for example modify('{"a":1}', ['[typescript]', 'editor.formatOnSave'], undefined, options). The document isn't empty, and there is nothing to delete. setProperty walks up the path to the closest parent that exists, and when there is none it threw for a deletion.

This PR returns no edits instead, as it already does when only the last segment is missing, and as VS Code's copy of this code does since microsoft/vscode#210025. Tools that edit settings files with modify hit this error, for example Kilo-Org/kilocode#12453.

How to test

Command Result
npx tsc -p ./src && npm run lint && node --enable-source-maps --test ./lib/esm/test/*.test.js 89 pass; the new test "remove property whose parent does not exist" fails without the change

`modify(text, path, undefined)` threw "Can not delete in empty document"
whenever an object or array on the path was missing, for example when
removing `['[typescript]', 'editor.formatOnSave']` from `{"a": 1}`. The
property doesn't exist, so there is nothing to remove: return no edits, as
is already done when only the last segment is missing.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the change making deletion a no-op when its parent is absent.

No open issues 🎉

What changed in this PR

Makes property deletion through modify a no-op when the parent does not exist, avoiding an exception when editing JSON settings.

Changes:

  • Returns an empty edit list instead of throwing when deletion finds no parent.
  • Adds regression coverage for a missing parent property and an empty document.
File Description
src/​test/​edit.test.ts Tests deletion with a missing parent and an empty document.
src/​impl/​edit.ts Returns no edits when deletion finds no parent.

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants