@samitouri / QOS-React-2 / commits / e571b32061

Wrap calls to deprecated functions with a warning.

Paul O’Shannessy committed Jun 11, 2015 at 15:28 UTC e571b320612e81076f045b027e89de152b0a94e7
3 files changed +64 -13
grunt/config/jsx.js
+2
@@ -6,6 +6,8 @@ var grunt = require('grunt');
6 var rootIDs = [
7 'React',
8 'ReactWithAddons',
9 + // deprecated is used in the npm package but not anywhere else, so build it.
10 + 'deprecated',
11 ];
12
13 var normal = {
npm-react/react.js
+53 -1
@@ -1 +1,53 @@
1 -module.exports = require('./lib/React');
1 +'use strict';
2 +
3 +var React = require('./lib/React');
4 +
5 +var assign = require('./lib/Object.assign');
6 +var deprecated = require('./lib/deprecated');
7 +
8 +// We want to warn once when any of these methods are used.
9 +if (process.env.NODE_ENV !== 'production') {
10 + var deprecations = {
11 + // ReactDOMClient
12 + findDOMNode: deprecated(
13 + 'findDOMNode',
14 + 'react-dom',
15 + React,
16 + React.findDOMNode
17 + ),
18 + render: deprecated(
19 + 'render',
20 + 'react-dom',
21 + React,
22 + React.render
23 + ),
24 + unmountComponentAtNode: deprecated(
25 + 'unmountComponentAtNode',
26 + 'react-dom',
27 + React,
28 + React.unmountComponentAtNode
29 + ),
30 + // ReactDOMServer
31 + renderToString: deprecated(
32 + 'renderToString',
33 + 'react-dom/server',
34 + React,
35 + React.renderToString
36 + ),
37 + renderToStaticMarkup: deprecated(
38 + 'renderToStaticMarkup',
39 + 'react-dom/server',
40 + React,
41 + React.renderToStaticMarkup
42 + ),
43 + };
44 + // Export a wrapped object. We'll use assign and take advantage of the fact
45 + // that this will override the original methods in React.
46 + module.exports = assign(
47 + {},
48 + React,
49 + deprecations
50 + );
51 +} else {
52 + module.exports = React;
53 +}
src/shared/utils/deprecated.js
+9 -12
@@ -18,30 +18,27 @@ var warning = require('warning');
18 * This will log a single deprecation notice per function and forward the call
19 * on to the new API.
20 *
21 - * @param {string} namespace The namespace of the call, eg 'React'
22 - * @param {string} oldName The old function name, eg 'renderComponent'
23 - * @param {string} newName The new function name, eg 'render'
21 + * @param {string} fnName The name of the function
22 + * @param {string} newModule The module that fn will exist in
23 * @param {*} ctx The context this forwarded call should run in
24 * @param {function} fn The function to forward on to
26 - * @return {*} Will be the value as returned from `fn`
25 + * @return {function} The function that will warn once and then call fn
26 */
28 -function deprecated(namespace, oldName, newName, ctx, fn) {
27 +function deprecated(fnName, newModule, ctx, fn) {
28 var warned = false;
29 if (__DEV__) {
30 var newFn = function() {
31 warning(
32 warned,
34 - '%s.%s will be deprecated in a future version. ' +
35 - 'Use %s.%s instead.',
36 - namespace,
37 - oldName,
38 - namespace,
39 - newName
33 + '`require("react").%s` is deprecated. Please use `require("%s").%s` ' +
34 + 'instead.',
35 + fnName,
36 + newModule,
37 + fnName
38 );
39 warned = true;
40 return fn.apply(ctx, arguments);
41 };
44 - newFn.displayName = `${namespace}_${oldName}`;
42 // We need to make sure all properties of the original fn are copied over.
43 // In particular, this is needed to support PropTypes
44 return assign(newFn, fn);