Skip to content

Commit ea1a6a5

Browse files
committed
shallow merge
1 parent e3f76da commit ea1a6a5

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

src/helpers.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function filterProperties<T>(obj: Record<string, T>, fn: (o: T) => boolea
2626
return result;
2727
}
2828

29-
export function merge<T>(src: T, target: T): T {
29+
export function merge<T>(src: T, target: T, shallow?: boolean): T {
3030
if (typeof src !== "object" || typeof target !== "object") {
3131
return target;
3232
}
@@ -42,7 +42,12 @@ export function merge<T>(src: T, target: T): T {
4242
if (Array.isArray(srcProp) !== Array.isArray(targetProp)) {
4343
throw new Error("Mismatch on property: " + k + JSON.stringify(targetProp));
4444
}
45-
src[k] = merge(src[k], target[k]);
45+
if (shallow && "name" in src[k] && "name" in target[k]) {
46+
src[k] = target[k];
47+
}
48+
else {
49+
src[k] = merge(src[k], target[k], shallow);
50+
}
4651
}
4752
}
4853
else {
@@ -53,17 +58,19 @@ export function merge<T>(src: T, target: T): T {
5358
return src;
5459
}
5560

56-
function mergeNamedArrays<T extends { name: string }>(srcProp: T[], targetProp: T[]) {
61+
function mergeNamedArrays<T extends { name: string; "new-type": string; }>(srcProp: T[], targetProp: T[]) {
5762
const map: any = {};
5863
for (const e1 of srcProp) {
59-
if (e1.name) {
60-
map[e1.name] = e1;
64+
const name = e1.name || e1["new-type"];
65+
if (name) {
66+
map[name] = e1;
6167
}
6268
}
6369

6470
for (const e2 of targetProp) {
65-
if (e2.name && map[e2.name]) {
66-
merge(map[e2.name], e2);
71+
const name = e2.name || e2["new-type"];
72+
if (name && map[name]) {
73+
merge(map[name], e2);
6774
}
6875
else {
6976
srcProp.push(e2);
@@ -116,7 +123,7 @@ export function isArray(value: any): value is ReadonlyArray<{}> {
116123
return Array.isArray ? Array.isArray(value) : value instanceof Array;
117124
}
118125

119-
export function flatMap<T, U>(array: ReadonlyArray<T> | undefined, mapfn: (x: T, i: number) => U | ReadonlyArray<U> | undefined): U[] {
126+
export function flatMap<T, U>(array: ReadonlyArray<T> | undefined, mapfn: (x: T, i: number) => U | ReadonlyArray<U> | undefined): U[] {
120127
let result: U[] | undefined;
121128
if (array) {
122129
result = [];
@@ -137,4 +144,4 @@ export function flatMap<T, U>(array: ReadonlyArray<T> | undefined, mapfn: (x: T,
137144

138145
export function concat<T>(a: T[] | undefined, b: T[] | undefined): T[] {
139146
return !a ? b || [] : a.concat(b || []);
140-
}
147+
}

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ function emitDom() {
9292
const knownWorkerTypes = new Set<string>(require(path.join(inputFolder, "knownWorkerTypes.json")));
9393

9494
for (const w of widlStandardTypes) {
95-
webidl = merge(webidl, w.browser);
95+
webidl = merge(webidl, w.browser, true);
9696
}
9797
webidl = prune(webidl, removedItems);
9898
webidl = merge(webidl, addedItems);

src/widlprocess.ts

-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ function convertArgument(arg: webidl2.Argument): Browser.Param {
138138
return {
139139
name: arg.name,
140140
...convertIdlType(arg.idlType),
141-
nullable: arg.idlType.nullable ? 1 : undefined,
142141
optional: arg.optional ? 1 : undefined,
143142
variadic: arg.variadic ? 1 : undefined,
144143
}

0 commit comments

Comments
 (0)