Skip to content

Commit c0095a3

Browse files
authored
Automatically trim redundant mixins (#1399)
Co-authored-by: saschanaz <saschanaz@users.noreply.github.com>
1 parent f89d4cf commit c0095a3

File tree

3 files changed

+46
-42
lines changed

3 files changed

+46
-42
lines changed

inputfiles/removedTypes.jsonc

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,7 @@
5151
"elementFromPoint": null,
5252
"elementsFromPoint": null
5353
}
54-
},
55-
"implements": ["GeometryUtils"]
56-
},
57-
"Element": {
58-
"implements": ["GeometryUtils", "Region"]
59-
},
60-
"Navigator": {
61-
"implements": [
62-
"NavigatorBadge",
63-
"NavigatorDeviceMemory",
64-
"NavigatorFonts",
65-
"NavigatorGPU",
66-
"NavigatorML",
67-
"NavigatorNetworkInformation",
68-
"NavigatorUA"
69-
]
70-
},
71-
"NetworkInformation": {
72-
"implements": ["NetworkInformationSaveData"]
54+
}
7355
},
7456
"Response": {
7557
"methods": {
@@ -91,9 +73,6 @@
9173
}
9274
}
9375
},
94-
"SVGElement": {
95-
"implements": ["SVGElementInstance"]
96-
},
9776
"SVGFEGaussianBlurElement": {
9877
"constants": {
9978
"constant": {
@@ -104,9 +83,6 @@
10483
}
10584
}
10685
},
107-
"Text": {
108-
"implements": ["GeometryUtils"]
109-
},
11086
"WebGLBuffer": {
11187
"extends": null
11288
},
@@ -142,17 +118,6 @@
142118
},
143119
"WebGLVertexArrayObjectOES": {
144120
"extends": null
145-
},
146-
"WorkerNavigator": {
147-
"implements": [
148-
"NavigatorBadge",
149-
"NavigatorDeviceMemory",
150-
"NavigatorFonts",
151-
"NavigatorGPU",
152-
"NavigatorML",
153-
"NavigatorNetworkInformation",
154-
"NavigatorUA"
155-
]
156121
}
157122
}
158123
},
@@ -576,6 +541,18 @@
576541
}
577542
}
578543
},
544+
"mixins": {
545+
"mixin":{
546+
"NavigatorNetworkInformation": {
547+
"properties": {
548+
"property": {
549+
// https://github.com/mdn/browser-compat-data/pull/17824
550+
"connection": null
551+
}
552+
}
553+
}
554+
}
555+
},
579556
"typedefs": {
580557
"typedef": [
581558
"ArrayBufferView"

src/build/bcd.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ function isSuitable(
5252

5353
export function getRemovalData(webidl: Browser.WebIdl): Browser.WebIdl {
5454
return mapToBcdCompat(webidl, ({ key, parentKey, compat, mixin }) => {
55-
// Allow all mixins for now, but not their members
56-
// Ultimately expose.ts should be updated to check empty mixins
55+
// Allow all mixins here, but not their members.
56+
// Empty mixins created by this will be managed by exposed.ts.
57+
// (It's better to manage mixins there as mixins can also conditionally be empty by exposure settings)
5758
if (mixin && !parentKey) {
5859
return;
5960
}

src/build/expose.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
mapToArray,
99
arrayToMap,
1010
} from "./helpers.js";
11+
import { isEmptyRecord } from "./utils/record.js";
1112

1213
class LoggedSet extends Set<string> {
1314
private unvisited: Set<string>;
@@ -52,6 +53,24 @@ export function getExposedTypes(
5253
);
5354
}
5455

56+
if (webidl.mixins) {
57+
const allIncludes = Object.values(filtered.interfaces?.interface || {})
58+
.map((i) => i.implements || [])
59+
.flat();
60+
const mixins = deepFilter(webidl.mixins.mixin, (o) => exposesTo(o, target));
61+
filtered.mixins!.mixin = filterProperties(
62+
mixins,
63+
(m: Browser.Interface) => allIncludes.includes(m.name) && !isEmptyMixin(m)
64+
);
65+
for (const value of Object.values(filtered.interfaces!.interface || {})) {
66+
if (value.implements) {
67+
value.implements = value.implements.filter(
68+
(i) => !!filtered.mixins!.mixin[i]
69+
);
70+
}
71+
}
72+
}
73+
5574
const knownIDLTypes = new Set([
5675
...followTypeReferences(webidl, filtered.interfaces!.interface),
5776
...followTypeReferences(
@@ -92,10 +111,6 @@ export function getExposedTypes(
92111
);
93112
if (webidl.enums)
94113
filtered.enums!.enum = filterProperties(webidl.enums.enum, isKnownName);
95-
if (webidl.mixins) {
96-
const mixins = deepFilter(webidl.mixins.mixin, (o) => exposesTo(o, target));
97-
filtered.mixins!.mixin = filterProperties(mixins, isKnownName);
98-
}
99114

100115
for (const unvisited of forceKnownTypesLogged.unvisitedValues()) {
101116
console.warn(`${unvisited} is redundant in knownTypes.json (${target})`);
@@ -264,3 +279,14 @@ function flattenType(type: Browser.Typed[]) {
264279
}
265280
throw new Error("Cannot process empty union type");
266281
}
282+
283+
function isEmptyMixin(i?: Browser.Interface) {
284+
return (
285+
!!i?.mixin &&
286+
isEmptyRecord(i.properties?.property) &&
287+
isEmptyRecord(i.methods?.method) &&
288+
isEmptyRecord(i.constants?.constant) &&
289+
!i.anonymousMethods?.method.length &&
290+
!i.events?.event.length
291+
);
292+
}

0 commit comments

Comments
 (0)