Skip to content

Commit 2d0c081

Browse files
committed
[added] ability to throw instead of warn
1 parent cef9128 commit 2d0c081

3 files changed

Lines changed: 23 additions & 7 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,9 @@ if (ENV === 'development') a11y();
2929
You probably don't want to call it if you're in production, and better
3030
yet, alias the module to nothing with webpack in production.
3131

32+
If you want it to throw errors instead of just warnings:
33+
34+
```
35+
a11y({throw: true});
36+
```
37+

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.replace(/^Warning: /, '')] = true;
11+
console.warn = (msg) => msgs[msg] = true;
1212
fn();
1313
console.warn = _warn;
1414
return msgs;

lib/index.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,40 @@
11
var React = require('react');
2-
var warning = require('react/lib/warning');
32
var assertions = require('./assertions');
43

5-
var assertAccessibility = (tagName, props, children) => {
4+
var assertAccessibility = (tagName, props, children, log) => {
65
var key;
76

87
var tagTests = assertions.tags[tagName];
98
if (tagTests)
109
for (key in tagTests)
11-
warning(tagTests[key].test(tagName, props, children), tagTests[key].msg);
10+
log(tagTests[key].test(tagName, props, children), tagTests[key].msg);
1211

1312
var propTests;
1413
for (var propName in props) {
1514
propTests = assertions.props[propName];
1615
if (propTests)
1716
for (key in propTests)
18-
warning(propTests[key].test(tagName, props, children), propTests[key].msg);
17+
log(propTests[key].test(tagName, props, children), propTests[key].msg);
1918
}
2019
};
2120

22-
module.exports = () => {
21+
var error = (passed, msg) => {
22+
if (!passed)
23+
throw new Error(msg);
24+
};
25+
26+
var warn = (passed, msg) => {
27+
if (!passed)
28+
console.warn(msg);
29+
};
30+
31+
module.exports = (options) => {
2332
var _createElement = React.createElement;
33+
var log = options && options.throw ? error : warn;
2434
React.createElement = function (type, _props, ...children) {
2535
if (typeof type === 'string') {
2636
var props = _props || {};
27-
assertAccessibility(type, props, children);
37+
assertAccessibility(type, props, children, log);
2838
}
2939
return _createElement.apply(this, arguments);
3040
};

0 commit comments

Comments
 (0)