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

add option to display element's props with warn/error message #11

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 19 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,42 @@ var assertAccessibility = (tagName, props, children, log) => {
var tagTests = assertions.tags[tagName];
if (tagTests)
for (key in tagTests)
log(tagTests[key].test(tagName, props, children), tagTests[key].msg);
log(tagTests[key].test(tagName, props, children), tagTests[key].msg, props);

var propTests;
for (var propName in props) {
propTests = assertions.props[propName];
if (propTests)
for (key in propTests)
log(propTests[key].test(tagName, props, children), propTests[key].msg);
log(propTests[key].test(tagName, props, children), propTests[key].msg, props);
}
};

var error = (passed, msg) => {
var error = (passed, msg, props, options) => {
var errMsg = options && options.showProps ? msg + " - Element's props:" + JSON.stringify(props) : msg;
if (!passed)
throw new Error(msg);
throw new Error(errMsg);
};

var warn = (passed, msg) => {
var warn = (passed, msg, props, options) => {
var warnMsg = options && options.showProps ? msg + " - Element's props:" + JSON.stringify(props) : msg;
if (!passed)
console.warn(msg);
console.warn(warnMsg);
};

// borrowed from https://github.com/rackt/react-a11y/pull/9/files#diff-6d186b954a58d5bb740f73d84fe39073R36
var getLogger = (options) => {
var log = options && options.throw ? error : warn;

return (passed, msg, props) => {
log(passed, msg, props, options);
};
}


module.exports = (options) => {
var _createElement = React.createElement;
var log = options && options.throw ? error : warn;
var log = getLogger(options);
React.createElement = function (type, _props, ...children) {
if (typeof type === 'string') {
var props = _props || {};
Expand Down