mirror of
https://github.com/morluto/rea.git
synced 2026-12-05 14:31:22 +00:00
refactor(config): reuse YAML source-key resolution (#16769)
This commit is contained in:
1 parent
fe2368b35f
commit
1d22bd0580
1 file changed
+8
-9
@@ -296,21 +296,17 @@ const retainYamlMapSource = (
|
||||
document: Document,
|
||||
): void => {
|
||||
const hasMerges = retainYamlMergeSources(previous, next, document);
|
||||
const hasSourceOnlyKeys = retainProjectedYamlKeys(
|
||||
const { hasSourceOnlyKeys, entries } = retainProjectedYamlKeys(
|
||||
previous,
|
||||
next,
|
||||
retainedSources,
|
||||
document,
|
||||
);
|
||||
const previousKeys = resolvedStringKeys(previous, document);
|
||||
// The source pair each rewritten pair replaces, including aliased keys.
|
||||
const matched = new Map<
|
||||
(typeof next.items)[number],
|
||||
(typeof previous.items)[number]
|
||||
>();
|
||||
const entries = new Map(
|
||||
[...previousKeys].map(([pair, key]) => [key, pair] as const),
|
||||
);
|
||||
for (const pair of next.items) {
|
||||
if (previous.items.includes(pair)) continue;
|
||||
const key = isScalar(pair.key) ? pair.key.value : undefined;
|
||||
@@ -406,18 +402,21 @@ const retainProjectedYamlKeys = (
|
||||
next: YAMLMap,
|
||||
retainedSources: ReadonlySet<Node>,
|
||||
document: Document,
|
||||
): boolean => {
|
||||
) => {
|
||||
// A JS property can represent several distinct native YAML keys. Keep
|
||||
// those source pairs together when the property was not edited.
|
||||
const groups = new Map<string, typeof previous.items>();
|
||||
const sourceOnlyKeys = new Set<string>();
|
||||
const entries = new Map<string, (typeof previous.items)[number]>();
|
||||
const stringKeys = resolvedStringKeys(previous, document);
|
||||
for (const pair of previous.items) {
|
||||
if (isYamlMergeKey(pair.key)) continue;
|
||||
let key: string;
|
||||
const stringKey = stringKeys.get(pair);
|
||||
if (stringKey !== undefined) key = stringKey;
|
||||
else {
|
||||
if (stringKey !== undefined) {
|
||||
key = stringKey;
|
||||
entries.set(key, pair);
|
||||
} else {
|
||||
const singleton = new YAMLMap(document.schema);
|
||||
singleton.items.push(new Pair(pair.key, null));
|
||||
const projected: unknown = singleton.toJS(document);
|
||||
@@ -482,7 +481,7 @@ const retainProjectedYamlKeys = (
|
||||
return replacement ?? [pair];
|
||||
});
|
||||
}
|
||||
return sourceOnlyKeys.size > 0;
|
||||
return { hasSourceOnlyKeys: sourceOnlyKeys.size > 0, entries };
|
||||
};
|
||||
|
||||
/** Keep a source spelling only while the selected schema resolves its value unchanged. */
|
||||
|
||||
Reference in new issue
Block a user