From 5df19a40f7395561ce5a71e6e9c167ca2063f871 Mon Sep 17 00:00:00 2001 From: Arnau Casau Date: Tue, 19 Nov 2024 19:22:27 +0100 Subject: [PATCH] deduplicate errors --- scripts/js/commands/checkAltText.ts | 15 +++++++++------ scripts/js/lib/extractMarkdownImages.test.ts | 2 +- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/scripts/js/commands/checkAltText.ts b/scripts/js/commands/checkAltText.ts index 55fed63bf74..e6ef108f9b5 100644 --- a/scripts/js/commands/checkAltText.ts +++ b/scripts/js/commands/checkAltText.ts @@ -16,24 +16,27 @@ import { readMarkdown } from "../lib/markdownReader.js"; async function main() { const files = await globby(["docs/**/*.{ipynb,mdx}", "!docs/api/**/*.mdx"]); - const errors: string[] = []; + const fileErrors: string[] = []; for (const file of files) { const markdown = await readMarkdown(file); const images: Image[] = await extractMarkdownImages(markdown); - const fileErrors = images.flatMap((image: Image) => + const imageErrors = images.flatMap((image: Image) => image.altText ? [] : `The image '${image.imageName}' does not have an alt text defined.`, ); + const imageErrorsDeduplicated = new Set(imageErrors); - if (fileErrors.length > 0) { - errors.push(`Error in file '${file}':\n\t- ${fileErrors.join("\n\t- ")}`); + if (imageErrorsDeduplicated.size > 0) { + fileErrors.push( + `Error in file '${file}':\n\t- ${[...imageErrorsDeduplicated].join("\n\t- ")}`, + ); } } - if (errors.length > 0) { - errors.forEach((error) => console.log(error)); + if (fileErrors.length > 0) { + fileErrors.forEach((error) => console.log(error)); console.error("\nSome images are missing alt text 💔\n"); process.exit(1); } diff --git a/scripts/js/lib/extractMarkdownImages.test.ts b/scripts/js/lib/extractMarkdownImages.test.ts index eb263091861..b4a57eaa9d4 100644 --- a/scripts/js/lib/extractMarkdownImages.test.ts +++ b/scripts/js/lib/extractMarkdownImages.test.ts @@ -12,7 +12,7 @@ import { expect, test } from "@playwright/test"; -import { Image, extractMarkdownImages } from "./extractMarkdownImages.js"; +import { extractMarkdownImages } from "./extractMarkdownImages.js"; test("Test the extraction of the images", async () => { const markdown = `