diff --git a/kayle/builder/build-rules.ts b/kayle/builder/build-rules.ts index 9b5d0fd..c37e981 100644 --- a/kayle/builder/build-rules.ts +++ b/kayle/builder/build-rules.ts @@ -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) => { @@ -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 => { + 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, ), ), diff --git a/kayle/lib/index.ts b/kayle/lib/index.ts index 7003d60..d5963d4 100644 --- a/kayle/lib/index.ts +++ b/kayle/lib/index.ts @@ -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"; diff --git a/kayle/lib/rules/index.ts b/kayle/lib/rules/index.ts index 7e73041..b67f196 100644 --- a/kayle/lib/rules/index.ts +++ b/kayle/lib/rules/index.ts @@ -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 => { + const rules = await import( + `./${locale.replace("-", "_")}/${runner === "htmlcs" ? "htmlcs" : "axe"}-rules` + ); + + return rules.axeRules || rules.htmlcsRules; +}; diff --git a/kayle/package.json b/kayle/package.json index cf853dd..c964d2e 100644 --- a/kayle/package.json +++ b/kayle/package.json @@ -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": [ diff --git a/kayle/tests/ignore-rules.ts b/kayle/tests/ignore-rules.ts index 69653e6..d33e130 100644 --- a/kayle/tests/ignore-rules.ts +++ b/kayle/tests/ignore-rules.ts @@ -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"; @@ -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,