Skip to content

Commit e00677f

Browse files
author
Keyan Zhang
committed
bail out if user uses getInitialState or getDefaultProps elsewhere
1 parent b8bb77a commit e00677f

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

transforms/__testfixtures__/class-initial-state.input.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,33 @@ var Loader = React.createClass({
128128
return null;
129129
},
130130
});
131+
132+
var helper = () => {};
133+
134+
var PassGetInitialState = React.createClass({
135+
getInitialState() {
136+
return this.lol();
137+
},
138+
139+
helper1: function() {
140+
helper(this.getInitialState);
141+
},
142+
143+
render() {
144+
return null;
145+
},
146+
});
147+
148+
var UseGetInitialState = React.createClass({
149+
getInitialState() {
150+
return this.lol();
151+
},
152+
153+
helper2() {
154+
this.setState(this.getInitialState());
155+
},
156+
157+
render() {
158+
return null;
159+
},
160+
});

transforms/__testfixtures__/class-initial-state.output.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,33 @@ class Loader extends React.Component {
140140
return null;
141141
}
142142
}
143+
144+
var helper = () => {};
145+
146+
var PassGetInitialState = React.createClass({
147+
getInitialState() {
148+
return this.lol();
149+
},
150+
151+
helper1: function() {
152+
helper(this.getInitialState);
153+
},
154+
155+
render() {
156+
return null;
157+
},
158+
});
159+
160+
var UseGetInitialState = React.createClass({
161+
getInitialState() {
162+
return this.lol();
163+
},
164+
165+
helper2() {
166+
this.setState(this.getInitialState());
167+
},
168+
169+
render() {
170+
return null;
171+
},
172+
});

transforms/class.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ module.exports = (file, api, options) => {
8484
0
8585
) > 0;
8686

87-
const callsDeprecatedAPIs = classPath => {
87+
const hasNoCallsToDeprecatedAPIs = classPath => {
8888
if (checkDeprecatedAPICalls(classPath)) {
8989
console.log(
9090
file.path + ': `' + ReactUtils.getComponentName(classPath) + '` ' +
@@ -97,6 +97,23 @@ module.exports = (file, api, options) => {
9797
return true;
9898
};
9999

100+
const hasNoCallsToAPIsThatWillBeRemoved = classPath => {
101+
const hasInvalidCalls = (
102+
j(classPath).find(j.Identifier, {name: DEFAULT_PROPS_FIELD}).size() > 1 ||
103+
j(classPath).find(j.Identifier, {name: GET_INITIAL_STATE_FIELD}).size() > 1
104+
);
105+
if (hasInvalidCalls) {
106+
console.log(
107+
file.path + ': `' + ReactUtils.getComponentName(classPath) + '` ' +
108+
'was skipped because of API calls that will be removed. Remove calls to `' +
109+
DEFAULT_PROPS_FIELD + '` and/or `' + GET_INITIAL_STATE_FIELD +
110+
'` in your React component and re-run this script.'
111+
);
112+
return false;
113+
}
114+
return true;
115+
};
116+
100117
const canConvertToClass = classPath => {
101118
const specPath = ReactUtils.getReactCreateClassSpec(classPath);
102119
const invalidProperties = specPath.properties.filter(prop => (
@@ -828,7 +845,8 @@ module.exports = (file, api, options) => {
828845
const apply = (path, type) =>
829846
path
830847
.filter(mixinsFilter)
831-
.filter(callsDeprecatedAPIs)
848+
.filter(hasNoCallsToDeprecatedAPIs)
849+
.filter(hasNoCallsToAPIsThatWillBeRemoved)
832850
.filter(canConvertToClass)
833851
.forEach(classPath => updateToClass(classPath, type));
834852

0 commit comments

Comments
 (0)