Skip to content

Commit

Permalink
feat: add warning function
Browse files Browse the repository at this point in the history
Signed-off-by: andycall <dongtiangche@outlook.com>
  • Loading branch information
andycall committed Sep 16, 2019
1 parent 6c5b193 commit 68f7075
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions packages/rax/src/warning.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
let warning = () => {};

if (process.env.NODE_ENV !== 'production') {
warning = (condition, format, ...args) => {
if (format === undefined) {
throw new Error(
'`warningWithoutStack(condition, format, ...args)` requires a warning ' +
'message argument',
);
}
if (args.length > 8) {
// Check before the condition to catch violations early.
throw new Error(
'warningWithoutStack() currently supports at most 8 arguments.',
);
}
if (condition) {
return;
}
if (typeof console !== 'undefined') {
const argsWithFormat = args.map(item => '' + item);
argsWithFormat.unshift('Warning: ' + format);

Function.prototype.apply.call(console.error, console, argsWithFormat);
}
try {
// This error was thrown as a convenience so that you can use this stack
// to find the callsite that caused this warning to fire.
let argIndex = 0;
const message =
'Warning: ' + format.replace(/%s/g, () => args[argIndex++]);
throw new Error(message);
} catch (x) {}
};
}

export default warning;

0 comments on commit 68f7075

Please # to comment.