Skip to content

Commit 1225b4f

Browse files
committed
AG-40548 Fix json-prune — content of array is incorrectly removed. #482
Squashed commit of the following: commit 4e7a9b2 Author: Adam Wróblewski <[email protected]> Date: Thu Mar 6 16:07:04 2025 +0100 Reduce nesting commit b5e1708 Author: Adam Wróblewski <[email protected]> Date: Thu Mar 6 15:45:17 2025 +0100 Update package.json and changeLog commit 188f2f6 Author: Adam Wróblewski <[email protected]> Date: Thu Mar 6 14:02:27 2025 +0100 Update changelog commit d08451c Author: Adam Wróblewski <[email protected]> Date: Thu Mar 6 13:39:08 2025 +0100 Fix json-prune — content of array is incorrectly removed
1 parent bb9da47 commit 1225b4f

File tree

4 files changed

+57
-14
lines changed

4 files changed

+57
-14
lines changed

CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic
1010
<!-- TODO: change `@added unknown` tag due to the actual version -->
1111
<!-- during new scriptlets or redirects releasing -->
1212

13+
## [v2.1.6] - 2025-03-06
14+
15+
### Fixed
16+
17+
- issue with incorrectly removing content from parsed array when using the `json-prune` scriptlet [#482]
18+
19+
[v2.1.6]: https://github.com/AdguardTeam/Scriptlets/compare/v2.1.5...v2.1.6
20+
[#482]: https://github.com/AdguardTeam/Scriptlets/issues/482
21+
1322
## [v2.1.5] - 2025-02-28
1423

1524
### Changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@adguard/scriptlets",
3-
"version": "2.1.5",
3+
"version": "2.1.6",
44
"description": "AdGuard's JavaScript library of Scriptlets and Redirect resources",
55
"type": "module",
66
"scripts": {

src/helpers/prune-utils.ts

+21-13
Original file line numberDiff line numberDiff line change
@@ -149,20 +149,28 @@ export const jsonPruner = (
149149
// Iterate in reverse order to avoid index issues when removing elements from an array
150150
for (let i = ownerObjArr.length - 1; i >= 0; i -= 1) {
151151
const ownerObj = ownerObjArr[i];
152-
if (ownerObj !== undefined && ownerObj.base) {
153-
if (Array.isArray(ownerObj.base)) {
154-
try {
155-
const index = Number(ownerObj.prop);
156-
// Delete operator leaves "undefined" in the array and it sometimes causes issues
157-
ownerObj.base.splice(index, 1);
158-
} catch (error) {
159-
// eslint-disable-next-line no-console
160-
console.error('Error while deleting array element', error);
161-
}
162-
} else {
163-
delete ownerObj.base[ownerObj.prop];
152+
if (ownerObj === undefined || !ownerObj.base) {
153+
continue;
154+
}
155+
156+
hit(source);
157+
158+
if (!Array.isArray(ownerObj.base)) {
159+
delete ownerObj.base[ownerObj.prop];
160+
continue;
161+
}
162+
163+
try {
164+
const index = Number(ownerObj.prop);
165+
if (Number.isNaN(index)) {
166+
continue;
164167
}
165-
hit(source);
168+
169+
// Delete operator leaves "undefined" in the array and it sometimes causes issues
170+
ownerObj.base.splice(index, 1);
171+
} catch (error) {
172+
// eslint-disable-next-line no-console
173+
console.error('Error while deleting array element', error);
166174
}
167175
}
168176
});

tests/helpers/prune-utils.spec.js

+26
Original file line numberDiff line numberDiff line change
@@ -931,4 +931,30 @@ describe('jsonPruner tests', () => {
931931

932932
expect(result).toStrictEqual(expected);
933933
});
934+
935+
test('Array - should not be removed', async () => {
936+
const root = [
937+
'foo',
938+
0,
939+
1,
940+
43200,
941+
100,
942+
];
943+
944+
const expected = [
945+
'foo',
946+
0,
947+
1,
948+
43200,
949+
100,
950+
];
951+
952+
// eslint-disable-next-line max-len
953+
const pathToPrune = getPrunePath('playerResponse.adPlacements playerResponse.playerAds playerResponse.adSlots adPlacements playerAds adSlots');
954+
const requiredPaths = getPrunePath('');
955+
const stack = '';
956+
const result = jsonPruner(name, root, pathToPrune, requiredPaths, stack, nativeObjects);
957+
958+
expect(result).toStrictEqual(expected);
959+
});
934960
});

0 commit comments

Comments
 (0)