diff options
author | claudiahdz <cghr1990@gmail.com> | 2020-02-19 00:38:25 +0300 |
---|---|---|
committer | isaacs <i@izs.me> | 2020-05-08 04:11:51 +0300 |
commit | 09dac89c1bc20eb2c1f1f272c9e07fc1a579d75a (patch) | |
tree | 64335e20df7d982ec16b9c4e57a86182de0583af /node_modules/parse-conflict-json | |
parent | d594d8e55241a7a8abf7a61c97fc41c75fc9267f (diff) |
@npmcli/arborist@0.0.0-pre.7
Diffstat (limited to 'node_modules/parse-conflict-json')
-rw-r--r-- | node_modules/parse-conflict-json/LICENSE | 15 | ||||
-rw-r--r-- | node_modules/parse-conflict-json/README.md | 39 | ||||
-rw-r--r-- | node_modules/parse-conflict-json/index.js | 92 | ||||
-rw-r--r-- | node_modules/parse-conflict-json/package.json | 65 |
4 files changed, 211 insertions, 0 deletions
diff --git a/node_modules/parse-conflict-json/LICENSE b/node_modules/parse-conflict-json/LICENSE new file mode 100644 index 000000000..20a476254 --- /dev/null +++ b/node_modules/parse-conflict-json/LICENSE @@ -0,0 +1,15 @@ +The ISC License + +Copyright (c) npm, Inc. and Contributors + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR +IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/node_modules/parse-conflict-json/README.md b/node_modules/parse-conflict-json/README.md new file mode 100644 index 000000000..d4ff7ef09 --- /dev/null +++ b/node_modules/parse-conflict-json/README.md @@ -0,0 +1,39 @@ +# parse-conflict-json + +Parse a JSON string that has git merge conflicts, resolving if possible. + +If the JSON is valid, it just does `JSON.parse` as normal. + +If either side of the conflict is invalid JSON, then an error is thrown for +that. + +## USAGE + +```js +// after a git merge that left some conflicts there +const data = fs.readFileSync('package-lock.json', 'utf8') + +// reviverFunction is passed to JSON.parse as the reviver function +// preference defaults to 'ours', set to 'theirs' to prefer the other +// side's changes. +const parsed = parseConflictJson(data, reviverFunction, preference) +``` + +## Algorithm + +If `prefer` is set to `theirs`, then the vaules of `theirs` and `ours` are +switched in the resolver function. (Ie, we'll apply their changes on top +of our object, rather than the other way around.) + +- Parse the conflicted file into 3 pieces: `ours`, `theirs`, and `parent` + +- Get the [diff](https://github.com/angus-c/just#just-diff) from `parent` + to `ours`. + +- [Apply](https://github.com/angus-c/just#just-diff-apply) each change of + that diff to `theirs`. + + If any change in the diff set cannot be applied (ie, because they + changed an object into a non-object and we changed a field on that + object), then replace the object at the specified path with the object + at the path in `ours`. diff --git a/node_modules/parse-conflict-json/index.js b/node_modules/parse-conflict-json/index.js new file mode 100644 index 000000000..8d230cca5 --- /dev/null +++ b/node_modules/parse-conflict-json/index.js @@ -0,0 +1,92 @@ +const parseJSON = require('json-parse-better-errors') +const { diff } = require('just-diff') +const { diffApply } = require('just-diff-apply') + +const stripBOM = content => { + content = content.toString() + // Remove byte order marker. This catches EF BB BF (the UTF-8 BOM) + // because the buffer-to-string conversion in `fs.readFileSync()` + // translates it to FEFF, the UTF-16 BOM. + if (content.charCodeAt(0) === 0xFEFF) + content = content.slice(1) + return content +} + +const PARENT_RE = /\|{7,}/g +const OURS_RE = /<{7,}/g +const THEIRS_RE = /={7,}/g +const END_RE = />{7,}/g + +const isDiff = str => + str.match(OURS_RE) && str.match(THEIRS_RE) && str.match(END_RE) + +const parseConflictJSON = (str, reviver, prefer) => { + prefer = prefer || 'ours' + if (prefer !== 'theirs' && prefer !== 'ours') + throw new TypeError('prefer param must be "ours" or "theirs" if set') + + str = stripBOM(str) + + if (!isDiff(str)) + return parseJSON(str) + + const pieces = str.split(/[\n\r]+/g).reduce((acc, line) => { + if (line.match(PARENT_RE)) + acc.state = 'parent' + else if (line.match(OURS_RE)) + acc.state = 'ours' + else if (line.match(THEIRS_RE)) + acc.state = 'theirs' + else if (line.match(END_RE)) + acc.state = 'top' + else { + if (acc.state === 'top' || acc.state === 'ours') + acc.ours += line + if (acc.state === 'top' || acc.state === 'theirs') + acc.theirs += line + if (acc.state === 'top' || acc.state === 'parent') + acc.parent += line + } + return acc + }, { + state: 'top', + ours: '', + theirs: '', + parent: '' + }) + + // this will throw if either piece is not valid JSON, that's intended + const parent = parseJSON(pieces.parent, reviver) + const ours = parseJSON(pieces.ours, reviver) + const theirs = parseJSON(pieces.theirs, reviver) + + return prefer === 'ours' + ? resolve(parent, ours, theirs) + : resolve(parent, theirs, ours) +} + +const isObj = obj => obj && typeof obj === 'object' + +const copyPath = (to, from, path, i) => { + const p = path[i] + if (isObj(to[p]) && isObj(from[p]) && + Array.isArray(to[p]) === Array.isArray(from[p])) + return copyPath(to[p], from[p], path, i + 1) + to[p] = from[p] +} + +// get the diff from parent->ours and applying our changes on top of theirs. +// If they turned an object into a non-object, then put it back. +const resolve = (parent, ours, theirs) => { + const dours = diff(parent, ours) + for (let i = 0; i < dours.length; i++) { + try { + diffApply(theirs, [dours[i]]) + } catch (e) { + copyPath(theirs, ours, dours[i].path, 0) + } + } + return theirs +} + +module.exports = parseConflictJSON diff --git a/node_modules/parse-conflict-json/package.json b/node_modules/parse-conflict-json/package.json new file mode 100644 index 000000000..b2935bec5 --- /dev/null +++ b/node_modules/parse-conflict-json/package.json @@ -0,0 +1,65 @@ +{ + "_from": "parse-conflict-json@^1.0.0", + "_id": "parse-conflict-json@1.0.1", + "_inBundle": false, + "_integrity": "sha512-htogfp+nUg/kCA7SnyH6Z0ZRAp6Pij1pgGeXWpbXCFCEdlC1S/sGcu6bUScj3cr140PU5fGKzjwm5i5rdQBWCw==", + "_location": "/parse-conflict-json", + "_phantomChildren": {}, + "_requested": { + "type": "range", + "registry": true, + "raw": "parse-conflict-json@^1.0.0", + "name": "parse-conflict-json", + "escapedName": "parse-conflict-json", + "rawSpec": "^1.0.0", + "saveSpec": null, + "fetchSpec": "^1.0.0" + }, + "_requiredBy": [ + "/@npmcli/arborist" + ], + "_resolved": "https://registry.npmjs.org/parse-conflict-json/-/parse-conflict-json-1.0.1.tgz", + "_shasum": "780b559bda5d6fd64846291dde9a38704a2132d2", + "_spec": "parse-conflict-json@^1.0.0", + "_where": "/Users/claudiahdz/npm/cli/node_modules/@npmcli/arborist", + "author": { + "name": "Isaac Z. Schlueter", + "email": "i@izs.me", + "url": "https://izs.me" + }, + "bugs": { + "url": "https://github.com/npm/parse-conflict-json/issues" + }, + "bundleDependencies": false, + "dependencies": { + "json-parse-better-errors": "^1.0.2", + "just-diff": "^3.0.1", + "just-diff-apply": "^3.0.0" + }, + "deprecated": false, + "description": "Parse a JSON string that has git merge conflicts, resolving if possible", + "devDependencies": { + "tap": "^14.6.1" + }, + "files": [ + "index.js" + ], + "homepage": "https://github.com/npm/parse-conflict-json#readme", + "license": "ISC", + "name": "parse-conflict-json", + "repository": { + "type": "git", + "url": "git+https://github.com/npm/parse-conflict-json.git" + }, + "scripts": { + "postpublish": "git push origin --follow-tags", + "postversion": "npm publish", + "preversion": "npm test", + "snap": "tap", + "test": "tap" + }, + "tap": { + "check-coverage": true + }, + "version": "1.0.1" +} |