Skip to content

Commit

Permalink
chore(rules): add locale import method
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Mar 10, 2024
1 parent 3249b3b commit e6783b3
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 7 deletions.
34 changes: 30 additions & 4 deletions kayle/builder/build-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type { Rule } from "./build-types";
const pConfig = {
singleQuote: false,
semi: true,
parser: "babel",
parser: "typescript",
};

const runBuildRules = async (language: string) => {
Expand Down Expand Up @@ -117,16 +117,42 @@ import type { Rule } from "./build-types";

await Promise.all(localesList.map(runBuildRules));

const runnerImports =
`
export type Rule = {
ruleId: string;
description?: string;
help?: string;
helpUrl?: string | string[];
tags?: string[];
actIds?: string[];
ruleType?: "error" | "warning" | "notice";
};
// import rule list with locales
export const importRules = async (locale: ${localesList.map((l) => `"${l}"`).join(" | ")}, runner: "htmlcs" | "axe"): Promise<Rule[]> => {
const rules = await import(` +
"`./${locale.replace('-', '_')}/${runner === 'htmlcs' ? 'htmlcs' : 'axe'}-rules`" +
`` +
`);
return rules.axeRules || rules.htmlcsRules
}`;

await writeFile(
`./lib/rules/index.ts`,
Buffer.from(
await format(
localesList
`${localesList
.map((l) => {
return `export { axeRules as axeRules${l.toUpperCase()} } from "./${l}/axe-rules";
export { htmlcsRules as htmlcsRules${l.toUpperCase()} } from "./${l}/htmlcs-rules";`;
export { htmlcsRules as htmlcsRules${l.toUpperCase()} } from "./${l}/htmlcs-rules";
`;
})
.join(""),
.join("")}
${runnerImports}
`,
pConfig,
),
),
Expand Down
2 changes: 1 addition & 1 deletion kayle/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ export {
export { extractLinks, innateBuilder } from "./wasm";
export { htmlcsLocales } from "./runners/htmlcs";
export { axeLocales } from "./runners/axe";
// export base rules to use upfront
export { axeRules } from "./rules/en/axe-rules";
export { htmlcsRules } from "./rules/en/htmlcs-rules";
export { importRules } from "./rules";
39 changes: 39 additions & 0 deletions kayle/lib/rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,42 @@ export { axeRules as axeRulesPT_BR } from "./pt_BR/axe-rules";
export { htmlcsRules as htmlcsRulesPT_BR } from "./pt_BR/htmlcs-rules";
export { axeRules as axeRulesEN } from "./en/axe-rules";
export { htmlcsRules as htmlcsRulesEN } from "./en/htmlcs-rules";

export type Rule = {
ruleId: string;
description?: string;
help?: string;
helpUrl?: string | string[];
tags?: string[];
actIds?: string[];
ruleType?: "error" | "warning" | "notice";
};

// import rule list with locales
export const importRules = async (
locale:
| "ar"
| "fr"
| "es"
| "it"
| "ja"
| "nl"
| "pl"
| "ko"
| "zh_CN"
| "zh_TW"
| "da"
| "de"
| "eu"
| "he"
| "no_NB"
| "pt_BR"
| "en",
runner: "htmlcs" | "axe",
): Promise<Rule[]> => {
const rules = await import(
`./${locale.replace("-", "_")}/${runner === "htmlcs" ? "htmlcs" : "axe"}-rules`
);

return rules.axeRules || rules.htmlcsRules;
};
2 changes: 1 addition & 1 deletion kayle/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kayle",
"version": "0.8.36",
"version": "0.8.37",
"description": "Extremely fast and accurate accessibility engine built for any headless tool like playwright or puppeteer.",
"main": "./build/index.js",
"keywords": [
Expand Down
4 changes: 3 additions & 1 deletion kayle/tests/ignore-rules.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from "assert";
import puppeteer from "puppeteer";
import { Standard, kayle, htmlcsRules } from "kayle";
import { Standard, kayle, importRules } from "kayle";

import { drakeMock } from "./mocks/html-mock";
import { performance } from "perf_hooks";
Expand All @@ -11,6 +11,8 @@ import { performance } from "perf_hooks";

page.on("console", (msg) => console.log("PAGE LOG:", msg.text()));

const htmlcsRules = await importRules("en", "htmlcs");

const startTime = performance.now();
const { issues, pageUrl, documentTitle, meta, automateable } = await kayle({
page,
Expand Down

0 comments on commit e6783b3

Please sign in to comment.