Skip to content

Commit dc4974b

Browse files
committed
Prevent double and circular redirects
1 parent eaf208d commit dc4974b

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

index.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,28 +184,33 @@ for (const [key, data] of yamlEntries('features')) {
184184
}
185185

186186
// Assert that feature references are valid
187+
188+
function assertValidReference(sourceID: string, targetID: string): void {
189+
if (targetID in features) {
190+
if (isRedirectData(features[targetID])) {
191+
throw new Error(`${sourceID} references a redirect "${targetID}" instead of an ordinary feature ID`);
192+
}
193+
return;
194+
}
195+
throw new Error(`${sourceID}'s reference to "${targetID}" is not a valid feature ID`);
196+
}
197+
187198
for (const [id, feature] of Object.entries(features)) {
188199
if (isOrdinaryFeatureData(feature)) {
189200
for (const alternative of feature.discouraged?.alternatives ?? []) {
190-
if (!(alternative in features)) {
191-
throw new Error(`${id}'s alternative "${alternative}" is not a valid feature ID`);
192-
}
201+
assertValidReference(id, alternative);
193202
}
194203
}
195204

196205
if (isRedirectData(feature)) {
197206
const { reason } = feature.redirect;
198207
switch (reason) {
199208
case 'moved':
200-
if (!(feature.redirect.target in features)) {
201-
throw new Error(`${id}'s redirect target "${feature.redirect.target} is not a valid feature ID`);
202-
}
209+
assertValidReference(id, feature.redirect.target);
203210
break;
204211
case 'split':
205212
for (const target of feature.redirect.targets) {
206-
if (!(target in features)) {
207-
throw new Error(`${id}'s redirect target "${target}" is not a valid feature ID`);
208-
}
213+
assertValidReference(id, target);
209214
}
210215
break;
211216
default:

0 commit comments

Comments
 (0)