json-conflict-resolver
Version:
A rules-based JSON conflict resolver that parses Git conflict markers, reconstructs ours/theirs, and merges with deterministic strategies — beyond line-based merges.
55 lines (54 loc) • 2.39 kB
TypeScript
import { Config, SupportedParsers } from "./types";
/**
* Represents a parsed conflict from a file with `ours` and `theirs` versions.
*
* @template T - The type of the parsed content.
*/
export interface ParsedConflict<T = unknown> {
/** Parsed content from the "ours" side of the conflict. */
ours: T;
/** Parsed content from the "theirs" side of the conflict. */
theirs: T;
/** Parsed content from the "base" side of the conflict (optional). */
base?: T;
/** Format used to parse the content (`json`, `yaml`, `toml`, `xml`, or `custom`). */
format: string;
}
/**
* Options for parsing conflicted content.
*/
export interface ParseConflictOptions extends Pick<Config, "parsers"> {
/**
* filename hint to prioritize parser choice as well as get base and ours from git.
* Example:
* - `config.yaml` → try `yaml` first.
* - `data.toml` → try `toml` first.
*
* If extension is unknown, falls back to `parsers` or `"json"`.
*/
filename: string;
}
/**
* Parses a conflicted file's content into separate `ours` and `theirs` objects.
*
* - Preserves non-conflicted lines in both versions.
* - Supports JSON, JSON5, YAML, TOML, and XML.
* - Lazy-loads optional peer dependencies (`json5`, `yaml`, `toml`, `fast-xml-parser`).
* - Parser order is determined by:
* 1. `filename` extension hint (if provided).
* 2. Explicit `parsers` option.
* 3. Default `"json"`.
*
* @template T - Expected type of parsed content.
* @param content - Raw file content containing conflict markers.
* @param options - Parsing options (parsers + filename hint).
* @returns Parsed conflict with both sides and detected format.
* @throws If parsing fails or conflict markers are invalid.
*/
export declare const parseConflictContent: <T = unknown>(content: string, options: ParseConflictOptions) => Promise<ParsedConflict<T>>;
/** Normalize parsers based on filename + options. */
export declare const normalizeParsers: (options: ParseConflictOptions) => SupportedParsers[];
/** Internal helper to try parsers in order. */
export declare const runParser: (raw: string, parsers: SupportedParsers[]) => Promise<[unknown, SupportedParsers]>;
/** Internal parser dispatcher for supported formats. */
export declare const parseFormat: (parser: "json" | "json5" | "yaml" | "toml" | "xml", raw: string) => Promise<unknown>;