From da377eed5cba72185b90f5fc32ef288331c856ef Mon Sep 17 00:00:00 2001 From: Gar Date: Mon, 28 Mar 2022 14:00:45 -0700 Subject: [PATCH] deps: parse-conflict-json@2.0.2 --- node_modules/just-diff-apply/index.d.ts | 17 +++ node_modules/just-diff-apply/index.js | 78 +++++++++++-- node_modules/just-diff-apply/index.mjs | 25 ++-- node_modules/just-diff-apply/index.tests.ts | 108 ++++++++++++++++++ node_modules/just-diff-apply/package.json | 3 +- node_modules/parse-conflict-json/package.json | 29 +++-- package-lock.json | 32 +++--- package.json | 2 +- 8 files changed, 241 insertions(+), 53 deletions(-) create mode 100644 node_modules/just-diff-apply/index.d.ts create mode 100644 node_modules/just-diff-apply/index.tests.ts diff --git a/node_modules/just-diff-apply/index.d.ts b/node_modules/just-diff-apply/index.d.ts new file mode 100644 index 0000000000000..9fc05257af0aa --- /dev/null +++ b/node_modules/just-diff-apply/index.d.ts @@ -0,0 +1,17 @@ +// Definitions by: Eddie Atkinson + +type Operation = "add" | "replace" | "remove"; + +type DiffOps = Array<{ + op: Operation; + path: Array; + value?: any; +}>; +type PathConverter = (path: string) => string[]; + +export function diffApply( + obj: T, + diff: DiffOps, + pathConverter?: PathConverter +): T; +export const jsonPatchPathConverter: PathConverter; diff --git a/node_modules/just-diff-apply/index.js b/node_modules/just-diff-apply/index.js index ceb32681172a8..c5d2c3265a852 100644 --- a/node_modules/just-diff-apply/index.js +++ b/node_modules/just-diff-apply/index.js @@ -47,6 +47,7 @@ module.exports = { var REMOVE = 'remove'; var REPLACE = 'replace'; var ADD = 'add'; +var MOVE = 'move'; function diffApply(obj, diff, pathConverter) { if (!obj || typeof obj != 'object') { @@ -62,44 +63,99 @@ function diffApply(obj, diff, pathConverter) { var thisDiff = diff[i]; var subObject = obj; var thisOp = thisDiff.op; - var thisPath = thisDiff.path; - if (pathConverter) { - thisPath = pathConverter(thisPath); - if (!Array.isArray(thisPath)) { - throw new Error('pathConverter must return an array'); + + var thisPath = transformPath(pathConverter, thisDiff.path); + var thisFromPath = thisDiff.from && transformPath(pathConverter, thisDiff.from); + var toPath, toPathCopy, lastToProp, subToObject, valueToMove; + + if (thisFromPath) { + // MOVE only, "fromPath" is effectively path and "path" is toPath + toPath = thisPath; + thisPath = thisFromPath; + + toPathCopy = toPath.slice(); + lastToProp = toPathCopy.pop(); + prototypeCheck(lastToProp); + if (lastToProp == null) { + return false; } - } else { - if (!Array.isArray(thisPath)) { - throw new Error('diff path must be an array, consider supplying a path converter'); + + var thisToProp; + while (((thisToProp = toPathCopy.shift())) != null) { + prototypeCheck(thisToProp); + if (!(thisToProp in subToObject)) { + subToObject[thisToProp] = {}; + } + subToObject = subToObject[thisToProp]; } } + var pathCopy = thisPath.slice(); var lastProp = pathCopy.pop(); + prototypeCheck(lastProp); if (lastProp == null) { return false; } + var thisProp; while (((thisProp = pathCopy.shift())) != null) { + prototypeCheck(thisProp); if (!(thisProp in subObject)) { subObject[thisProp] = {}; } subObject = subObject[thisProp]; } - if (thisOp === REMOVE || thisOp === REPLACE) { + if (thisOp === REMOVE || thisOp === REPLACE || thisOp === MOVE) { + var path = thisOp === MOVE ? thisDiff.from : thisDiff.path; if (!subObject.hasOwnProperty(lastProp)) { - throw new Error(['expected to find property', thisDiff.path, 'in object', obj].join(' ')); + throw new Error(['expected to find property', path, 'in object', obj].join(' ')); } } - if (thisOp === REMOVE) { + if (thisOp === REMOVE || thisOp === MOVE) { + if (thisOp === MOVE) { + valueToMove = subObject[lastProp]; + } Array.isArray(subObject) ? subObject.splice(lastProp, 1) : delete subObject[lastProp]; } if (thisOp === REPLACE || thisOp === ADD) { subObject[lastProp] = thisDiff.value; } + + if (thisOp === MOVE) { + subObject[lastToProp] = valueToMove; + } } return subObject; } +function transformPath(pathConverter, thisPath) { + if(pathConverter) { + thisPath = pathConverter(thisPath); + if(!Array.isArray(thisPath)) { + throw new Error([ + 'pathConverter must return an array, returned:', + thisPath, + ].join(' ')); + } + } else { + if(!Array.isArray(thisPath)) { + throw new Error([ + 'diff path', + thisPath, + 'must be an array, consider supplying a path converter'] + .join(' ')); + } + } + return thisPath; +} + function jsonPatchPathConverter(stringPath) { return stringPath.split('/').slice(1); } + +function prototypeCheck(prop) { + // coercion is intentional to catch prop values like `['__proto__']` + if (prop == '__proto__' || prop == 'constructor' || prop == 'prototype') { + throw new Error('setting of prototype values not supported'); + } +} diff --git a/node_modules/just-diff-apply/index.mjs b/node_modules/just-diff-apply/index.mjs index fcd26c2f5a23e..045830507cd17 100644 --- a/node_modules/just-diff-apply/index.mjs +++ b/node_modules/just-diff-apply/index.mjs @@ -65,18 +65,18 @@ function diffApply(obj, diff, pathConverter) { } } else { if (!Array.isArray(thisPath)) { - throw new Error( - 'diff path must be an array, consider supplying a path converter' - ); + throw new Error('diff path must be an array, consider supplying a path converter'); } } var pathCopy = thisPath.slice(); var lastProp = pathCopy.pop(); + prototypeCheck(lastProp); if (lastProp == null) { return false; } var thisProp; - while ((thisProp = pathCopy.shift()) != null) { + while (((thisProp = pathCopy.shift())) != null) { + prototypeCheck(thisProp); if (!(thisProp in subObject)) { subObject[thisProp] = {}; } @@ -84,17 +84,11 @@ function diffApply(obj, diff, pathConverter) { } if (thisOp === REMOVE || thisOp === REPLACE) { if (!subObject.hasOwnProperty(lastProp)) { - throw new Error( - ['expected to find property', thisDiff.path, 'in object', obj].join( - ' ' - ) - ); + throw new Error(['expected to find property', thisDiff.path, 'in object', obj].join(' ')); } } if (thisOp === REMOVE) { - Array.isArray(subObject) - ? subObject.splice(lastProp, 1) - : delete subObject[lastProp]; + Array.isArray(subObject) ? subObject.splice(lastProp, 1) : delete subObject[lastProp]; } if (thisOp === REPLACE || thisOp === ADD) { subObject[lastProp] = thisDiff.value; @@ -107,4 +101,11 @@ function jsonPatchPathConverter(stringPath) { return stringPath.split('/').slice(1); } +function prototypeCheck(prop) { + // coercion is intentional to catch prop values like `['__proto__']` + if (prop == '__proto__' || prop == 'constructor' || prop == 'prototype') { + throw new Error('setting of prototype values not supported'); + } +} + export {diffApply, jsonPatchPathConverter}; diff --git a/node_modules/just-diff-apply/index.tests.ts b/node_modules/just-diff-apply/index.tests.ts new file mode 100644 index 0000000000000..d02ba89b838cd --- /dev/null +++ b/node_modules/just-diff-apply/index.tests.ts @@ -0,0 +1,108 @@ +import * as diffObj from "./index"; + +const { diffApply, jsonPatchPathConverter } = diffObj; +const obj1 = { + a: 2, + b: 3, + c: { + d: 5 + } +}; +const arr1 = [1, "bee"]; + +const objOps: diffObj.DiffOps = [ + { + op: "replace", + path: ["a"], + value: 10 + }, + { + op: "remove", + path: ["b"] + }, + { + op: "add", + path: ["e"], + value: 15 + }, + { + op: "remove", + path: ["c", "d"] + } +]; + +const arrOps: diffObj.DiffOps = [ + { + op: "replace", + path: [1], + value: 10 + }, + { + op: "remove", + path: [2] + }, + { + op: "add", + path: [7], + value: 15 + } +]; + +//OK +diffApply(obj1, objOps); +diffApply(obj1, []); +diffApply(arr1, arrOps); +diffApply(arr1, []); +diffApply(obj1, objOps, jsonPatchPathConverter); +diffApply(arr1, arrOps, jsonPatchPathConverter); + +// not OK +// @ts-expect-error +diffApply(obj1); +// @ts-expect-error +diffApply(arr2); +// @ts-expect-error +diffApply("a"); +// @ts-expect-error +diffApply(true); + +// @ts-expect-error +diffApply(obj1, 1); +// @ts-expect-error +diffApply(3, arr2); +// @ts-expect-error +diffApply(obj1, "a"); +// @ts-expect-error +diffApply("b", arr2); + +// @ts-expect-error +diffApply(obj1, [{ op: "delete", path: ["a"] }]); +// @ts-expect-error +diffApply(obj1, [{ op: "delete", path: ["a"] }], jsonPatchPathConverter); +// @ts-expect-error +diffApply(obj1, "a", jsonPatchPathConverter); +// @ts-expect-error +diffApply(obj1, ["a", "b", "c"], jsonPatchPathConverter); + +// @ts-expect-error +diff("a", jsonPatchPathConverter); +// @ts-expect-error +diff(true, jsonPatchPathConverter); + +// @ts-expect-error +diff(obj1, 1, jsonPatchPathConverter); +// @ts-expect-error +diff(3, arr2, jsonPatchPathConverter); +// @ts-expect-error +diff(obj1, "a", jsonPatchPathConverter); +// @ts-expect-error +diff("b", arr2, jsonPatchPathConverter); + +// @ts-expect-error +diff(obj1, obj2, "a"); +// @ts-expect-error +diff(arr1, arr2, 1); +// @ts-expect-error +diff(obj1, arr1, "bee"); +// @ts-expect-error +diff(obj2, arr2, "nope"); diff --git a/node_modules/just-diff-apply/package.json b/node_modules/just-diff-apply/package.json index c38bd47aa6990..b8e5012ff83b1 100644 --- a/node_modules/just-diff-apply/package.json +++ b/node_modules/just-diff-apply/package.json @@ -1,6 +1,6 @@ { "name": "just-diff-apply", - "version": "4.0.1", + "version": "5.2.0", "description": "Apply a diff to an object. Optionally supports jsonPatch protocol", "main": "index.js", "module": "index.mjs", @@ -10,6 +10,7 @@ "default": "./index.mjs" } }, + "types": "index.d.ts", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "build": "rollup -c" diff --git a/node_modules/parse-conflict-json/package.json b/node_modules/parse-conflict-json/package.json index bb633e158b5d1..5dab68d52f7ca 100644 --- a/node_modules/parse-conflict-json/package.json +++ b/node_modules/parse-conflict-json/package.json @@ -1,6 +1,6 @@ { "name": "parse-conflict-json", - "version": "2.0.1", + "version": "2.0.2", "description": "Parse a JSON string that has git merge conflicts, resolving if possible", "author": "GitHub Inc.", "license": "ISC", @@ -11,34 +11,39 @@ "preversion": "npm test", "postversion": "npm publish", "postpublish": "git push origin --follow-tags", - "lint": "eslint '**/*.js'", - "postlint": "npm-template-check", + "lint": "eslint \"**/*.js\"", + "postlint": "template-oss-check", "lintfix": "npm run lint -- --fix", "prepublishOnly": "git push origin --follow-tags", - "posttest": "npm run lint" + "posttest": "npm run lint", + "template-oss-apply": "template-oss-apply --force" }, "tap": { "check-coverage": true }, "devDependencies": { - "@npmcli/template-oss": "^2.3.1", - "tap": "^15.1.5" + "@npmcli/eslint-config": "^3.0.1", + "@npmcli/template-oss": "3.2.0", + "tap": "^16.0.1" }, "dependencies": { "json-parse-even-better-errors": "^2.3.1", "just-diff": "^5.0.1", - "just-diff-apply": "^4.0.1" + "just-diff-apply": "^5.2.0" }, "repository": { "type": "git", - "url": "git+https://github.com/npm/parse-conflict-json.git" + "url": "https://github.com/npm/parse-conflict-json.git" }, "files": [ - "bin", - "lib" + "bin/", + "lib/" ], - "templateVersion": "2.3.1", "engines": { - "node": "^12.13.0 || ^14.15.0 || >=16" + "node": "^12.13.0 || ^14.15.0 || >=16.0.0" + }, + "templateOSS": { + "//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.", + "version": "3.2.0" } } diff --git a/package-lock.json b/package-lock.json index cf6fe7ab80577..30b7d79734d95 100644 --- a/package-lock.json +++ b/package-lock.json @@ -142,7 +142,7 @@ "npmlog": "^6.0.1", "opener": "^1.5.2", "pacote": "^13.0.5", - "parse-conflict-json": "^2.0.1", + "parse-conflict-json": "^2.0.2", "proc-log": "^2.0.1", "qrcode-terminal": "^0.12.0", "read": "~1.0.7", @@ -4628,9 +4628,9 @@ "inBundle": true }, "node_modules/just-diff-apply": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/just-diff-apply/-/just-diff-apply-4.0.1.tgz", - "integrity": "sha512-AKOkzB5P6FkfP21UlZVX/OPXx/sC2GagpLX9cBxqHqDuRjwmZ/AJRKSNrB9jHPpRW1W1ONs6gly1gW46t055nQ==", + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/just-diff-apply/-/just-diff-apply-5.2.0.tgz", + "integrity": "sha512-unjtin7rnng0KUpE4RPWwTl8iwWiZuyZqOQ+vm8orV6aIXX8mHN8zlKCPPbOycfDNuLh2PBazbFhNoDJv4S/FA==", "inBundle": true }, "node_modules/lcov-parse": { @@ -5839,17 +5839,17 @@ } }, "node_modules/parse-conflict-json": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/parse-conflict-json/-/parse-conflict-json-2.0.1.tgz", - "integrity": "sha512-Y7nYw+QaSGBto1LB9lgwOR05Rtz5SbuTf+Oe7HJ6SYQ/DHsvRjQ8O03oWdJbvkt6GzDWospgyZbGmjDYL0sDgA==", + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/parse-conflict-json/-/parse-conflict-json-2.0.2.tgz", + "integrity": "sha512-jDbRGb00TAPFsKWCpZZOT93SxVP9nONOSgES3AevqRq/CHvavEBvKAjxX9p5Y5F0RZLxH9Ufd9+RwtCsa+lFDA==", "inBundle": true, "dependencies": { "json-parse-even-better-errors": "^2.3.1", "just-diff": "^5.0.1", - "just-diff-apply": "^4.0.1" + "just-diff-apply": "^5.2.0" }, "engines": { - "node": "^12.13.0 || ^14.15.0 || >=16" + "node": "^12.13.0 || ^14.15.0 || >=16.0.0" } }, "node_modules/parse-entities": { @@ -14027,9 +14027,9 @@ "integrity": "sha512-X00TokkRIDotUIf3EV4xUm6ELc/IkqhS/vPSHdWnsM5y0HoNMfEqrazizI7g78lpHvnRSRt/PFfKtRqJCOGIuQ==" }, "just-diff-apply": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/just-diff-apply/-/just-diff-apply-4.0.1.tgz", - "integrity": "sha512-AKOkzB5P6FkfP21UlZVX/OPXx/sC2GagpLX9cBxqHqDuRjwmZ/AJRKSNrB9jHPpRW1W1ONs6gly1gW46t055nQ==" + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/just-diff-apply/-/just-diff-apply-5.2.0.tgz", + "integrity": "sha512-unjtin7rnng0KUpE4RPWwTl8iwWiZuyZqOQ+vm8orV6aIXX8mHN8zlKCPPbOycfDNuLh2PBazbFhNoDJv4S/FA==" }, "lcov-parse": { "version": "1.0.0", @@ -15193,13 +15193,13 @@ } }, "parse-conflict-json": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/parse-conflict-json/-/parse-conflict-json-2.0.1.tgz", - "integrity": "sha512-Y7nYw+QaSGBto1LB9lgwOR05Rtz5SbuTf+Oe7HJ6SYQ/DHsvRjQ8O03oWdJbvkt6GzDWospgyZbGmjDYL0sDgA==", + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/parse-conflict-json/-/parse-conflict-json-2.0.2.tgz", + "integrity": "sha512-jDbRGb00TAPFsKWCpZZOT93SxVP9nONOSgES3AevqRq/CHvavEBvKAjxX9p5Y5F0RZLxH9Ufd9+RwtCsa+lFDA==", "requires": { "json-parse-even-better-errors": "^2.3.1", "just-diff": "^5.0.1", - "just-diff-apply": "^4.0.1" + "just-diff-apply": "^5.2.0" } }, "parse-entities": { diff --git a/package.json b/package.json index 8b91e3f4d66f4..664421f81bc01 100644 --- a/package.json +++ b/package.json @@ -109,7 +109,7 @@ "npmlog": "^6.0.1", "opener": "^1.5.2", "pacote": "^13.0.5", - "parse-conflict-json": "^2.0.1", + "parse-conflict-json": "^2.0.2", "proc-log": "^2.0.1", "qrcode-terminal": "^0.12.0", "read": "~1.0.7",