Skip to content

Commit

Permalink
chore(rules): fix htmlcs rule ignore handling
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Mar 10, 2024
1 parent bdc13ba commit ff037d4
Show file tree
Hide file tree
Showing 49 changed files with 4,674 additions and 3,345 deletions.
2 changes: 1 addition & 1 deletion kayle/bench/fast_axecore-playwright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async function launchBench() {
unit: "OPS/S",
value: 1000 / avg,
},
])
]),
);
}

Expand Down
2 changes: 1 addition & 1 deletion kayle/bench/fast_htmlcs-playwright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async function launchBench() {
unit: "OPS/S",
value: 1000 / avg,
},
])
]),
);
}

Expand Down
2 changes: 1 addition & 1 deletion kayle/builder/build-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ window.addEventListener("kayle_send", async (event) => {

writeFileSync(
`${ext}/content-script.js`,
`${extensionRunner}\n${extensionAxe}\n${extensionHtmlcs}\n${extensionRawEnd}`
`${extensionRunner}\n${extensionAxe}\n${extensionHtmlcs}\n${extensionRawEnd}`,
);

const extensionManifest = `{
Expand Down
37 changes: 23 additions & 14 deletions kayle/builder/build-htmlcs-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ const isHTMLCSAddMessageCall = (node) => {
return false;
};

// get the add Message call directly
const extractPureAddMessageArgs = (s: string) => {
return s.replace(/\.replace\(.*?\)/, "").replace(/^_global\./, "");
};

const processFile = (filePath) => {
const code = readFileSync(filePath, "utf8");
const ast = parse(code, { sourceType: "script", ecmaVersion: 2020 });
Expand All @@ -52,36 +57,40 @@ const processFile = (filePath) => {
return arg.name;
}

if (arg.type === "MemberExpression") {
// @ts-ignore
return arg.property.name?.toLowerCase();
if (
arg.type === "MemberExpression" &&
arg.property &&
arg.property.type === "Identifier"
) {
return arg.property.name.toLowerCase();
}

if (arg.type === "CallExpression" && arg.callee) {
return extractPureAddMessageArgs(generate(arg));
}

if (
index === 2 &&
arg.type === "CallExpression" &&
// @ts-ignore
arg.callee?.object?.object?.name === "_global" &&
// @ts-ignore
arg.callee?.object?.property?.name === "HTMLCS" &&
// @ts-ignore
arg.callee?.property?.name === "getTranslation"
arg.callee &&
arg.arguments.length
) {
if (arg.arguments.length) {
return generate(arg).replace("_global.", "");
}
return extractPureAddMessageArgs(generate(arg));
}

return null;
});

params.push(pathParse(filePath).name);

// run regex on first match
if (!guide) {
guide = code.match(
/(_global\.HTMLCS_WCAG2AAA_Sniffs_)([A-Za-z0-9_]+)/
/(_global\.HTMLCS_WCAG2AAA_Sniffs_)([A-Za-z0-9_]+)/,
);
if (guide && guide[2]) {
const end = guide[2].length - 23;
guide[2] = guide[2].slice(0, guide[2].length - end);
}
}

if (guide && guide[2]) {
Expand Down
32 changes: 16 additions & 16 deletions kayle/builder/build-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { processParams } from "./build-htmlcs-params";
import type { Rule } from "./build-types";

(async () => {
const browser = await chromium.launch({ headless: true });
const paramList = await processParams();
const browser = await chromium.launch({ headless: true });

const pConfig = {
singleQuote: true,
Expand All @@ -35,7 +35,7 @@ import type { Rule } from "./build-types";
origin: "https://www.example.com",
language,
},
true
true,
);

await page.evaluate((o) => {
Expand All @@ -47,11 +47,11 @@ import type { Rule } from "./build-types";
});

await page.exposeFunction("pushHtmlcsRule", (t: Rule) =>
fast_htmlcs_rules.push(t)
fast_htmlcs_rules.push(t),
);

await page.exposeFunction("pushAxeRule", (t: Rule) =>
fast_axe_rules.push(t)
fast_axe_rules.push(t),
);

await page.evaluate(() => {
Expand All @@ -76,32 +76,32 @@ import type { Rule } from "./build-types";
Buffer.from(
await format(
`/* ${DNE} */\nexport const htmlcsRules = ${JSON.stringify(
fast_htmlcs_rules.filter((r) => r.description)
fast_htmlcs_rules.filter((r) => r.description),
)};`,
pConfig
)
pConfig,
),
),
"utf8"
"utf8",
);

await writeFile(
`./lib/rules/${language}/axe-rules.ts`,
Buffer.from(
await format(
`/* ${DNE} */\nexport const axeRules = ${JSON.stringify(
fast_axe_rules
fast_axe_rules,
)};`,
pConfig
)
pConfig,
),
),
"utf8"
"utf8",
);

await page.close();
};

const localesList: string[] = Array.from(
new Set(htmlcsLocales.concat(axeLocales))
new Set(htmlcsLocales.concat(axeLocales)),
);

localesList.push("en");
Expand All @@ -118,10 +118,10 @@ import type { Rule } from "./build-types";
export { htmlcsRules as htmlcsRules${l.toUpperCase()} } from "./${l}/htmlcs-rules";`;
})
.join(""),
pConfig
)
pConfig,
),
),
"utf8"
"utf8",
);

