From 68f7075954d66617de5933dbd32a51195d058cd6 Mon Sep 17 00:00:00 2001 From: andycall Date: Sun, 15 Sep 2019 22:10:42 +0800 Subject: [PATCH] feat: add warning function Signed-off-by: andycall --- packages/rax/src/warning.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 packages/rax/src/warning.js diff --git a/packages/rax/src/warning.js b/packages/rax/src/warning.js new file mode 100644 index 0000000000..1a08e84083 --- /dev/null +++ b/packages/rax/src/warning.js @@ -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;