Skip to content

Commit

Permalink
refactor(yaml): inline mergeMappings() (denoland#5777)
Browse files Browse the repository at this point in the history
  • Loading branch information
timreichen authored Aug 22, 2024
1 parent 1d2d33a commit bb0db58
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions yaml/_loader_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,28 @@ export class LoaderState {
}
return false;
}
mergeMappings(
destination: Record<string, unknown>,
source: Record<string, unknown>,
overridableKeys: Set<string>,
) {
if (!isObject(source)) {
return this.throwError(
"cannot merge mappings; the provided source object is unacceptable",
);
}

for (const [key, value] of Object.entries(source)) {
if (Object.hasOwn(destination, key)) continue;
Object.defineProperty(destination, key, {
value,
writable: true,
enumerable: true,
configurable: true,
});
overridableKeys.add(key);
}
}

readDocument() {
const documentStart = this.position;
Expand Down Expand Up @@ -475,30 +497,6 @@ export class LoaderState {
}
}

function mergeMappings(
state: LoaderState,
destination: Record<string, unknown>,
source: Record<string, unknown>,
overridableKeys: Set<string>,
) {
if (!isObject(source)) {
return state.throwError(
"cannot merge mappings; the provided source object is unacceptable",
);
}

for (const [key, value] of Object.entries(source)) {
if (Object.hasOwn(destination, key)) continue;
Object.defineProperty(destination, key, {
value,
writable: true,
enumerable: true,
configurable: true,
});
overridableKeys.add(key);
}
}

function storeMappingPair(
state: LoaderState,
result: Record<string, unknown> | null,
Expand Down Expand Up @@ -552,11 +550,10 @@ function storeMappingPair(
index < valueNode.length;
index++
) {
mergeMappings(state, result, valueNode[index], overridableKeys);
state.mergeMappings(result, valueNode[index], overridableKeys);
}
} else {
mergeMappings(
state,
state.mergeMappings(
result,
valueNode as Record<string, unknown>,
overridableKeys,
Expand Down

0 comments on commit bb0db58

Please sign in to comment.