await browser.close();
Expand Down
22 changes: 11 additions & 11 deletions kayle/lib/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ export const actions = [
target.dispatchEvent(
new Event("input", {
bubbles: true,
})
}),
);

return Promise.resolve();
},
selector,
value
value,
);
} catch (error) {
throw new Error(`${failedActionElement} "${selector}"`);
Expand Down Expand Up @@ -91,7 +91,7 @@ export const actions = [
target.dispatchEvent(
new Event("input", {
bubbles: true,
})
}),
);
return Promise.resolve();
}, selector);
Expand All @@ -117,12 +117,12 @@ export const actions = [
target.dispatchEvent(
new Event("change", {
bubbles: true,
})
}),
);
return Promise.resolve();
},
selector,
checked
checked,
);
} catch (error) {
throw new Error(`${failedActionElement} "${selector}"`);
Expand Down Expand Up @@ -176,7 +176,7 @@ export const actions = [
{},
property,
expectedValue,
negated
negated,
);
},
},
Expand Down Expand Up @@ -216,7 +216,7 @@ export const actions = [
polling: 200,
},
selector,
state
state,
);
},
},
Expand Down Expand Up @@ -244,11 +244,11 @@ export const actions = [
},
{
once: true,
}
},
);
},
selector,
eventType
eventType,
);

await page.waitForFunction(
Expand All @@ -263,7 +263,7 @@ export const actions = [
},
{
polling: 200,
}
},
);
} catch (error) {
throw new Error(`${failedActionElement} "${selector}"`);
Expand All @@ -283,7 +283,7 @@ export const actions = [
*/
export async function runAction(browser, page, options, act, customActions?) {
const action = (customActions ?? actions).find((item) =>
item.match.test(act)
item.match.test(act),
);

if (!action) {
Expand Down
8 changes: 4 additions & 4 deletions kayle/lib/auto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function autoKayle(
cb?: ((result: Audit) => Promise<void>) | ((result: Audit) => void);
} = {},
ignoreSet?: Set<String>,
_results?: Audit[]
_results?: Audit[],
): Promise<Audit[]> {
if (!write) {
const { writeFile } = await import("fs/promises");
Expand Down Expand Up @@ -51,7 +51,7 @@ export async function autoKayle(
if (o.store) {
await write(
`${o.store}/${encodeURIComponent(o.page.url())}`,
await o.page.content()
await o.page.content(),
);
}

Expand Down Expand Up @@ -84,7 +84,7 @@ export async function autoKayle(
origin: link,
},
ignoreSet,
_results
_results,
);
})
.catch((e) => {
Expand All @@ -94,7 +94,7 @@ export async function autoKayle(
console.error(e);
}
});
})
}),
);

return _results;
Expand Down
10 changes: 5 additions & 5 deletions kayle/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type BrowserContext = {
newCDPSession?(page: Partial<Page> | Frame): Partial<CDPSession>;
overridePermissions?(
origin: string,
permissions: Permission[]
permissions: Permission[],
): Promise<void>;
};

Expand Down Expand Up @@ -89,7 +89,7 @@ type Page = {
_routes?: { url: string }[];
route(
path: string,
intercept: (config: any, next: any) => Promise<void> | Promise<boolean>
intercept: (config: any, next: any) => Promise<void> | Promise<boolean>,
): Promise<void>;
setRequestInterception?(enable?: boolean): Promise<void>;
listenerCount?(name: string): number;
Expand All @@ -99,19 +99,19 @@ type Page = {
| Function
| {
default: Function;
}
},
): Promise<void>;
addInitScript?(script: { content?: string }): Promise<void>;
evaluateOnNewDocument?<
Params extends unknown[],
Func extends (...args: Params) => unknown = (...args: Params) => unknown
Func extends (...args: Params) => unknown = (...args: Params) => unknown,
>(
pageFunction: Func | string,
...args: Params
): Promise<{ identifier: string }>;
evaluate<
Params extends unknown[],
Func extends EvaluateFunc<Params> = EvaluateFunc<Params>
Func extends EvaluateFunc<Params> = EvaluateFunc<Params>,
>(
pageFunction: Func | string,
...args: Params
Expand Down
Loading

0 comments on commit ff037d4

Please sign in to comment.