Skip to content

Commit

Permalink
fix: Update warning to use the latest version from facebook/fbjs
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This changes the internal workings. A major release is
made to ensure minimal effect on downstream users.
  • Loading branch information
Emeegeemee authored and BerkeleyTrue committed May 22, 2018
1 parent 3dad9ee commit 0572ddd
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 66 deletions.
58 changes: 30 additions & 28 deletions browser.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* Copyright 2014-2015, Facebook, Inc.
* All rights reserved.
* Copyright (c) 2014-present, Facebook, Inc.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule warning
*/

'use strict';
Expand All @@ -19,40 +19,42 @@
var warning = function() {};

if (process.env.NODE_ENV !== 'production') {
warning = function(condition, format, args) {
function printWarning(format, args) {
var len = arguments.length;
args = new Array(len > 2 ? len - 2 : 0);
for (var key = 2; key < len; key++) {
args[key - 2] = arguments[key];
}
if (format === undefined) {
throw new Error(
'`warning(condition, format, ...args)` requires a warning ' +
'message argument'
);
var argIndex = 0;
var message = 'Warning: ' +
format.replace(/%s/g, function() {
return args[argIndex++];
});
if (typeof console !== 'undefined') {
console.error(message);
}
try {
// --- Welcome to debugging React ---
// This error was thrown as a convenience so that you can use this stack
// to find the callsite that caused this warning to fire.
throw new Error(message);
} catch (x) {}
}

if (format.length < 10 || (/^[s\W]*$/).test(format)) {
warning = function(condition, format, args) {
var len = arguments.length;
args = new Array(len > 2 ? len - 2 : 0);
for (var key = 2; key < len; key++) {
args[key - 2] = arguments[key];
}
if (format === undefined) {
throw new Error(
'The warning format should be able to uniquely identify this ' +
'warning. Please, use a more descriptive format than: ' + format
'`warning(condition, format, ...args)` requires a warning ' +
'message argument'
);
}

if (!condition) {
var argIndex = 0;
var message = 'Warning: ' +
format.replace(/%s/g, function() {
return args[argIndex++];
});
if (typeof console !== 'undefined') {
console.error(message);
}
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.
throw new Error(message);
} catch(x) {}
printWarning.apply(null, [format].concat(args));
}
};
}
Expand Down
8 changes: 0 additions & 8 deletions test/package/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@ module.exports = function(t) {
warning(false);
}, /requires a warning/i);

t.throws(function() {
warning(true, 'short');
}, /use a more descriptive format/i);

t.throws(function() {
warning(false, 'short');
}, /use a more descriptive format/i);

var error = console.error;

console.error = function(msg) {
Expand Down
4 changes: 2 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ var vm = require('vm');
var file = __dirname + '/package/' + process.env.NODE_ENV + '.js';

test('node', function(t) {
t.plan(5);
t.plan(3);
require(file)(t);
});

test('browserify', function(t) {
t.plan(8);
t.plan(6);
var b = browserify({
entries: file,
standalone: 'package'
Expand Down
58 changes: 30 additions & 28 deletions warning.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* Copyright 2014-2015, Facebook, Inc.
* All rights reserved.
* Copyright (c) 2014-present, Facebook, Inc.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule warning
*/

'use strict';
Expand All @@ -21,40 +21,42 @@ var __DEV__ = process.env.NODE_ENV !== 'production';
var warning = function() {};

if (__DEV__) {
warning = function(condition, format, args) {
function printWarning(format, args) {
var len = arguments.length;
args = new Array(len > 2 ? len - 2 : 0);
for (var key = 2; key < len; key++) {
args[key - 2] = arguments[key];
}
if (format === undefined) {
throw new Error(
'`warning(condition, format, ...args)` requires a warning ' +
'message argument'
);
var argIndex = 0;
var message = 'Warning: ' +
format.replace(/%s/g, function() {
return args[argIndex++];
});
if (typeof console !== 'undefined') {
console.error(message);
}
try {
// --- Welcome to debugging React ---
// This error was thrown as a convenience so that you can use this stack
// to find the callsite that caused this warning to fire.
throw new Error(message);
} catch (x) {}
}

if (format.length < 10 || (/^[s\W]*$/).test(format)) {
warning = function(condition, format, args) {
var len = arguments.length;
args = new Array(len > 2 ? len - 2 : 0);
for (var key = 2; key < len; key++) {
args[key - 2] = arguments[key];
}
if (format === undefined) {
throw new Error(
'The warning format should be able to uniquely identify this ' +
'warning. Please, use a more descriptive format than: ' + format
'`warning(condition, format, ...args)` requires a warning ' +
'message argument'
);
}

if (!condition) {
var argIndex = 0;
var message = 'Warning: ' +
format.replace(/%s/g, function() {
return args[argIndex++];
});
if (typeof console !== 'undefined') {
console.error(message);
}
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.
throw new Error(message);
} catch(x) {}
printWarning.apply(null, [format].concat(args));
}
};
}
Expand Down

0 comments on commit 0572ddd

Please sign in to comment.