@samitouri / QOS-React / commits / fc1cfb6225

Make React.__spread warn

Paul O’Shannessy committed Apr 7, 2016 at 16:24 UTC fc1cfb6225f5c3af6843515363db63ed3dcbb824
2 files changed +43 -13
scripts/babel/transform-object-assign-require.js
+23 -11
@@ -12,6 +12,20 @@
12 module.exports = function autoImporter(babel) {
13 const t = babel.types;
14
15 + function getAssignIdent(path, file, state) {
16 + if (!state.id) {
17 + state.id = path.scope.generateUidIdentifier('assign');
18 + path.scope.getProgramParent().push({
19 + id: state.id,
20 + init: t.callExpression(
21 + t.identifier('require'),
22 + [t.stringLiteral('object-assign')]
23 + ),
24 + });
25 + }
26 + return state.id;
27 + }
28 +
29 return {
30 pre: function() {
31 // map from module to generated identifier
@@ -22,17 +36,15 @@ module.exports = function autoImporter(babel) {
36 CallExpression: function(path, file) {
37 if (path.get('callee').matchesPattern('Object.assign')) {
38 // generate identifier and require if it hasn't been already
25 - if (!this.id) {
26 - this.id = path.scope.generateUidIdentifier('assign');
27 - path.scope.getProgramParent().push({
28 - id: this.id,
29 - init: t.callExpression(
30 - t.identifier('require'),
31 - [t.stringLiteral('object-assign')]
32 - ),
33 - });
34 - }
35 - path.node.callee = this.id;
39 + var id = getAssignIdent(path, file, this);
40 + path.node.callee = id;
41 + }
42 + },
43 +
44 + MemberExpression: function(path, file) {
45 + if (path.matchesPattern('Object.assign')) {
46 + var id = getAssignIdent(path, file, this);
47 + path.replaceWith(id);
48 }
49 },
50 },
src/isomorphic/React.js
+20 -2
@@ -21,6 +21,7 @@ var ReactPropTypes = require('ReactPropTypes');
21 var ReactVersion = require('ReactVersion');
22
23 var onlyChild = require('onlyChild');
24 +var warning = require('warning');
25
26 var createElement = ReactElement.createElement;
27 var createFactory = ReactElement.createFactory;
@@ -32,6 +33,23 @@ if (__DEV__) {
33 cloneElement = ReactElementValidator.cloneElement;
34 }
35
36 +var __spread = Object.assign;
37 +
38 +if (__DEV__) {
39 + var warned = false;
40 + __spread = function() {
41 + warning(
42 + warned,
43 + 'React.__spread is deprecated and should not be used. Use ' +
44 + 'Object.assign directly or another helper function with similar ' +
45 + 'semantics. You may be seeing this warning due to your compiler. ' +
46 + 'See https://fb.me/react-spread-deprecation for more details.'
47 + );
48 + warned = true;
49 + return Object.assign.apply(null, arguments);
50 + };
51 +}
52 +
53 var React = {
54
55 // Modern
@@ -66,8 +84,8 @@ var React = {
84
85 version: ReactVersion,
86
69 - // Hook for JSX spread, don't use this for anything else.
70 - __spread: Object.assign,
87 + // Deprecated hook for JSX spread, don't use this for anything.
88 + __spread: __spread,
89 };
90
91 module.exports = React;