-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(rules): add ast building htmlcs rules
- Loading branch information
Showing
14 changed files
with
2,140 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { lstatSync, readdirSync, readFileSync } from "fs"; | ||
import { join, parse as pathParse } from "path"; | ||
import { parse } from "acorn"; | ||
import { simple } from "acorn-walk"; | ||
import type { ParamList } from "./build-types"; | ||
|
||
const paramList: ParamList[] = []; | ||
|
||
const processDirectory = (directory) => { | ||
readdirSync(directory).forEach((file) => { | ||
const fullPath = join(directory, file); | ||
if (lstatSync(fullPath).isDirectory()) { | ||
processDirectory(fullPath); | ||
} else if (fullPath.endsWith(".js")) { | ||
processFile(fullPath); | ||
} | ||
}); | ||
}; | ||
|
||
const processFile = (filePath) => { | ||
const code = readFileSync(filePath, "utf8"); | ||
const ast = parse(code, { sourceType: "script", ecmaVersion: 2020 }); | ||
|
||
simple(ast, { | ||
CallExpression(node) { | ||
if ( | ||
node.callee.type === "MemberExpression" && | ||
// @ts-ignore | ||
node.callee.object.name === "HTMLCS" && | ||
// @ts-ignore | ||
node.callee.property.name === "addMessage" | ||
) { | ||
const params = node.arguments.map((arg) => { | ||
if (arg.type === "Literal") { | ||
return arg.value; | ||
} | ||
if (arg.type === "Identifier") { | ||
return arg.name; | ||
} | ||
if (arg.type === "MemberExpression") { | ||
// @ts-ignore | ||
return arg.property.name?.toLowerCase(); | ||
} | ||
|
||
return null; | ||
}); | ||
|
||
params.push(pathParse(filePath).name); | ||
|
||
paramList.push(params as ParamList); | ||
} | ||
}, | ||
}); | ||
}; | ||
|
||
// process the params to a list | ||
export const processParams = () => { | ||
processDirectory("../fast_htmlcs/dist/Standards"); | ||
return paramList; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export type Rule = { | ||
ruleId: string; | ||
description?: string; | ||
help?: string; | ||
helpUrl?: string | string[]; | ||
tags?: string[]; | ||
actIds?: string[]; | ||
ruleType?: "error" | "warning" | "notice"; | ||
}; | ||
|
||
export type ParamList = string[]; | ||
|
||
declare global { | ||
interface Window { | ||
HTMLCS: any; | ||
axe: any; | ||
pushHtmlcsRule: (r: Rule) => void; | ||
pushAxeRule: (r: Rule) => void; | ||
htmlcsRuleMap: (t: ParamList) => Rule; | ||
paramList: ParamList[]; | ||
} | ||
} | ||
|
||
window.HTMLCS = window.HTMLCS || {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import type { Rule, ParamList } from "./build-types"; | ||
|
||
// Hand stich the rules to map from htmlcs. | ||
// We need to map the ruleId to the translation file key. | ||
|
||
// map the rules to a detailed object | ||
// the descriptions handling is not perfect and needs to handle split cases. | ||
export const htmlcsRuleMap = (rule: ParamList) => { | ||
const concatWcagLink = (l: string) => | ||
`https://www.w3.org/TR/WCAG20-TECHS/${l}`; | ||
|
||
const helpLinks = rule[3] ? rule[3].split(",") : []; | ||
const section508 = !!rule[2]; | ||
|
||
// translations that do not use _ to concat | ||
const directRuleAssignments = [ | ||
"Check", | ||
"SinglePointer_Check", | ||
"AccessibleName", | ||
"Mousedown_Check", | ||
"Touchstart_Check", | ||
"Devicemotion", | ||
]; | ||
|
||
// TODO: PATCH RULES FROM INLINE TRANSLATIONS | ||
|
||
const description = | ||
rule[2] || | ||
window.HTMLCS.getTranslation(`${rule[4]}_${rule[3]}`) || | ||
window.HTMLCS.getTranslation(`${rule[4]}${rule[3]}`) || | ||
""; | ||
|
||
return { | ||
ruleId: | ||
section508 || !description | ||
? rule[3] | ||
: `${rule[4]}${directRuleAssignments.includes(rule[3]) ? "." : "_"}${rule[3]}`, | ||
description: description, | ||
helpUrl: section508 | ||
? [] | ||
: helpLinks.map((r) => concatWcagLink(r.split(".")[0])), | ||
ruleType: rule[0], | ||
}; | ||
}; |
Oops, something went wrong.