Skip to content

Commit 7072559

Browse files
committed
Make React.__spread warn
1 parent f02d87b commit 7072559

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

scripts/babel/transform-object-assign-require.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@
1212
module.exports = function autoImporter(babel) {
1313
const t = babel.types;
1414

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+
1529
return {
1630
pre: function() {
1731
// map from module to generated identifier
@@ -22,17 +36,15 @@ module.exports = function autoImporter(babel) {
2236
CallExpression: function(path, file) {
2337
if (path.get('callee').matchesPattern('Object.assign')) {
2438
// 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);
3648
}
3749
},
3850
},

src/isomorphic/React.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var ReactPropTypes = require('ReactPropTypes');
2121
var ReactVersion = require('ReactVersion');
2222

2323
var onlyChild = require('onlyChild');
24+
var warning = require('warning');
2425

2526
var createElement = ReactElement.createElement;
2627
var createFactory = ReactElement.createFactory;
@@ -32,6 +33,23 @@ if (__DEV__) {
3233
cloneElement = ReactElementValidator.cloneElement;
3334
}
3435

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+
3553
var React = {
3654

3755
// Modern
@@ -67,7 +85,7 @@ var React = {
6785
version: ReactVersion,
6886

6987
// Hook for JSX spread, don't use this for anything else.
70-
__spread: Object.assign,
88+
__spread: __spread,
7189
};
7290

7391
module.exports = React;

0 commit comments

Comments
 (0)