From 964a95ea8d1726e62e6f79c71d3768413f0dea06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20B=C5=82a=C5=BCejewicz=20=28Peter=20Blazejewicz=29?= Date: Thu, 27 Aug 2020 04:08:12 +0200 Subject: [PATCH] feat(parse-conflict-json): new definition (#47041) Small module to work with JSON files: - definition file - tests https://github.com/npm/parse-conflict-json https://github.com/npm/parse-conflict-json#usage Thanks! --- types/parse-conflict-json/index.d.ts | 36 +++++++++++++++++++ .../parse-conflict-json-tests.ts | 12 +++++++ types/parse-conflict-json/tsconfig.json | 23 ++++++++++++ types/parse-conflict-json/tslint.json | 1 + 4 files changed, 72 insertions(+) create mode 100644 types/parse-conflict-json/index.d.ts create mode 100644 types/parse-conflict-json/parse-conflict-json-tests.ts create mode 100644 types/parse-conflict-json/tsconfig.json create mode 100644 types/parse-conflict-json/tslint.json diff --git a/types/parse-conflict-json/index.d.ts b/types/parse-conflict-json/index.d.ts new file mode 100644 index 0000000000..345a600c79 --- /dev/null +++ b/types/parse-conflict-json/index.d.ts @@ -0,0 +1,36 @@ +// Type definitions for parse-conflict-json 1.1 +// Project: https://github.com/npm/parse-conflict-json#readme +// Definitions by: Piotr Błażejewicz +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +/// + +/** + * 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. + */ +declare function parseConflictJSON( + text: string, + reviver?: (this: any, key: string, value: any) => any, + prefer?: parseConflictJSON.Prefer, +): any; + +declare namespace parseConflictJSON { + /** + * returns true if the data looks like a conflicted diff file + */ + function isDiff(text: string): boolean; + + /** + * 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 from parent to ours. + * 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. + */ + type Prefer = 'theirs' | 'ours'; +} + +export = parseConflictJSON; diff --git a/types/parse-conflict-json/parse-conflict-json-tests.ts b/types/parse-conflict-json/parse-conflict-json-tests.ts new file mode 100644 index 0000000000..75a7e39e2e --- /dev/null +++ b/types/parse-conflict-json/parse-conflict-json-tests.ts @@ -0,0 +1,12 @@ +import parseConflictJson = require('parse-conflict-json'); +import fs = require('fs'); + +const data = fs.readFileSync('tsconfig.json', 'utf8'); + +parseConflictJson.isDiff(data); // $ExpectType boolean +parseConflictJson.isDiff(JSON.stringify({})); // $ExpectType boolean + +parseConflictJson(data); // $ExpectType any +parseConflictJson(data, (name, value) => value); // $ExpectType any +parseConflictJson(data, (name, value) => value, 'theirs'); // $ExpectType any +parseConflictJson(data, (name, value) => value, 'ours'); // $ExpectType any diff --git a/types/parse-conflict-json/tsconfig.json b/types/parse-conflict-json/tsconfig.json new file mode 100644 index 0000000000..0ec144db06 --- /dev/null +++ b/types/parse-conflict-json/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "parse-conflict-json-tests.ts" + ] +} diff --git a/types/parse-conflict-json/tslint.json b/types/parse-conflict-json/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/parse-conflict-json/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }