Skip to content

Commit e1da1fa

Browse files
committed
* Fixes object property assignment bug in resolving package-locks with conflicts PR-URL: #4141 Credit: @wraithgar Close: #4141 Reviewed-by: @fritzy
1 parent 4b0c29a commit e1da1fa

File tree

14 files changed

+391
-81
lines changed

14 files changed

+391
-81
lines changed

node_modules/@npmcli/arborist/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@npmcli/arborist",
3-
"version": "4.1.0",
3+
"version": "4.1.1",
44
"description": "Manage node_modules trees",
55
"dependencies": {
66
"@isaacs/string-locale-compare": "^1.1.0",
@@ -24,7 +24,7 @@
2424
"npm-pick-manifest": "^6.1.0",
2525
"npm-registry-fetch": "^11.0.0",
2626
"pacote": "^12.0.2",
27-
"parse-conflict-json": "^1.1.1",
27+
"parse-conflict-json": "^2.0.1",
2828
"proc-log": "^1.0.0",
2929
"promise-all-reject-late": "^1.0.0",
3030
"promise-call-limit": "^1.0.1",
+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
const obj1 = {a: 3, b: 5};
3+
diffApply(obj1,
4+
[
5+
{ "op": "remove", "path": ['b'] },
6+
{ "op": "replace", "path": ['a'], "value": 4 },
7+
{ "op": "add", "path": ['c'], "value": 5 }
8+
]
9+
);
10+
obj1; // {a: 4, c: 5}
11+
12+
// using converter to apply jsPatch standard paths
13+
// see http://jsonpatch.com
14+
import {diff, jsonPatchPathConverter} from 'just-diff'
15+
const obj2 = {a: 3, b: 5};
16+
diffApply(obj2, [
17+
{ "op": "remove", "path": '/b' },
18+
{ "op": "replace", "path": '/a', "value": 4 }
19+
{ "op": "add", "path": '/c', "value": 5 }
20+
], jsonPatchPathConverter);
21+
obj2; // {a: 4, c: 5}
22+
23+
// arrays
24+
const obj3 = {a: 4, b: [1, 2, 3]};
25+
diffApply(obj3, [
26+
{ "op": "replace", "path": ['a'], "value": 3 }
27+
{ "op": "replace", "path": ['b', 2], "value": 4 }
28+
{ "op": "add", "path": ['b', 3], "value": 9 }
29+
]);
30+
obj3; // {a: 3, b: [1, 2, 4, 9]}
31+
32+
// nested paths
33+
const obj4 = {a: 4, b: {c: 3}};
34+
diffApply(obj4, [
35+
{ "op": "replace", "path": ['a'], "value": 5 }
36+
{ "op": "remove", "path": ['b', 'c']}
37+
{ "op": "add", "path": ['b', 'd'], "value": 4 }
38+
]);
39+
obj4; // {a: 5, b: {d: 4}}
40+
*/
41+
42+
var REMOVE = 'remove';
43+
var REPLACE = 'replace';
44+
var ADD = 'add';
45+
46+
function diffApply(obj, diff, pathConverter) {
47+
if (!obj || typeof obj != 'object') {
48+
throw new Error('base object must be an object or an array');
49+
}
50+
51+
if (!Array.isArray(diff)) {
52+
throw new Error('diff must be an array');
53+
}
54+
55+
var diffLength = diff.length;
56+
for (var i = 0; i < diffLength; i++) {
57+
var thisDiff = diff[i];
58+
var subObject = obj;
59+
var thisOp = thisDiff.op;
60+
var thisPath = thisDiff.path;
61+
if (pathConverter) {
62+
thisPath = pathConverter(thisPath);
63+
if (!Array.isArray(thisPath)) {
64+
throw new Error('pathConverter must return an array');
65+
}
66+
} else {
67+
if (!Array.isArray(thisPath)) {
68+
throw new Error(
69+
'diff path must be an array, consider supplying a path converter'
70+
);
71+
}
72+
}
73+
var pathCopy = thisPath.slice();
74+
var lastProp = pathCopy.pop();
75+
if (lastProp == null) {
76+
return false;
77+
}
78+
var thisProp;
79+
while ((thisProp = pathCopy.shift()) != null) {
80+
if (!(thisProp in subObject)) {
81+
subObject[thisProp] = {};
82+
}
83+
subObject = subObject[thisProp];
84+
}
85+
if (thisOp === REMOVE || thisOp === REPLACE) {
86+
if (!subObject.hasOwnProperty(lastProp)) {
87+
throw new Error(
88+
['expected to find property', thisDiff.path, 'in object', obj].join(
89+
' '
90+
)
91+
);
92+
}
93+
}
94+
if (thisOp === REMOVE) {
95+
Array.isArray(subObject)
96+
? subObject.splice(lastProp, 1)
97+
: delete subObject[lastProp];
98+
}
99+
if (thisOp === REPLACE || thisOp === ADD) {
100+
subObject[lastProp] = thisDiff.value;
101+
}
102+
}
103+
return subObject;
104+
}
105+
106+
function jsonPatchPathConverter(stringPath) {
107+
return stringPath.split('/').slice(1);
108+
}
109+
110+
export {diffApply, jsonPatchPathConverter};

node_modules/just-diff-apply/package.json

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
{
22
"name": "just-diff-apply",
3-
"version": "3.0.0",
3+
"version": "4.0.1",
44
"description": "Apply a diff to an object. Optionally supports jsonPatch protocol",
55
"main": "index.js",
6+
"module": "index.mjs",
7+
"exports": {
8+
".": {
9+
"require": "./index.js",
10+
"default": "./index.mjs"
11+
}
12+
},
613
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
14+
"test": "echo \"Error: no test specified\" && exit 1",
15+
"build": "rollup -c"
816
},
917
"repository": "https://github.com/angus-c/just",
1018
"keywords": [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const createRollupConfig = require('../../config/createRollupConfig');
2+
3+
module.exports = createRollupConfig(__dirname);

node_modules/just-diff/index.mjs

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
const obj1 = {a: 4, b: 5};
3+
const obj2 = {a: 3, b: 5};
4+
const obj3 = {a: 4, c: 5};
5+
6+
diff(obj1, obj2);
7+
[
8+
{ "op": "replace", "path": ['a'], "value": 3 }
9+
]
10+
11+
diff(obj2, obj3);
12+
[
13+
{ "op": "remove", "path": ['b'] },
14+
{ "op": "replace", "path": ['a'], "value": 4 }
15+
{ "op": "add", "path": ['c'], "value": 5 }
16+
]
17+
18+
// using converter to generate jsPatch standard paths
19+
// see http://jsonpatch.com
20+
import {diff, jsonPatchPathConverter} from 'just-diff'
21+
diff(obj1, obj2, jsonPatchPathConverter);
22+
[
23+
{ "op": "replace", "path": '/a', "value": 3 }
24+
]
25+
26+
diff(obj2, obj3, jsonPatchPathConverter);
27+
[
28+
{ "op": "remove", "path": '/b' },
29+
{ "op": "replace", "path": '/a', "value": 4 }
30+
{ "op": "add", "path": '/c', "value": 5 }
31+
]
32+
33+
// arrays
34+
const obj4 = {a: 4, b: [1, 2, 3]};
35+
const obj5 = {a: 3, b: [1, 2, 4]};
36+
const obj6 = {a: 3, b: [1, 2, 4, 5]};
37+
38+
diff(obj4, obj5);
39+
[
40+
{ "op": "replace", "path": ['a'], "value": 3 }
41+
{ "op": "replace", "path": ['b', 2], "value": 4 }
42+
]
43+
44+
diff(obj5, obj6);
45+
[
46+
{ "op": "add", "path": ['b', 3], "value": 5 }
47+
]
48+
49+
// nested paths
50+
const obj7 = {a: 4, b: {c: 3}};
51+
const obj8 = {a: 4, b: {c: 4}};
52+
const obj9 = {a: 5, b: {d: 4}};
53+
54+
diff(obj7, obj8);
55+
[
56+
{ "op": "replace", "path": ['b', 'c'], "value": 4 }
57+
]
58+
59+
diff(obj8, obj9);
60+
[
61+
{ "op": "replace", "path": ['a'], "value": 5 }
62+
{ "op": "remove", "path": ['b', 'c']}
63+
{ "op": "add", "path": ['b', 'd'], "value": 4 }
64+
]
65+
*/
66+
67+
function diff(obj1, obj2, pathConverter) {
68+
if (!obj1 || typeof obj1 != 'object' || !obj2 || typeof obj2 != 'object') {
69+
throw new Error('both arguments must be objects or arrays');
70+
}
71+
72+
pathConverter ||
73+
(pathConverter = function(arr) {
74+
return arr;
75+
});
76+
77+
function getDiff(obj1, obj2, basePath, diffs) {
78+
var obj1Keys = Object.keys(obj1);
79+
var obj1KeysLength = obj1Keys.length;
80+
var obj2Keys = Object.keys(obj2);
81+
var obj2KeysLength = obj2Keys.length;
82+
var path;
83+
84+
for (var i = 0; i < obj1KeysLength; i++) {
85+
var key = Array.isArray(obj1) ? Number(obj1Keys[i]) : obj1Keys[i];
86+
if (!(key in obj2)) {
87+
path = basePath.concat(key);
88+
diffs.remove.push({
89+
op: 'remove',
90+
path: pathConverter(path),
91+
});
92+
}
93+
}
94+
95+
for (var i = 0; i < obj2KeysLength; i++) {
96+
var key = Array.isArray(obj2) ? Number(obj2Keys[i]) : obj2Keys[i];
97+
var obj1AtKey = obj1[key];
98+
var obj2AtKey = obj2[key];
99+
if (!(key in obj1)) {
100+
path = basePath.concat(key);
101+
var obj2Value = obj2[key];
102+
diffs.add.push({
103+
op: 'add',
104+
path: pathConverter(path),
105+
value: obj2Value,
106+
});
107+
} else if (obj1AtKey !== obj2AtKey) {
108+
if (
109+
Object(obj1AtKey) !== obj1AtKey ||
110+
Object(obj2AtKey) !== obj2AtKey
111+
) {
112+
path = pushReplace(path, basePath, key, diffs, pathConverter, obj2);
113+
} else {
114+
if (
115+
!Object.keys(obj1AtKey).length &&
116+
!Object.keys(obj2AtKey).length &&
117+
String(obj1AtKey) != String(obj2AtKey)
118+
) {
119+
path = pushReplace(path, basePath, key, diffs, pathConverter, obj2);
120+
} else {
121+
getDiff(obj1[key], obj2[key], basePath.concat(key), diffs);
122+
}
123+
}
124+
}
125+
}
126+
127+
return diffs.remove.reverse().concat(diffs.replace).concat(diffs.add);
128+
}
129+
return getDiff(obj1, obj2, [], {remove: [], replace: [], add: []});
130+
}
131+
132+
function pushReplace(path, basePath, key, diffs, pathConverter, obj2) {
133+
path = basePath.concat(key);
134+
diffs.replace.push({
135+
op: 'replace',
136+
path: pathConverter(path),
137+
value: obj2[key],
138+
});
139+
return path;
140+
}
141+
142+
function jsonPatchPathConverter(arrayPath) {
143+
return [''].concat(arrayPath).join('/');
144+
}
145+
146+
export {diff, jsonPatchPathConverter};

node_modules/just-diff/index.tests.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import diffObj = require('./index');
1+
import * as diffObj from './index'
22

33
const {diff, jsonPatchPathConverter} = diffObj;
44
const obj1 = {a: 2, b: 3};

node_modules/just-diff/package.json

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
{
22
"name": "just-diff",
3-
"version": "3.1.1",
3+
"version": "5.0.1",
44
"description": "Return an object representing the diffs between two objects. Supports jsonPatch protocol",
55
"main": "index.js",
6+
"module": "index.mjs",
7+
"exports": {
8+
".": {
9+
"require": "./index.js",
10+
"default": "./index.mjs"
11+
}
12+
},
613
"types": "index.d.ts",
714
"scripts": {
8-
"test": "echo \"Error: no test specified\" && exit 1"
15+
"test": "echo \"Error: no test specified\" && exit 1",
16+
"build": "rollup -c"
917
},
1018
"repository": "https://github.com/angus-c/just",
1119
"keywords": [
@@ -20,4 +28,4 @@
2028
"bugs": {
2129
"url": "https://github.com/angus-c/just/issues"
2230
}
23-
}
31+
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const createRollupConfig = require('../../config/createRollupConfig');
2+
3+
module.exports = createRollupConfig(__dirname);

node_modules/parse-conflict-json/LICENSE

-15
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!-- This file is automatically added by @npmcli/template-oss. Do not edit. -->
2+
3+
ISC License
4+
5+
Copyright npm, Inc.
6+
7+
Permission to use, copy, modify, and/or distribute this
8+
software for any purpose with or without fee is hereby
9+
granted, provided that the above copyright notice and this
10+
permission notice appear in all copies.
11+
12+
THE SOFTWARE IS PROVIDED "AS IS" AND NPM DISCLAIMS ALL
13+
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
14+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
15+
EVENT SHALL NPM BE LIABLE FOR ANY SPECIAL, DIRECT,
16+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
18+
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19+
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
20+
USE OR PERFORMANCE OF THIS SOFTWARE.

0 commit comments

Comments
 (0)