diff --git a/src/addons/__tests__/ReactDOMFactories-test.js b/src/addons/__tests__/ReactDOMFactories-test.js index b4a7a78ceda5..641faa3f003d 100644 --- a/src/addons/__tests__/ReactDOMFactories-test.js +++ b/src/addons/__tests__/ReactDOMFactories-test.js @@ -17,13 +17,15 @@ var {div} = require('ReactDOMFactories'); describe('ReactDOMFactories', () => { it('allow factories to be called without warnings', () => { spyOn(console, 'error'); + spyOn(console, 'warn'); var element = div(); expect(element.type).toBe('div'); expect(console.error).not.toHaveBeenCalled(); + expect(console.warn).not.toHaveBeenCalled(); }); it('warns once when accessing React.DOM methods', () => { - spyOn(console, 'error'); + spyOn(console, 'warn'); var a = React.DOM.a(); var p = React.DOM.p(); @@ -31,8 +33,8 @@ describe('ReactDOMFactories', () => { expect(a.type).toBe('a'); expect(p.type).toBe('p'); - expect(console.error).toHaveBeenCalledTimes(1); - expect(console.error.calls.first().args[0]).toContain( + expect(console.warn).toHaveBeenCalledTimes(1); + expect(console.warn.calls.first().args[0]).toContain( 'Warning: Accessing factories like React.DOM.a has been deprecated', ); }); diff --git a/src/isomorphic/React.js b/src/isomorphic/React.js index f9cb15cc19ff..e816d569ba63 100644 --- a/src/isomorphic/React.js +++ b/src/isomorphic/React.js @@ -21,13 +21,13 @@ var ReactPropTypes = require('ReactPropTypes'); var ReactVersion = require('ReactVersion'); var onlyChild = require('onlyChild'); -var warning = require('warning'); var createElement = ReactElement.createElement; var createFactory = ReactElement.createFactory; var cloneElement = ReactElement.cloneElement; if (__DEV__) { + var lowPriorityWarning = require('lowPriorityWarning'); var canDefineProperty = require('canDefineProperty'); var ReactElementValidator = require('ReactElementValidator'); var didWarnPropTypesDeprecated = false; @@ -45,7 +45,7 @@ if (__DEV__) { var warnedForSpread = false; var warnedForCreateMixin = false; __spread = function() { - warning( + lowPriorityWarning( warnedForSpread, 'React.__spread is deprecated and should not be used. Use ' + 'Object.assign directly or another helper function with similar ' + @@ -57,7 +57,7 @@ if (__DEV__) { }; createMixin = function(mixin) { - warning( + lowPriorityWarning( warnedForCreateMixin, 'React.createMixin is deprecated and should not be used. You ' + 'can use this mixin directly instead.', @@ -107,7 +107,7 @@ if (__DEV__) { if (canDefineProperty) { Object.defineProperty(React, 'PropTypes', { get() { - warning( + lowPriorityWarning( didWarnPropTypesDeprecated, 'Accessing PropTypes via the main React package is deprecated. Use ' + 'the prop-types package from npm instead.', @@ -126,7 +126,7 @@ if (__DEV__) { Object.keys(ReactDOMFactories).forEach(function(factory) { React.DOM[factory] = function(...args) { if (!warnedForFactories) { - warning( + lowPriorityWarning( false, 'Accessing factories like React.DOM.%s has been deprecated ' + 'and will be removed in the future. Use the ' + diff --git a/src/isomorphic/__tests__/React-test.js b/src/isomorphic/__tests__/React-test.js index c2098227e5e8..7639bb107529 100644 --- a/src/isomorphic/__tests__/React-test.js +++ b/src/isomorphic/__tests__/React-test.js @@ -19,21 +19,21 @@ describe('React', () => { }); it('should log a deprecation warning once when using React.__spread', () => { - spyOn(console, 'error'); + spyOn(console, 'warn'); React.__spread({}); React.__spread({}); - expect(console.error.calls.count()).toBe(1); - expect(console.error.calls.argsFor(0)[0]).toContain( + expect(console.warn.calls.count()).toBe(1); + expect(console.warn.calls.argsFor(0)[0]).toContain( 'React.__spread is deprecated and should not be used', ); }); it('should log a deprecation warning once when using React.createMixin', () => { - spyOn(console, 'error'); + spyOn(console, 'warn'); React.createMixin(); React.createMixin(); - expect(console.error.calls.count()).toBe(1); - expect(console.error.calls.argsFor(0)[0]).toContain( + expect(console.warn.calls.count()).toBe(1); + expect(console.warn.calls.argsFor(0)[0]).toContain( 'React.createMixin is deprecated and should not be used', ); }); diff --git a/src/isomorphic/classic/element/ReactElementValidator.js b/src/isomorphic/classic/element/ReactElementValidator.js index f7ee32194eab..17f0332f8d2e 100644 --- a/src/isomorphic/classic/element/ReactElementValidator.js +++ b/src/isomorphic/classic/element/ReactElementValidator.js @@ -27,6 +27,7 @@ var checkReactTypeSpec = require('checkReactTypeSpec'); var canDefineProperty = require('canDefineProperty'); var getIteratorFn = require('getIteratorFn'); var warning = require('warning'); +var lowPriorityWarning = require('lowPriorityWarning'); function getDeclarationErrorAddendum() { if (ReactCurrentOwner.current) { @@ -275,7 +276,7 @@ var ReactElementValidator = { Object.defineProperty(validatedFactory, 'type', { enumerable: false, get: function() { - warning( + lowPriorityWarning( false, 'Factory.type is deprecated. Access the class directly ' + 'before passing it to createFactory.', diff --git a/src/isomorphic/classic/element/__tests__/ReactElementValidator-test.js b/src/isomorphic/classic/element/__tests__/ReactElementValidator-test.js index 26e1c7217905..a772395aa6a2 100644 --- a/src/isomorphic/classic/element/__tests__/ReactElementValidator-test.js +++ b/src/isomorphic/classic/element/__tests__/ReactElementValidator-test.js @@ -444,20 +444,20 @@ describe('ReactElementValidator', () => { }); it('should warn when accessing .type on an element factory', () => { - spyOn(console, 'error'); + spyOn(console, 'warn'); function TestComponent() { return
; } var TestFactory = React.createFactory(TestComponent); expect(TestFactory.type).toBe(TestComponent); - expect(console.error.calls.count()).toBe(1); - expect(console.error.calls.argsFor(0)[0]).toBe( + expect(console.warn.calls.count()).toBe(1); + expect(console.warn.calls.argsFor(0)[0]).toBe( 'Warning: Factory.type is deprecated. Access the class directly before ' + 'passing it to createFactory.', ); // Warn once, not again expect(TestFactory.type).toBe(TestComponent); - expect(console.error.calls.count()).toBe(1); + expect(console.warn.calls.count()).toBe(1); }); it('does not warn when using DOM node as children', () => { diff --git a/src/isomorphic/modern/class/ReactComponent.js b/src/isomorphic/modern/class/ReactComponent.js index f0ab6fd090ca..aa03ca82045c 100644 --- a/src/isomorphic/modern/class/ReactComponent.js +++ b/src/isomorphic/modern/class/ReactComponent.js @@ -16,7 +16,7 @@ var ReactNoopUpdateQueue = require('ReactNoopUpdateQueue'); var canDefineProperty = require('canDefineProperty'); var emptyObject = require('emptyObject'); var invariant = require('invariant'); -var warning = require('warning'); +var lowPriorityWarning = require('lowPriorityWarning'); /** * Base class helpers for the updating state of a component. @@ -114,7 +114,7 @@ if (__DEV__) { if (canDefineProperty) { Object.defineProperty(ReactComponent.prototype, methodName, { get: function() { - warning( + lowPriorityWarning( false, '%s(...) is deprecated in plain JavaScript React classes. %s', info[0], diff --git a/src/isomorphic/modern/class/__tests__/ReactCoffeeScriptClass-test.coffee b/src/isomorphic/modern/class/__tests__/ReactCoffeeScriptClass-test.coffee index 2e4c19c3c2ac..23661be18e54 100644 --- a/src/isomorphic/modern/class/__tests__/ReactCoffeeScriptClass-test.coffee +++ b/src/isomorphic/modern/class/__tests__/ReactCoffeeScriptClass-test.coffee @@ -378,16 +378,16 @@ describe 'ReactCoffeeScriptClass', -> undefined it 'should throw AND warn when trying to access classic APIs', -> - spyOn console, 'error' + spyOn console, 'warn' instance = test Inner(name: 'foo'), 'DIV', 'foo' expect(-> instance.replaceState {}).toThrow() expect(-> instance.isMounted()).toThrow() - expect(console.error.calls.count()).toBe 2 - expect(console.error.calls.argsFor(0)[0]).toContain( + expect(console.warn.calls.count()).toBe 2 + expect(console.warn.calls.argsFor(0)[0]).toContain( 'replaceState(...) is deprecated in plain JavaScript React classes' ) - expect(console.error.calls.argsFor(1)[0]).toContain( + expect(console.warn.calls.argsFor(1)[0]).toContain( 'isMounted(...) is deprecated in plain JavaScript React classes' ) undefined diff --git a/src/isomorphic/modern/class/__tests__/ReactES6Class-test.js b/src/isomorphic/modern/class/__tests__/ReactES6Class-test.js index 2cad6c5dc6e1..ee63a7af70ab 100644 --- a/src/isomorphic/modern/class/__tests__/ReactES6Class-test.js +++ b/src/isomorphic/modern/class/__tests__/ReactES6Class-test.js @@ -408,15 +408,15 @@ describe('ReactES6Class', () => { }); it('should throw AND warn when trying to access classic APIs', () => { - spyOn(console, 'error'); + spyOn(console, 'warn'); var instance = test(, 'DIV', 'foo'); expect(() => instance.replaceState({})).toThrow(); expect(() => instance.isMounted()).toThrow(); - expect(console.error.calls.count()).toBe(2); - expect(console.error.calls.argsFor(0)[0]).toContain( + expect(console.warn.calls.count()).toBe(2); + expect(console.warn.calls.argsFor(0)[0]).toContain( 'replaceState(...) is deprecated in plain JavaScript React classes', ); - expect(console.error.calls.argsFor(1)[0]).toContain( + expect(console.warn.calls.argsFor(1)[0]).toContain( 'isMounted(...) is deprecated in plain JavaScript React classes', ); }); diff --git a/src/isomorphic/modern/class/__tests__/ReactTypeScriptClass-test.ts b/src/isomorphic/modern/class/__tests__/ReactTypeScriptClass-test.ts index 6f4a2d0faf7b..d7de7469d343 100644 --- a/src/isomorphic/modern/class/__tests__/ReactTypeScriptClass-test.ts +++ b/src/isomorphic/modern/class/__tests__/ReactTypeScriptClass-test.ts @@ -502,18 +502,18 @@ describe('ReactTypeScriptClass', function() { }); it('should throw AND warn when trying to access classic APIs', function() { - spyOn(console, 'error'); + spyOn(console, 'warn'); var instance = test( React.createElement(Inner, {name: 'foo'}), 'DIV','foo' ); expect(() => instance.replaceState({})).toThrow(); expect(() => instance.isMounted()).toThrow(); - expect((console.error).calls.count()).toBe(2); - expect((console.error).calls.argsFor(0)[0]).toContain( + expect((console.warn).calls.count()).toBe(2); + expect((console.warn).calls.argsFor(0)[0]).toContain( 'replaceState(...) is deprecated in plain JavaScript React classes' ); - expect((console.error).calls.argsFor(1)[0]).toContain( + expect((console.warn).calls.argsFor(1)[0]).toContain( 'isMounted(...) is deprecated in plain JavaScript React classes' ); }); diff --git a/src/renderers/dom/client/__tests__/ReactDOM-test.js b/src/renderers/dom/client/__tests__/ReactDOM-test.js index e5b143e90697..b55e913ffba9 100644 --- a/src/renderers/dom/client/__tests__/ReactDOM-test.js +++ b/src/renderers/dom/client/__tests__/ReactDOM-test.js @@ -110,10 +110,10 @@ describe('ReactDOM', () => { }); it('throws warning when React.DOM factories are called', () => { - spyOn(console, 'error'); + spyOn(console, 'warn'); var element = React.DOM.div(); expect(element.type).toBe('div'); - expect(console.error.calls.count()).toBe(1); + expect(console.warn.calls.count()).toBe(1); }); it('throws in render() if the mount callback is not a function', () => { diff --git a/src/renderers/shared/ReactPerf.js b/src/renderers/shared/ReactPerf.js index 7f65b170b99e..17abb58095e7 100644 --- a/src/renderers/shared/ReactPerf.js +++ b/src/renderers/shared/ReactPerf.js @@ -13,7 +13,7 @@ 'use strict'; var ReactDebugTool = require('ReactDebugTool'); -var warning = require('warning'); +var lowPriorityWarning = require('lowPriorityWarning'); var alreadyWarned = false; import type {FlushHistory} from 'ReactDebugTool'; @@ -390,7 +390,7 @@ function printOperations(flushHistory?: FlushHistory) { var warnedAboutPrintDOM = false; function printDOM(measurements: FlushHistory) { - warning( + lowPriorityWarning( warnedAboutPrintDOM, '`ReactPerf.printDOM(...)` is deprecated. Use ' + '`ReactPerf.printOperations(...)` instead.', @@ -401,7 +401,7 @@ function printDOM(measurements: FlushHistory) { var warnedAboutGetMeasurementsSummaryMap = false; function getMeasurementsSummaryMap(measurements: FlushHistory) { - warning( + lowPriorityWarning( warnedAboutGetMeasurementsSummaryMap, '`ReactPerf.getMeasurementsSummaryMap(...)` is deprecated. Use ' + '`ReactPerf.getWasted(...)` instead.', diff --git a/src/renderers/shared/__tests__/ReactPerf-test.js b/src/renderers/shared/__tests__/ReactPerf-test.js index fb64a841a7dd..3c5f3d58e63d 100644 --- a/src/renderers/shared/__tests__/ReactPerf-test.js +++ b/src/renderers/shared/__tests__/ReactPerf-test.js @@ -401,30 +401,30 @@ describe('ReactPerf', () => { it('warns once when using getMeasurementsSummaryMap', () => { var measurements = measure(() => {}); - spyOn(console, 'error'); + spyOn(console, 'warn'); ReactPerf.getMeasurementsSummaryMap(measurements); - expect(console.error.calls.count()).toBe(1); - expect(console.error.calls.argsFor(0)[0]).toContain( + expect(console.warn.calls.count()).toBe(1); + expect(console.warn.calls.argsFor(0)[0]).toContain( '`ReactPerf.getMeasurementsSummaryMap(...)` is deprecated. Use ' + '`ReactPerf.getWasted(...)` instead.', ); ReactPerf.getMeasurementsSummaryMap(measurements); - expect(console.error.calls.count()).toBe(1); + expect(console.warn.calls.count()).toBe(1); }); it('warns once when using printDOM', () => { var measurements = measure(() => {}); - spyOn(console, 'error'); + spyOn(console, 'warn'); ReactPerf.printDOM(measurements); - expect(console.error.calls.count()).toBe(1); - expect(console.error.calls.argsFor(0)[0]).toContain( + expect(console.warn.calls.count()).toBe(1); + expect(console.warn.calls.argsFor(0)[0]).toContain( '`ReactPerf.printDOM(...)` is deprecated. Use ' + '`ReactPerf.printOperations(...)` instead.', ); ReactPerf.printDOM(measurements); - expect(console.error.calls.count()).toBe(1); + expect(console.warn.calls.count()).toBe(1); }); it('returns isRunning state', () => { diff --git a/src/shared/utils/deprecated.js b/src/shared/utils/deprecated.js index e9158dbc86de..81e24cfd98d5 100644 --- a/src/shared/utils/deprecated.js +++ b/src/shared/utils/deprecated.js @@ -12,7 +12,7 @@ 'use strict'; -var warning = require('warning'); +var lowPriorityWarning = require('lowPriorityWarning'); /** * This will log a single deprecation notice per function and forward the call @@ -35,7 +35,7 @@ function deprecated( var warned = false; if (__DEV__) { var newFn = function() { - warning( + lowPriorityWarning( warned, /* eslint-disable no-useless-concat */ // Require examples in this string must be split to prevent React's diff --git a/src/shared/utils/lowPriorityWarning.js b/src/shared/utils/lowPriorityWarning.js new file mode 100644 index 000000000000..d4deb584dccc --- /dev/null +++ b/src/shared/utils/lowPriorityWarning.js @@ -0,0 +1,41 @@ +/** + * Copyright 2014-2015, Facebook, Inc. + * All rights reserved. + * + * 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. + * + * @providesModule lowPriorityWarning + */ + +'use strict'; + +/** + * Forked from fbjs/warning: + * https://github.com/facebook/fbjs/blob/e66ba20ad5be433eb54423f2b097d829324d9de6/packages/fbjs/src/__forks__/warning.js + * + * Only change is we use console.warn instead of console.error, + * and do nothing when 'console' is not supported. + * This really simplifies the code. + * --- + * + * Similar to invariant but only logs a warning if the condition is not met. + * This can be used to log issues in development environments in critical + * paths. Removing the logging code for production environments will keep the + * same logic and follow the same code paths. + */ + +var lowPriorityWarning = function() {}; + +if (__DEV__) { + lowPriorityWarning = function(condition, format, ...args) { + var argIndex = 0; + var message = 'Warning: ' + format.replace(/%s/g, () => args[argIndex++]); + if (!condition && typeof console !== 'undefined') { + console.warn(message); + } + }; +} + +module.exports = lowPriorityWarning;