Skip to content

Commit 0eff225

Browse files
authored
Simplify and document the dist generation flow (#1233)
Parts of this were more complicated than they need to be because initially the dist file was a modified version of the source with comments preserved. Now that it is just computed data, simplifications are possible. Also, only call computeBaseline once with the compat keys that will actually be used. This does mean that fixing one warning could reveal another instead of showing both at the same time, but that's not so awful.
1 parent fe05aae commit 0eff225

File tree

5 files changed

+54
-108
lines changed

5 files changed

+54
-108
lines changed

features/anchor-positioning.yml.dist

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ compat_features:
9191
- css.properties.bottom.anchor
9292
- css.properties.height.anchor-size
9393
- css.properties.inline-size.anchor-size
94-
- css.properties.inset.anchor
9594
- css.properties.inset-area
9695
- css.properties.inset-area.block-end
9796
- css.properties.inset-area.block-start
@@ -128,12 +127,13 @@ compat_features:
128127
- css.properties.inset-area.y-self-end
129128
- css.properties.inset-area.y-self-start
130129
- css.properties.inset-area.y-start
131-
- css.properties.inset-block.anchor
132130
- css.properties.inset-block-end.anchor
133131
- css.properties.inset-block-start.anchor
134-
- css.properties.inset-inline.anchor
132+
- css.properties.inset-block.anchor
135133
- css.properties.inset-inline-end.anchor
136134
- css.properties.inset-inline-start.anchor
135+
- css.properties.inset-inline.anchor
136+
- css.properties.inset.anchor
137137
- css.properties.justify-items.anchor-center
138138
- css.properties.justify-self.anchor-center
139139
- css.properties.left.anchor

features/border-image.yml.dist

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ status:
1515
safari_ios: "9.3"
1616
compat_features:
1717
- css.properties.border-image
18-
- css.properties.border-image.fill
19-
- css.properties.border-image.gradient
20-
- css.properties.border-image.optional_border_image_slice
2118
- css.properties.border-image-outset
2219
- css.properties.border-image-repeat
2320
- css.properties.border-image-repeat.round
2421
- css.properties.border-image-repeat.space
2522
- css.properties.border-image-slice
2623
- css.properties.border-image-source
2724
- css.properties.border-image-width
25+
- css.properties.border-image.fill
26+
- css.properties.border-image.gradient
27+
- css.properties.border-image.optional_border_image_slice

features/overflow-shorthand.yml.dist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ status:
1414
safari_ios: "16"
1515
compat_features:
1616
- css.properties.overflow
17-
- css.properties.overflow.clip
18-
- css.properties.overflow.multiple_keywords
1917
- css.properties.overflow-x
2018
- css.properties.overflow-x.clip
2119
- css.properties.overflow-y
2220
- css.properties.overflow-y.clip
21+
- css.properties.overflow.clip
22+
- css.properties.overflow.multiple_keywords
2323
- css.types.overflow
2424
- css.types.overflow.clip

features/transforms3d.yml.dist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ status:
1515
compat_features:
1616
- css.properties.backface-visibility
1717
- css.properties.perspective
18-
- css.properties.perspective.none
1918
- css.properties.perspective-origin
2019
- css.properties.perspective-origin.bottom
2120
- css.properties.perspective-origin.center
2221
- css.properties.perspective-origin.left
2322
- css.properties.perspective-origin.right
2423
- css.properties.perspective-origin.top
25-
- css.properties.transform.3d
24+
- css.properties.perspective.none
2625
- css.properties.transform-style
26+
- css.properties.transform.3d
2727
- css.types.transform-function.matrix3d
2828
- css.types.transform-function.perspective
2929
- css.types.transform-function.rotate3d

scripts/dist.ts

Lines changed: 44 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -74,83 +74,66 @@ function checkDistFile(sourcePath: string, distPath: string): boolean {
7474
* successful, generates a `status` block.
7575
*/
7676
function toDist(sourcePath: string): YAML.Document {
77-
const yaml = YAML.parseDocument(
78-
fs.readFileSync(sourcePath, { encoding: "utf-8" }),
79-
);
77+
const source = YAML.parse(fs.readFileSync(sourcePath, { encoding: "utf-8" }));
8078
const { name: id } = path.parse(sourcePath);
8179

82-
const taggedCompatFeatures = (
83-
tagsToFeatures.get(`web-features:${id}`) ?? []
84-
).map((f) => `${f.id}`);
85-
86-
const overridden = {
87-
compatFeatures: yaml.toJS().compat_features,
88-
status: yaml.toJS().status,
89-
};
90-
91-
const generated = {
92-
compatFeatures: taggedCompatFeatures.length
93-
? taggedCompatFeatures
94-
: undefined,
95-
status: taggedCompatFeatures.length
96-
? computeBaseline({
97-
compatKeys: taggedCompatFeatures as [string, ...string[]],
98-
checkAncestors: false,
99-
})
100-
: undefined,
101-
statusByCompatFeaturesOverride: Array.isArray(overridden.compatFeatures)
102-
? computeBaseline({
103-
compatKeys: overridden.compatFeatures as [string, ...string[]],
104-
checkAncestors: false,
105-
})
106-
: undefined,
107-
};
80+
// Collect tagged compat features. A `compat_features` list in the source
81+
// takes precedence, but can be removed if it matches the tagged features.
82+
const taggedCompatFeatures = (tagsToFeatures.get(`web-features:${id}`) ?? [])
83+
.map((f) => `${f.id}`)
84+
.sort();
10885

109-
warnOnNeedlessOverrides(id, overridden, generated);
86+
if (source.compat_features) {
87+
source.compat_features.sort();
88+
if (isDeepStrictEqual(source.compat_features, taggedCompatFeatures)) {
89+
logger.warn(
90+
`${id}: compat_features override matches tags in @mdn/browser-compat-data. Consider deleting this override.`,
91+
);
92+
}
93+
}
11094

111-
const dist = new Document({});
112-
insertHeaderComments(dist, id);
95+
// Compute the status. A `status` block in the source takes precedence, but
96+
// can be removed if it matches the computed status.
97+
let computedStatus = computeBaseline({
98+
compatKeys: source.compat_features ?? taggedCompatFeatures,
99+
checkAncestors: false,
100+
});
113101

114-
if (!overridden.compatFeatures && generated.compatFeatures) {
115-
insertCompatFeatures(dist, generated.compatFeatures);
102+
if (computedStatus.discouraged) {
103+
logger.warn(
104+
`${id}: contains at least one deprecated compat feature and can never be Baseline. Was this intentional?`,
105+
);
116106
}
117107

118-
if (!overridden.status) {
119-
const status = generated.statusByCompatFeaturesOverride ?? generated.status;
120-
if (status) {
121-
if (status.discouraged) {
122-
logger.warn(
123-
`${id}: contains at least one deprecated compat feature and can never be Baseline. Was this intentional?`,
124-
);
125-
}
126-
insertStatus(dist, JSON.parse(status.toJSON()));
108+
computedStatus = JSON.parse(computedStatus.toJSON());
109+
110+
if (source.status) {
111+
if (isDeepStrictEqual(source.status, computedStatus)) {
112+
logger.warn(
113+
`${id}: status override matches computed status. Consider deleting this override.`,
114+
);
127115
}
128116
}
129117

130-
return dist;
131-
}
132-
133-
function insertCompatFeatures(yaml: Document, compatFeatures: string[]) {
134-
yaml.set("compat_features", compatFeatures);
135-
}
136-
137-
function insertStatus(yaml: Document, status) {
138-
// Create the status node and insert it just before "compat_features"
139-
const statusNode = yaml.createPair("status", status);
140-
assert(YAML.isMap(yaml.contents));
141-
const targetIndex = yaml.contents.items.findIndex(
142-
(item) => item.key.toString() === "compat_features",
143-
);
144-
yaml.contents.items.splice(targetIndex, 0, statusNode as YAML.Pair<any, any>);
145-
}
118+
// Assemble and return the dist YAML.
119+
const dist = new Document({});
146120

147-
function insertHeaderComments(yaml: Document, id: string) {
148-
yaml.commentBefore = [
121+
dist.commentBefore = [
149122
`Generated from: ${id}.yml`,
150123
`Do not edit this file by hand. Edit the source file instead!`,
151124
]
152125
.map((line) => ` ${line}`)
153126
.join("\n");
127+
128+
if (!source.status) {
129+
dist.set("status", computedStatus);
130+
}
131+
132+
if (!source.compat_features) {
133+
dist.set("compat_features", taggedCompatFeatures);
134+
}
135+
136+
return dist;
154137
}
155138

156139
const compat = new Compat();
@@ -171,43 +154,6 @@ const tagsToFeatures: Map<string, Feature[]> = (() => {
171154
return map;
172155
})();
173156

174-
function warnOnNeedlessOverrides(id, overridden, generated) {
175-
if (overridden.compatFeatures?.length && generated.compatFeatures?.length) {
176-
if (
177-
isDeepStrictEqual(
178-
[...overridden.compatFeatures].sort(),
179-
[...generated.compatFeatures].sort(),
180-
)
181-
) {
182-
logger.warn(
183-
`${id}: compat_features override matches tags in @mdn/browser-compat-data. Consider deleting this override.`,
184-
);
185-
}
186-
}
187-
188-
if (
189-
overridden.status &&
190-
generated.statusByCompatFeaturesOverride &&
191-
isDeepStrictEqual(
192-
overridden.status,
193-
generated.statusByCompatFeaturesOverride,
194-
)
195-
) {
196-
logger.warn(
197-
`${id}: status override matches generated status from compat_features override. Consider deleting this override.`,
198-
);
199-
}
200-
if (
201-
overridden.status &&
202-
generated.status &&
203-
isDeepStrictEqual(overridden.status, generated.status)
204-
) {
205-
logger.warn(
206-
`${id}: status override matches generated status from tags. Consider deleting this override.`,
207-
);
208-
}
209-
}
210-
211157
function main() {
212158
const filePaths = argv.paths.flatMap((fileOrDirectory) => {
213159
if (fs.statSync(fileOrDirectory).isDirectory()) {

0 commit comments

Comments
 (0)