Skip to content

Commit

Permalink
feat(utils.getRule): add function to get rule by id (#2724)
Browse files Browse the repository at this point in the history
* feat(utils.getRule): add function to get rule by id

* replace
  • Loading branch information
straker authored Jan 5, 2021
1 parent 508190b commit 9d0af53
Show file tree
Hide file tree
Showing 35 changed files with 82 additions and 95 deletions.
9 changes: 7 additions & 2 deletions lib/core/public/run-virtual-rule.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import SerialVirtualNode from '../base/virtual-node/serial-virtual-node';
import AbstractVirtualNode from '../base/virtual-node/abstract-virtual-node';
import * as helpers from '../reporters/helpers';
import { publishMetaData, finalizeRuleResult, aggregateResult } from '../utils';
import {
publishMetaData,
finalizeRuleResult,
aggregateResult,
getRule
} from '../utils';

/**
* Run a rule in a non-browser environment
Expand All @@ -20,7 +25,7 @@ function runVirtualRule(ruleId, vNode, options = {}) {
vNode = new SerialVirtualNode(vNode);
}

let rule = axe._audit.rules.find(rule => rule.id === ruleId);
let rule = getRule(ruleId);

if (!rule) {
throw new Error('unknown rule `' + ruleId + '`');
Expand Down
2 changes: 2 additions & 0 deletions lib/core/utils/finalize-result.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import aggregateNodeResults from './aggregate-node-results';
* @return {object}
*/
function finalizeRuleResult(ruleResult) {
// we don't use getRule so that this code does not throw but returns
// the results
const rule = axe._audit.rules.find(rule => rule.id === ruleResult.id);
if (rule && rule.impact) {
ruleResult.nodes.forEach(node => {
Expand Down
17 changes: 17 additions & 0 deletions lib/core/utils/get-rule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Get an axe rule by id.
* @param {String} ruelId the rule id
* @return {Rule}
*/
function getRule(ruleId) {
// TODO: es-modules_audit
var rule = axe._audit.rules.find(rule => rule.id === ruleId);

if (!rule) {
throw new Error(`Cannot find rule by id: ${ruleId}`);
}

return rule;
}

export default getRule;
1 change: 1 addition & 0 deletions lib/core/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export { default as getFriendlyUriEnd } from './get-friendly-uri-end';
export { default as getNodeAttributes } from './get-node-attributes';
export { default as getNodeFromTree } from './get-node-from-tree';
export { default as getRootNode } from './get-root-node';
export { default as getRule } from './get-rule';
export { default as getScrollState } from './get-scroll-state';
export { default as getScroll } from './get-scroll';
export { default as getShadowSelector } from './get-shadow-selector';
Expand Down
25 changes: 25 additions & 0 deletions test/core/utils/get-rule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
describe('axe.utils.getRule', function() {
beforeEach(function() {
axe._load({
rules: [
{
id: 'rule1'
},
{
id: 'rule2'
}
]
});
});

it('should return the rule by the id', function() {
var rule = axe.utils.getRule('rule1');
assert.isTrue(rule.id === 'rule1');
});

it("should throw error if the rule doesn't exist", function() {
assert.throws(function() {
axe.utils.getRule('no-id');
});
});
});
4 changes: 1 addition & 3 deletions test/integration/rules/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
}

var fixture = document.getElementById('fixture');
var rule = axe._audit.rules.find(function(rule) {
return rule.id === ruleId;
});
var rule = axe.utils.getRule(ruleId);

if (!rule) {
return;
Expand Down
4 changes: 1 addition & 3 deletions test/rule-matches/aria-allowed-attr-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ describe('aria-allowed-attr-matches', function() {
var rule;

beforeEach(function() {
rule = axe._audit.rules.find(function(rule) {
return rule.id === 'aria-allowed-attr';
});
rule = axe.utils.getRule('aria-allowed-attr');
});

afterEach(function() {
Expand Down
4 changes: 1 addition & 3 deletions test/rule-matches/aria-allowed-role-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ describe('aria-allowed-role-matches', function() {
var rule;

beforeEach(function() {
rule = axe._audit.rules.find(function(rule) {
return rule.id === 'aria-allowed-role';
});
rule = axe.utils.getRule('aria-allowed-role');
});

afterEach(function() {
Expand Down
4 changes: 1 addition & 3 deletions test/rule-matches/aria-has-attr-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ describe('aria-has-attr-matches', function() {
var rule;

beforeEach(function() {
rule = axe._audit.rules.find(function(rule) {
return rule.id === 'aria-valid-attr-value';
});
rule = axe.utils.getRule('aria-valid-attr-value');
});

afterEach(function() {
Expand Down
4 changes: 1 addition & 3 deletions test/rule-matches/aria-hidden-focus-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ describe('aria-hidden-focus-matches', function() {
var fixtureSetup = axe.testUtils.fixtureSetup;

beforeEach(function() {
rule = axe._audit.rules.find(function(rule) {
return rule.id === 'aria-hidden-focus';
});
rule = axe.utils.getRule('aria-hidden-focus');
});

afterEach(function() {
Expand Down
4 changes: 1 addition & 3 deletions test/rule-matches/aria-required-children-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ describe('aria-required-children-matches', function() {
var rule;

beforeEach(function() {
rule = axe._audit.rules.find(function(rule) {
return rule.id === 'aria-required-children';
});
rule = axe.utils.getRule('aria-required-children');
});

afterEach(function() {
Expand Down
4 changes: 1 addition & 3 deletions test/rule-matches/autocomplete-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ describe('autocomplete-matches', function() {
'use strict';
var fixture = document.getElementById('fixture');
var queryFixture = axe.testUtils.queryFixture;
var rule = axe._audit.rules.find(function(rule) {
return rule.id === 'autocomplete-valid';
});
var rule = axe.utils.getRule('autocomplete-valid');

afterEach(function() {
fixture.innerHTML = '';
Expand Down
4 changes: 1 addition & 3 deletions test/rule-matches/color-contrast-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ describe('color-contrast-matches', function() {
var rule;

beforeEach(function() {
rule = axe._audit.rules.find(function(rule) {
return rule.id === 'color-contrast';
});
rule = axe.utils.getRule('color-contrast');
});

afterEach(function() {
Expand Down
4 changes: 1 addition & 3 deletions test/rule-matches/data-table-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ describe('data-table-matches', function() {
var rule;

beforeEach(function() {
rule = axe._audit.rules.find(function(rule) {
return rule.id === 'th-has-data-cells';
});
rule = axe.utils.getRule('th-has-data-cells');
});

afterEach(function() {
Expand Down
4 changes: 1 addition & 3 deletions test/rule-matches/duplicate-id-active-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ describe('duplicate-id-active matches', function() {
var rule;

beforeEach(function() {
rule = axe._audit.rules.find(function(rule) {
return rule.id === 'duplicate-id-active';
});
rule = axe.utils.getRule('duplicate-id-active');
});

afterEach(function() {
Expand Down
4 changes: 1 addition & 3 deletions test/rule-matches/duplicate-id-aria-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ describe('duplicate-id-aria matches', function() {
var rule;

beforeEach(function() {
rule = axe._audit.rules.find(function(rule) {
return rule.id === 'duplicate-id-aria';
});
rule = axe.utils.getRule('duplicate-id-aria');
});

afterEach(function() {
Expand Down
4 changes: 1 addition & 3 deletions test/rule-matches/duplicate-id-misc-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ describe('duplicate-id-misc matches', function() {
var rule;

beforeEach(function() {
rule = axe._audit.rules.find(function(rule) {
return rule.id === 'duplicate-id';
});
rule = axe.utils.getRule('duplicate-id');
});

afterEach(function() {
Expand Down
4 changes: 1 addition & 3 deletions test/rule-matches/heading-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ describe('heading-matches', function() {
var rule;

beforeEach(function() {
rule = axe._audit.rules.find(function(rule) {
return rule.id === 'empty-heading';
});
rule = axe.utils.getRule('empty-heading');
});

afterEach(function() {
Expand Down
4 changes: 1 addition & 3 deletions test/rule-matches/html-namespace-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ describe('html-namespace-matches', function() {
beforeEach(function() {
fixture = document.getElementById('fixture');
axeFixtureSetup = axe.testUtils.fixtureSetup;
rule = axe._audit.rules.find(function(rule) {
return rule.id === 'role-img-alt';
});
rule = axe.utils.getRule('role-img-alt');
});

afterEach(function() {
Expand Down
4 changes: 1 addition & 3 deletions test/rule-matches/html-xml-lang-mismatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ describe('xml-lang-mismatch-matches', function() {
var fixture = document.getElementById('fixture');

beforeEach(function() {
rule = axe._audit.rules.find(function(rule) {
return rule.id === 'html-xml-lang-mismatch';
});
rule = axe.utils.getRule('html-xml-lang-mismatch');
dom = document.createElement('html');
});

Expand Down
4 changes: 1 addition & 3 deletions test/rule-matches/identical-links-same-purpose-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ describe('identical-links-same-purpose-matches tests', function() {
var fixture = document.getElementById('fixture');
var queryFixture = axe.testUtils.queryFixture;
var isPhantom = window.PHANTOMJS ? true : false;
var rule = axe._audit.rules.find(function(rule) {
return rule.id === 'identical-links-same-purpose';
});
var rule = axe.utils.getRule('identical-links-same-purpose');

afterEach(function() {
fixture.innerHTML = '';
Expand Down
4 changes: 1 addition & 3 deletions test/rule-matches/is-initiator-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ describe('is-initiator-matches', function() {
var rule;

beforeEach(function() {
rule = axe._audit.rules.find(function(rule) {
return rule.id === 'html-has-lang';
});
rule = axe.utils.getRule('html-has-lang');
});

afterEach(function() {
Expand Down
4 changes: 1 addition & 3 deletions test/rule-matches/label-content-name-mismatch-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ describe('label-content-name-mismatch-matches tests', function() {

var fixture = document.getElementById('fixture');
var queryFixture = axe.testUtils.queryFixture;
var rule = axe._audit.rules.find(function(rule) {
return rule.id === 'label-content-name-mismatch';
});
var rule = axe.utils.getRule('label-content-name-mismatch');

afterEach(function() {
fixture.innerHTML = '';
Expand Down
4 changes: 1 addition & 3 deletions test/rule-matches/label-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ describe('label-matches', function() {

beforeEach(function() {
fixture.innerHTML = '';
rule = axe._audit.rules.find(function(rule) {
return rule.id === 'label';
});
rule = axe.utils.getRule('label');
});

it('returns true for non-input elements', function() {
Expand Down
4 changes: 1 addition & 3 deletions test/rule-matches/landmark-has-body-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ describe('landmark-has-body-context', function() {
var shadowSupport = axe.testUtils.shadowSupport.v1;

beforeEach(function() {
rule = axe._audit.rules.find(function(rule) {
return rule.id === 'landmark-banner-is-top-level';
});
rule = axe.utils.getRule('landmark-banner-is-top-level');
});

it('returns true for elements with a role', function() {
Expand Down
4 changes: 1 addition & 3 deletions test/rule-matches/landmark-unique-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ describe('landmark-unique-matches', function() {
beforeEach(function() {
fixture = document.getElementById('fixture');
axeFixtureSetup = axe.testUtils.fixtureSetup;
rule = axe._audit.rules.find(function(rule) {
return rule.id === 'landmark-unique';
});
rule = axe.utils.getRule('landmark-unique');
});

it('should not match because not a landmark', function() {
Expand Down
4 changes: 1 addition & 3 deletions test/rule-matches/nested-interactive-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ describe('nested-interactive-matches', function() {
var rule;

beforeEach(function() {
rule = axe._audit.rules.find(function(rule) {
return rule.id === 'nested-interactive';
});
rule = axe.utils.getRule('nested-interactive');
});

afterEach(function() {
Expand Down
4 changes: 1 addition & 3 deletions test/rule-matches/no-autoplay-audio-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ describe('no-autoplay-audio-matches', function() {
var rule;

beforeEach(function() {
rule = axe._audit.rules.find(function(rule) {
return rule.id === 'no-autoplay-audio';
});
rule = axe.utils.getRule('no-autoplay-audio');
});

afterEach(function() {
Expand Down
4 changes: 1 addition & 3 deletions test/rule-matches/no-empty-role-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ describe('no-role-empty-matches', function() {
var rule;

beforeEach(function() {
rule = axe._audit.rules.find(function(rule) {
return rule.id === 'aria-roles';
});
rule = axe.utils.getRule('aria-roles');
});

afterEach(function() {
Expand Down
4 changes: 1 addition & 3 deletions test/rule-matches/no-explicit-name-required-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ describe('no-explicit-name-matches', function() {
var rule;

beforeEach(function() {
rule = axe._audit.rules.find(function(rule) {
return rule.id === 'button-name';
});
rule = axe.utils.getRule('button-name');
});

afterEach(function() {
Expand Down
7 changes: 1 addition & 6 deletions test/rule-matches/no-naming-method-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ describe('no-naming-method-matches', function() {

var fixture = document.getElementById('fixture');
var queryFixture = axe.testUtils.queryFixture;
var rule = axe._audit.rules.find(function(rule) {
return (
rule.id === 'aria-toggle-field-name' ||
rule.id === 'aria-input-field-name'
);
});
var rule = axe.utils.getRule('aria-toggle-field-name');

afterEach(function() {
fixture.innerHTML = '';
Expand Down
4 changes: 1 addition & 3 deletions test/rule-matches/p-as-heading-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ describe('p-as-heading-matches', function() {
var fixture = document.getElementById('fixture');

beforeEach(function() {
rule = axe._audit.rules.find(function(rule) {
return rule.id === 'p-as-heading';
});
rule = axe.utils.getRule('p-as-heading');
});

afterEach(function() {
Expand Down
4 changes: 1 addition & 3 deletions test/rule-matches/scrollable-region-focusable-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ describe('scrollable-region-focusable-matches', function() {
var fixture = document.getElementById('fixture');
var queryFixture = axe.testUtils.queryFixture;
var shadowSupported = axe.testUtils.shadowSupport.v1;
var rule = axe._audit.rules.find(function(rule) {
return rule.id === 'scrollable-region-focusable';
});
var rule = axe.utils.getRule('scrollable-region-focusable');

afterEach(function() {
fixture.innerHTML = '';
Expand Down
Loading

0 comments on commit 9d0af53

Please sign in to comment.