Skip to content

Commit

Permalink
fix(bcd): determine version_last at build time (#7614)
Browse files Browse the repository at this point in the history
Fixes the version range in production.
  • Loading branch information
danielhjacobs authored Nov 18, 2022
1 parent 1652955 commit 55044b5
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 37 deletions.
35 changes: 30 additions & 5 deletions build/document-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import web from "../kumascript/src/api/web";
interface SimpleSupportStatementWithReleaseDate
extends bcd.SimpleSupportStatement {
release_date?: string;
version_last?: bcd.VersionValue;
}

type SectionsAndFlaws = [Section[], string[]];
Expand Down Expand Up @@ -464,16 +465,17 @@ function _addSingleSpecialSection(
: [originalInfo];

for (const infoEntry of infos) {
const added =
typeof infoEntry.version_added === "string" &&
infoEntry.version_added.startsWith("≤")
? infoEntry.version_added.slice(1)
: infoEntry.version_added;
const added = _normalizeVersion(infoEntry.version_added);
const removed = _normalizeVersion(infoEntry.version_removed);
if (browserReleaseData.has(browser)) {
if (browserReleaseData.get(browser).has(added)) {
infoEntry.release_date = browserReleaseData
.get(browser)
.get(added).release_date;
infoEntry.version_last = _getPreviousVersion(
removed,
browsers[browser]
);
}
}
}
Expand Down Expand Up @@ -506,6 +508,29 @@ function _addSingleSpecialSection(
];
}

function _getPreviousVersion(
version: bcd.VersionValue,
browser: bcd.BrowserStatement
): bcd.VersionValue {
if (browser && typeof version === "string") {
const browserVersions = Object.keys(browser["releases"]).sort(
_compareVersions
);
const currentVersionIndex = browserVersions.indexOf(version);
if (currentVersionIndex > 0) {
return browserVersions[currentVersionIndex - 1];
}
}

return version;
}

function _normalizeVersion(version: bcd.VersionValue): bcd.VersionValue {
return typeof version === "string" && version.startsWith("≤")
? version.slice(1)
: version;
}

function _getFirstVersion(support: bcd.SimpleSupportStatement): string {
if (typeof support.version_added === "string") {
return support.version_added;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { BrowserInfoContext } from "./browser-info";
import {
asList,
getCurrentSupport,
getPreviousVersion,
hasMore,
hasNoteworthyNotes,
isFullySupportedWithoutLimitation,
Expand Down Expand Up @@ -136,10 +135,7 @@ const CellText = React.memo(
const currentSupport = getCurrentSupport(support);

const added = currentSupport?.version_added ?? null;
const removed = getPreviousVersion(
currentSupport?.version_removed ?? null,
browser
);
const lastVersion = currentSupport?.version_last ?? null;

const browserReleaseDate = getSupportBrowserReleaseDate(support);
const supportClassName = getSupportClassName(support, browser);
Expand All @@ -155,7 +151,7 @@ const CellText = React.memo(
status = { isSupported: "unknown" };
break;
case true:
status = { isSupported: removed ? "no" : "yes" };
status = { isSupported: lastVersion ? "no" : "yes" };
break;
case false:
status = { isSupported: "no" };
Expand All @@ -166,7 +162,7 @@ const CellText = React.memo(
default:
status = {
isSupported: supportClassName,
label: versionLabelFromSupport(added, removed, browser),
label: versionLabelFromSupport(added, lastVersion, browser),
};
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { compareVersions } from "compare-versions";

import type BCD from "@mdn/browser-compat-data/types";

// Extended for the fields, beyond the bcd types, that are extra-added
Expand All @@ -8,6 +6,9 @@ interface SimpleSupportStatementExtended extends BCD.SimpleSupportStatement {
// Known for some support statements where the browser *version* is known,
// as opposed to just "true" and if the version release date is known.
release_date?: string;
// The version before the version_removed if the *version* removed is known,
// as opposed to just "true". Otherwise the version_removed.
version_last?: BCD.VersionValue;
}

export type SupportStatementExtended =
Expand Down Expand Up @@ -193,20 +194,3 @@ export function getCurrentSupport(
// Default (likely never reached)
return getFirst(support);
}

export function getPreviousVersion(
version: BCD.VersionValue,
browser: BCD.BrowserStatement
): BCD.VersionValue {
if (browser && typeof version === "string") {
const browserVersions = Object.keys(browser["releases"]).sort(
compareVersions
);
const currentVersionIndex = browserVersions.indexOf(version);
if (currentVersionIndex > 0) {
return browserVersions[currentVersionIndex - 1];
}
}

return version;
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
"chalk": "^4.1.2",
"cheerio": "^1.0.0-rc.12",
"cli-progress": "^3.11.2",
"compare-versions": "^5.0.1",
"compression": "^1.7.4",
"cookie": "^0.5.0",
"cookie-parser": "^1.4.6",
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3947,11 +3947,6 @@ commondir@^1.0.1:
resolved "https://registry.yarnpkg.com/commondir/-/commondir-1.0.1.tgz#ddd800da0c66127393cca5950ea968a3aaf1253b"
integrity sha512-W9pAhw0ja1Edb5GVdIF1mjZw/ASI0AlShXM83UUGe2DVr5TdAPEA1OA8m/g8zWp9x6On7gqufY+FatDbC3MDQg==

compare-versions@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/compare-versions/-/compare-versions-5.0.1.tgz#14c6008436d994c3787aba38d4087fabe858555e"
integrity sha512-v8Au3l0b+Nwkp4G142JcgJFh1/TUhdxut7wzD1Nq1dyp5oa3tXaqb03EXOAB6jS4gMlalkjAUPZBMiAfKUixHQ==

compressible@~2.0.16:
version "2.0.18"
resolved "https://registry.yarnpkg.com/compressible/-/compressible-2.0.18.tgz#af53cca6b070d4c3c0750fbd77286a6d7cc46fba"
Expand Down

0 comments on commit 55044b5

Please sign in to comment.