Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance(bcd): Remove use of @mdn/bcd-utils-api in testing #10186

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions .env.testing
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ BUILD_ALWAYS_ALLOW_ROBOTS=true
REACT_APP_ENABLE_PLUS=true

REACT_APP_FXA_SIGNIN_URL=/users/fxa/login/authenticate/

REACT_APP_BCD_BASE_URL = ""
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ import {
isNotSupportedAtAll,
isTruthy,
versionIsPreview,
SupportStatementExtended,
bugURLToString,
} from "./utils";
import { LEGEND_LABELS } from "./legend";
import { DEFAULT_LOCALE } from "../../../../../libs/constants";

function getSupportClassName(
support: SupportStatementExtended | undefined,
support: BCD.SupportStatement | undefined,
browser: BCD.BrowserStatement
): "no" | "yes" | "partial" | "preview" | "removed-partial" | "unknown" {
if (!support) {
Expand Down Expand Up @@ -48,12 +47,17 @@ function getSupportClassName(
}

function getSupportBrowserReleaseDate(
support: SupportStatementExtended | undefined
currentSupport: BCD.SimpleSupportStatement | undefined,
browser: BCD.BrowserStatement
): string | undefined {
if (!support) {
if (!currentSupport) {
return undefined;
}
return getCurrentSupport(support)!.release_date;
const version = currentSupport.version_added;
if (typeof version !== "string" || version === "preview") {
return undefined;
}
return browser.releases[version].release_date;
}

function StatusIcons({ status }: { status: BCD.StatusBlock }) {
Expand Down Expand Up @@ -138,7 +142,10 @@ const CellText = React.memo(
const added = currentSupport?.version_added ?? null;
const lastVersion = currentSupport?.version_last ?? null;

const browserReleaseDate = getSupportBrowserReleaseDate(support);
const browserReleaseDate = getSupportBrowserReleaseDate(
currentSupport,
browser
);
const supportClassName = getSupportClassName(support, browser);

let status:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
import type BCD from "@mdn/browser-compat-data/types";

// Extended for the fields, beyond the bcd types, that are extra-added
// exclusively in Yari.
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 =
| SimpleSupportStatementExtended
| SimpleSupportStatementExtended[];

export function getFirst<T>(a: T | T[]): T;
export function getFirst<T>(a: T | T[] | undefined): T | undefined {
return Array.isArray(a) ? a[0] : a;
Expand Down Expand Up @@ -160,8 +145,8 @@ function isFullySupportedWithoutMajorLimitation(

// Prioritizes support items
export function getCurrentSupport(
support: SupportStatementExtended | undefined
): SimpleSupportStatementExtended | undefined {
support: BCD.SupportStatement | undefined
): BCD.SimpleSupportStatement | undefined {
if (!support) return undefined;

// Full support without limitation
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
"@codemirror/state": "^6.4.1",
"@codemirror/theme-one-dark": "^6.1.2",
"@fast-csv/parse": "^5.0.0",
"@mdn/bcd-utils-api": "^0.0.6",
"@mdn/browser-compat-data": "^5.5.16",
"@mozilla/glean": "4.0.0",
"@sentry/integrations": "^7.107.0",
Expand Down
34 changes: 21 additions & 13 deletions scripts/ai-help-macros.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ import { load as cheerio } from "cheerio";

import { DocMetadata } from "../libs/types/document.js";
import { BUILD_OUT_ROOT, OPENAI_KEY, PG_URI } from "../libs/env/index.js";
import {
getBCDDataForPath,
SimpleSupportStatementExtended,
} from "@mdn/bcd-utils-api";
import path from "node:path";
import bcd from "@mdn/browser-compat-data" assert { type: "json" };
import {
BrowserStatement,
SimpleSupportStatement,
Expand Down Expand Up @@ -351,19 +348,27 @@ async function* builtDocs(directory: string) {
}

function buildBCDTable(query: string) {
const bcdData = getBCDDataForPath(query);
if (!bcdData) return "";
const { browsers, data } = bcdData;
const bcdPathParts = query.split(".");

let data: any = bcd;
for (const part of bcdPathParts) {
if (!(part in data)) {
return "";
}
data = data[part];
}
if (!data) return "";

return data.__compat?.support
? `<table class="bc-table">
<thead><tr><th>Browser</th><th>Support</th>
<tbody>
${Object.entries(data.__compat?.support)
${Object.entries(data.__compat.support)
.map(
([browser, support]) =>
`<tr><td>${browsers[browser].name}</td><td>${buildBCDSupportString(
browsers[browser],
support
`<tr><td>${bcd.browsers[browser].name}</td><td>${buildBCDSupportString(
bcd.browsers[browser],
support as SimpleSupportStatement[]
)}</td></tr>`
)
.join("\n")}
Expand All @@ -374,10 +379,13 @@ ${Object.entries(data.__compat?.support)

function buildBCDSupportString(
browser: BrowserStatement,
support: (SimpleSupportStatement & SimpleSupportStatementExtended)[]
support: SimpleSupportStatement[]
) {
return support
.flatMap((item) => {
const releaseDate =
typeof item.version_added === "string" &&
browser.releases[item.version_added]?.release_date;
return [
item.version_removed &&
!support.some(
Expand Down Expand Up @@ -405,7 +413,7 @@ function buildBCDSupportString(
isFullySupportedWithoutLimitation(item) &&
!versionIsPreview(item.version_added, browser)
? `Full support since version ${item.version_added}${
item.release_date ? ` (released ${item.release_date})` : ""
releaseDate ? ` (released ${releaseDate})` : ""
}`
: isNotSupportedAtAll(item)
? "No support"
Expand Down
21 changes: 18 additions & 3 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import send from "send";
import { createProxyMiddleware } from "http-proxy-middleware";
import cookieParser from "cookie-parser";
import openEditor from "open-editor";
import { getBCDDataForPath } from "@mdn/bcd-utils-api";
import sanitizeFilename from "sanitize-filename";

import bcd from "@mdn/browser-compat-data" assert { type: "json" };

import {
buildDocument,
buildLiveSamplePageFromURL,
Expand Down Expand Up @@ -79,8 +80,22 @@ const bcdRouter = express.Router({ caseSensitive: true });

// Note that this route will only get hit if .env has this: REACT_APP_BCD_BASE_URL=""
bcdRouter.get("/api/v0/current/:path.json", async (req, res) => {
const data = getBCDDataForPath(req.params.path);
return data ? res.json(data) : res.status(404).send("BCD path not found");
const bcdPath = req.params.path;
const bcdPathParts = bcdPath.split(".");

let data = bcd;

for (const part of bcdPathParts) {
if (part in data) {
data = data[part];
} else {
res.status(404).send("BCD path not found");
}
}

return data
? res.json({ browsers: bcd.browsers, data, query: bcdPath })
: res.status(404).send("BCD path not found");
});

bcdRouter.use(
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2276,11 +2276,6 @@
dependencies:
call-bind "^1.0.7"

"@mdn/bcd-utils-api@^0.0.6":
version "0.0.6"
resolved "https://registry.yarnpkg.com/@mdn/bcd-utils-api/-/bcd-utils-api-0.0.6.tgz#166fd6c431702af3c84133013f337b028476e7bc"
integrity sha512-aT0tumSuqi8RbD2ownK9f84pygC/m9/NlXh6IAWcSxj4YtFv14ohH95KcVLiLnTFx1reokVagYvC7P9611EU2A==

"@mdn/browser-compat-data@^5.5.16":
version "5.5.16"
resolved "https://registry.yarnpkg.com/@mdn/browser-compat-data/-/browser-compat-data-5.5.16.tgz#3fe64c7497c56191094f57e159ff50097452be4f"
Expand Down
Loading