From cf4f4e54967db819ed9457da89cd5c41041f39de Mon Sep 17 00:00:00 2001 From: drawliin Date: Mon, 19 Oct 2026 10:36:53 +0000 Subject: [PATCH] fix: preserve array items when deleting named properties --- lib/utils/queryable.js | 2 +- test/lib/commands/pkg.js | 4 ++++ test/lib/utils/queryable.js | 10 ++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/utils/queryable.js b/lib/utils/queryable.js index fac7581e6f852..c20b3b849317c 100644 --- a/lib/utils/queryable.js +++ b/lib/utils/queryable.js @@ -197,7 +197,7 @@ const setter = ({ data, key, value, force }) => { } else { // handles special deletion cases for obj props / array items if (value === _delete) { - if (Array.isArray(_data)) { + if (Array.isArray(_data) && typeof _key === 'number') { _data.splice(_key, 1) } else { delete _data[_key] diff --git a/test/lib/commands/pkg.js b/test/lib/commands/pkg.js index 50288cc42366b..f26b5a4c480e2 100644 --- a/test/lib/commands/pkg.js +++ b/test/lib/commands/pkg.js @@ -494,6 +494,10 @@ t.test('delete', t => { }), }, }) + const before = readPackageJson(npm.prefix) + await npm.exec('pkg', ['delete', 'info.foo.bar.missing']) + t.strictSame(readPackageJson(npm.prefix), before, + 'should leave array items unchanged when deleting a missing property') await npm.exec('pkg', ['delete', 'info.foo.bar[0].baz']) t.matchSnapshot( readPackageJson(npm.prefix), diff --git a/test/lib/utils/queryable.js b/test/lib/utils/queryable.js index 5eca84214cd65..36810dfa70f71 100644 --- a/test/lib/utils/queryable.js +++ b/test/lib/utils/queryable.js @@ -919,6 +919,16 @@ t.test('delete values', async t => { }, 'should delete array item' ) + + const arrays = { + keywords: ['alpha', 'beta'], + contributors: [{ name: 'alice' }, { name: 'bob' }], + } + const qq = new Queryable(structuredClone(arrays)) + for (const key of ['keywords.missing', 'keywords.beta', 'contributors.missing']) { + qq.delete(key) + t.strictSame(qq.toJSON(), arrays, `should leave arrays unchanged when deleting ${key}`) + } }) t.test('logger', async t => {