Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: modernized rule generator #4106

Merged
merged 2 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/rule-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async function run() {
const result = await Promise.all(
files.map(async meta => {
const path = `${meta.dir}/${meta.name}`;
const content = meta.content;
const content = meta.content + '\n';
await createFile(path, content);
return path;
})
Expand Down
72 changes: 44 additions & 28 deletions build/rule-generator/get-files-metadata.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const directories = require('./directories');
const outdent = require('outdent');

/**
* Helper to convert a given string to camel case (split by hyphens if any)
Expand All @@ -23,8 +24,8 @@ const getRuleSpecFileMeta = (ruleName, ruleHasMatches, ruleChecks) => {
name: `${ruleName}.json`,
content: JSON.stringify(
{
id: `${ruleName}`,
selector: '',
id: ruleName,
selector: '*',
...(ruleHasMatches && {
matches: `${ruleName}-matches`
}),
Expand Down Expand Up @@ -70,15 +71,13 @@ const getRuleMatchesFileMeta = (
const fnName = `${camelCase(ruleName)}Matches`;
const ruleMatchesJs = {
name: `${ruleName}-matches.js`,
content: `
// TODO: Filter node(s)

function ${fnName}(node, virtualNode) {
return node
}
content: outdent`
// TODO: Filter node(s)

export default ${fnName}
`,
export default function ${fnName}(node, virtualNode) {
return true;
}
`,
dir: directories.rules
};
files.push(ruleMatchesJs);
Expand All @@ -87,11 +86,17 @@ const getRuleMatchesFileMeta = (
if (ruleHasUnitTestAssets) {
const ruleMatchesTestJs = {
name: `${ruleName}-matches.js`,
content: `
describe('${ruleName}-matches', function() {
'use strict';
// TODO: Write tests
})
content: outdent`
describe('${ruleName}-matches', () => {
const rule = axe.utils.getRule('${ruleName}');
const { queryFixture } = axe.testUtils;

// TODO: Replace with real tests
it('returns false for paragraphs', () => {
const vNode = queryFixture('<p id="target">Hello world</p>');
assert.isFalse(rule.matches(vNode.actualNode, vNode));
});
});
`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`,
`,

dir: directories.testRuleMatches
};
Expand Down Expand Up @@ -142,13 +147,12 @@ const getCheckJsFileMeta = (name, dir) => {
const fnName = `${camelCase(name)}Evaluate`;
return {
name: `${name}-evaluate.js`,
content: `
// TODO: Logic for check
function ${fnName}(node, options, virtualNode) {
return true
}
export default ${fnName};
`,
content: outdent`
// TODO: Logic for check
export default function ${fnName}(node, options, virtualNode) {
return true
}
`,
dir
};
};
Expand All @@ -163,12 +167,24 @@ const getCheckJsFileMeta = (name, dir) => {
const getCheckTestJsFileMeta = (name, dir) => {
return {
name: `${name}.js`,
content: `
describe('${name} tests', function() {
'use strict';
// TODO: Write tests
})
`,
content: outdent`
describe('${name} tests', () => {
const { checkSetup, getCheckEvaluate } = axe.testUtils;
const checkContext = axe.testUtils.MockCheckContext();
const checkEvaluate = getCheckEvaluate('${name}');

afterEach(() => {
checkContext.reset();
});

// TODO: Replace this with real tests for this check
it('returns false when img has no alt', () => {
const args = checkSetup('<img id="target" />');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const args = checkSetup('<img id="target" />');
const params = checkSetup('<img id="target" />');

assert.isFalse(checkEvaluate.apply(checkContext, params));
assert.deepEqual(checkContext._data, { messageKey: 'missing-alt' });
});
});
`,
dir
};
};
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
"mocha": "^10.2.0",
"node-notifier": "^10.0.1",
"npm-run-all": "^4.1.5",
"outdent": "^0.8.0",
"prettier": "^2.8.2",
"proxyquire": "^2.1.3",
"revalidator": "^0.3.1",
Expand Down