Skip to content

Commit 803325d

Browse files
committed
Merge pull request #14 from courseload/tag-failures
Mark all failing tags with an ID and include it in the message
2 parents f267e2d + 72ecb69 commit 803325d

3 files changed

Lines changed: 29 additions & 19 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
dist
2+
node_modules/

lib/__tests__/index-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var k = () => {};
88
var captureWarnings = (fn) => {
99
var _warn = console.warn;
1010
var msgs = {};
11-
console.warn = (msg) => msgs[msg] = true;
11+
console.warn = (id, msg) => msgs[msg] = true;
1212
fn();
1313
console.warn = _warn;
1414
return msgs;

lib/index.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,51 @@
11
var React = require('react');
22
var assertions = require('./assertions');
33

4-
var assertAccessibility = (tagName, props, children, log) => {
4+
var assertAccessibility = (tagName, props, children) => {
55
var key;
6+
var failures = [];
67

7-
var tagTests = assertions.tags[tagName];
8-
if (tagTests)
9-
for (key in tagTests)
10-
log(tagTests[key].test(tagName, props, children), tagTests[key].msg);
8+
var tagTests = assertions.tags[tagName] || [];
9+
for (key in tagTests)
10+
if (tagTests[key] && !tagTests[key].test(tagName, props, children))
11+
failures.push(tagTests[key].msg);
1112

1213
var propTests;
1314
for (var propName in props) {
1415
if (props[propName] === null || props[propName] === undefined) continue;
15-
propTests = assertions.props[propName];
16-
if (propTests)
17-
for (key in propTests)
18-
log(propTests[key].test(tagName, props, children), propTests[key].msg);
16+
propTests = assertions.props[propName] || [];
17+
for (key in propTests)
18+
if (propTests[key] && !propTests[key].test(tagName, props, children))
19+
failures.push(propTests[key].msg);
1920
}
21+
return failures;
2022
};
2123

22-
var error = (passed, msg) => {
23-
if (!passed)
24-
throw new Error(msg);
24+
var error = (id, msg) => {
25+
throw new Error('#' + id + ": " + msg);
2526
};
2627

27-
var warn = (passed, msg) => {
28-
if (!passed)
29-
console.warn(msg);
28+
var warn = (id, msg) => {
29+
console.warn('#' + id, msg);
3030
};
3131

32+
var nextId = 0;
3233
module.exports = (options) => {
3334
var _createElement = React.createElement;
3435
var log = options && options.throw ? error : warn;
3536
React.createElement = function (type, _props, ...children) {
37+
var props = _props || {};
3638
if (typeof type === 'string') {
37-
var props = _props || {};
38-
assertAccessibility(type, props, children, log);
39+
var failures = assertAccessibility(type, props, children);
40+
if (failures.length) {
41+
// Generate an id if one doesn't exist
42+
props.id = (props.id || 'a11y-' + nextId++);
43+
44+
for (var i = 0; i < failures.length; i++)
45+
log(props.id, failures[i]);
46+
}
3947
}
40-
return _createElement.apply(this, arguments);
48+
// make sure props with the id is passed down, even if no props were passed in.
49+
return _createElement.apply(this, [type, props].concat(children));
4150
};
4251
};

0 commit comments

Comments
 (0)