From e571b320612e81076f045b027e89de152b0a94e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20O=E2=80=99Shannessy?= Date: Thu, 11 Jun 2015 15:28:03 -0700 Subject: [PATCH] Wrap calls to deprecated functions with a warning. --- grunt/config/jsx.js | 2 ++ npm-react/react.js | 54 +++++++++++++++++++++++++++++++++- src/shared/utils/deprecated.js | 21 ++++++------- 3 files changed, 64 insertions(+), 13 deletions(-) diff --git a/grunt/config/jsx.js b/grunt/config/jsx.js index 9b91f75339..2cf6b54b30 100644 --- a/grunt/config/jsx.js +++ b/grunt/config/jsx.js @@ -6,6 +6,8 @@ var grunt = require('grunt'); var rootIDs = [ 'React', 'ReactWithAddons', + // deprecated is used in the npm package but not anywhere else, so build it. + 'deprecated', ]; var normal = { diff --git a/npm-react/react.js b/npm-react/react.js index 08da025716..a241f97d93 100644 --- a/npm-react/react.js +++ b/npm-react/react.js @@ -1 +1,53 @@ -module.exports = require('./lib/React'); +'use strict'; + +var React = require('./lib/React'); + +var assign = require('./lib/Object.assign'); +var deprecated = require('./lib/deprecated'); + +// We want to warn once when any of these methods are used. +if (process.env.NODE_ENV !== 'production') { + var deprecations = { + // ReactDOMClient + findDOMNode: deprecated( + 'findDOMNode', + 'react-dom', + React, + React.findDOMNode + ), + render: deprecated( + 'render', + 'react-dom', + React, + React.render + ), + unmountComponentAtNode: deprecated( + 'unmountComponentAtNode', + 'react-dom', + React, + React.unmountComponentAtNode + ), + // ReactDOMServer + renderToString: deprecated( + 'renderToString', + 'react-dom/server', + React, + React.renderToString + ), + renderToStaticMarkup: deprecated( + 'renderToStaticMarkup', + 'react-dom/server', + React, + React.renderToStaticMarkup + ), + }; + // Export a wrapped object. We'll use assign and take advantage of the fact + // that this will override the original methods in React. + module.exports = assign( + {}, + React, + deprecations + ); +} else { + module.exports = React; +} diff --git a/src/shared/utils/deprecated.js b/src/shared/utils/deprecated.js index b6a4c5a066..8b6e567518 100644 --- a/src/shared/utils/deprecated.js +++ b/src/shared/utils/deprecated.js @@ -18,30 +18,27 @@ var warning = require('warning'); * This will log a single deprecation notice per function and forward the call * on to the new API. * - * @param {string} namespace The namespace of the call, eg 'React' - * @param {string} oldName The old function name, eg 'renderComponent' - * @param {string} newName The new function name, eg 'render' + * @param {string} fnName The name of the function + * @param {string} newModule The module that fn will exist in * @param {*} ctx The context this forwarded call should run in * @param {function} fn The function to forward on to - * @return {*} Will be the value as returned from `fn` + * @return {function} The function that will warn once and then call fn */ -function deprecated(namespace, oldName, newName, ctx, fn) { +function deprecated(fnName, newModule, ctx, fn) { var warned = false; if (__DEV__) { var newFn = function() { warning( warned, - '%s.%s will be deprecated in a future version. ' + - 'Use %s.%s instead.', - namespace, - oldName, - namespace, - newName + '`require("react").%s` is deprecated. Please use `require("%s").%s` ' + + 'instead.', + fnName, + newModule, + fnName ); warned = true; return fn.apply(ctx, arguments); }; - newFn.displayName = `${namespace}_${oldName}`; // We need to make sure all properties of the original fn are copied over. // In particular, this is needed to support PropTypes return assign(newFn, fn);