Skip to content

Commit 87dd612

Browse files
committed
refactor(bcd): split mapInterfaceLike
1 parent 82fbfeb commit 87dd612

2 files changed

Lines changed: 77 additions & 69 deletions

File tree

src/build/bcd.ts

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import {
66
SupportBlock,
77
} from "@mdn/browser-compat-data/types";
88
import { camelToHyphenCase } from "./utils/css.js";
9-
import { filterMapRecord, isEmptyRecord } from "./utils/record.js";
10-
import { mapDefined } from "./helpers.js";
119
import { forceKeepAlive } from "./bcd/keep-alive.js";
10+
import { mapToBcdCompat } from "./bcd/mapper.js";
1211

1312
function hasMultipleImplementations(support: SupportBlock, prefix?: string) {
1413
function hasStableImplementation(
@@ -126,73 +125,6 @@ export function getRemovalData(webidl: Browser.WebIdl): Browser.WebIdl {
126125
}) as Browser.WebIdl;
127126
}
128127

129-
interface DataToMap {
130-
key: string;
131-
compat?: CompatStatement;
132-
webkit?: boolean;
133-
mixin: boolean;
134-
parentKey?: string;
135-
}
136-
137-
function mapToBcdCompat(
138-
webidl: Browser.WebIdl,
139-
mapper: (data: DataToMap) => any
140-
): Browser.WebIdl | undefined {
141-
function mapInterfaceLike(name: string, i: Browser.Interface) {
142-
const intCompat = bcd.api[name]?.__compat;
143-
const mapped = mapper({ key: name, compat: intCompat, mixin: !!i.mixin });
144-
if (!intCompat) {
145-
if (mapped) {
146-
return { name: i.name, ...mapped };
147-
}
148-
return;
149-
}
150-
const result = { ...mapped };
151-
152-
const recordMapper = (key: string) => {
153-
const compat = bcd.api[name][key]?.__compat;
154-
return mapper({
155-
key,
156-
parentKey: name,
157-
webkit: key.startsWith("webkit"),
158-
compat,
159-
mixin: !!i.mixin,
160-
});
161-
};
162-
const methods = filterMapRecord(i.methods?.method, recordMapper);
163-
const properties = filterMapRecord(i.properties?.property, recordMapper);
164-
if (!isEmptyRecord(methods)) {
165-
result.methods = { method: methods! };
166-
}
167-
if (!isEmptyRecord(properties)) {
168-
result.properties = { property: properties! };
169-
}
170-
if (!isEmptyRecord(result)) {
171-
return { name: i.name, ...result };
172-
}
173-
}
174-
175-
const interfaces = filterMapRecord(
176-
webidl.interfaces?.interface,
177-
mapInterfaceLike
178-
);
179-
const mixins = filterMapRecord(webidl.mixins?.mixin, mapInterfaceLike);
180-
const namespaces = mapDefined(webidl.namespaces, (n) =>
181-
mapInterfaceLike(n.name, n)
182-
);
183-
if (
184-
!isEmptyRecord(interfaces) ||
185-
!isEmptyRecord(mixins) ||
186-
!isEmptyRecord(namespaces)
187-
) {
188-
return {
189-
interfaces: interfaces && { interface: interfaces },
190-
mixins: mixins && { mixin: mixins },
191-
namespaces,
192-
};
193-
}
194-
}
195-
196128
export function getDeprecationData(webidl: Browser.WebIdl): Browser.WebIdl {
197129
const webkitExceptions = ["webkitLineClamp"];
198130
return mapToBcdCompat(webidl, ({ key, compat, webkit }) => {

src/build/bcd/mapper.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { CompatStatement } from "@mdn/browser-compat-data/types";
2+
import * as Browser from "../types";
3+
import { filterMapRecord, isEmptyRecord } from "../utils/record.js";
4+
import { mapDefined } from "../helpers.js";
5+
import bcd from "@mdn/browser-compat-data";
6+
7+
interface DataToMap {
8+
key: string;
9+
compat?: CompatStatement;
10+
webkit?: boolean;
11+
mixin: boolean;
12+
parentKey?: string;
13+
}
14+
15+
function mapInterfaceLike(
16+
name: string,
17+
i: Browser.Interface,
18+
mapper: (data: DataToMap) => any
19+
) {
20+
const intCompat = bcd.api[name]?.__compat;
21+
const mapped = mapper({ key: name, compat: intCompat, mixin: !!i.mixin });
22+
if (!intCompat) {
23+
if (mapped) {
24+
return { name: i.name, ...mapped };
25+
}
26+
return;
27+
}
28+
const result = { ...mapped };
29+
30+
const recordMapper = (key: string) => {
31+
const compat = bcd.api[name][key]?.__compat;
32+
return mapper({
33+
key,
34+
parentKey: name,
35+
webkit: key.startsWith("webkit"),
36+
compat,
37+
mixin: !!i.mixin,
38+
});
39+
};
40+
const methods = filterMapRecord(i.methods?.method, recordMapper);
41+
const properties = filterMapRecord(i.properties?.property, recordMapper);
42+
if (!isEmptyRecord(methods)) {
43+
result.methods = { method: methods! };
44+
}
45+
if (!isEmptyRecord(properties)) {
46+
result.properties = { property: properties! };
47+
}
48+
if (!isEmptyRecord(result)) {
49+
return { name: i.name, ...result };
50+
}
51+
}
52+
53+
export function mapToBcdCompat(
54+
webidl: Browser.WebIdl,
55+
mapper: (data: DataToMap) => any
56+
): Browser.WebIdl | undefined {
57+
const map = (name: string, i: Browser.Interface) =>
58+
mapInterfaceLike(name, i, mapper);
59+
60+
const interfaces = filterMapRecord(webidl.interfaces?.interface, map);
61+
const mixins = filterMapRecord(webidl.mixins?.mixin, map);
62+
const namespaces = mapDefined(webidl.namespaces, (n) =>
63+
mapInterfaceLike(n.name, n, mapper)
64+
);
65+
if (
66+
!isEmptyRecord(interfaces) ||
67+
!isEmptyRecord(mixins) ||
68+
!isEmptyRecord(namespaces)
69+
) {
70+
return {
71+
interfaces: interfaces && { interface: interfaces },
72+
mixins: mixins && { mixin: mixins },
73+
namespaces,
74+
};
75+
}
76+
}

0 commit comments

Comments
 (0)