Skip to content

Commit f00fbb0

Browse files
author
Keyan Zhang
committed
rename this.context to context in constructor
1 parent 82d4a52 commit f00fbb0

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class App2 extends React.Component {
5656
constructor(props, context) {
5757
super(props, context);
5858
const state = {
59-
whatever: this.context.whatever, // needs context
59+
whatever: context.whatever, // needs context
6060
};
6161
this.state = state;
6262
}

transforms/class.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -411,18 +411,30 @@ module.exports = (file, api, options) => {
411411
fn.value
412412
), fn);
413413

414-
const updatePropsAccess = getInitialState =>
415-
j(getInitialState)
416-
.find(j.MemberExpression, {
417-
object: {
418-
type: 'ThisExpression',
419-
},
420-
property: {
421-
type: 'Identifier',
422-
name: 'props',
423-
},
424-
})
425-
.forEach(path => j(path).replaceWith(j.identifier('props')));
414+
const updatePropsAndContextAccess = getInitialState => {
415+
const collection = j(getInitialState);
416+
417+
collection.find(j.MemberExpression, {
418+
object: {
419+
type: 'ThisExpression',
420+
},
421+
property: {
422+
type: 'Identifier',
423+
name: 'props',
424+
},
425+
}).forEach(path => j(path).replaceWith(j.identifier('props')));
426+
427+
collection.find(j.MemberExpression, {
428+
object: {
429+
type: 'ThisExpression',
430+
},
431+
property: {
432+
type: 'Identifier',
433+
name: 'context',
434+
},
435+
}).forEach(path => j(path).replaceWith(j.identifier('context')));
436+
};
437+
426438

427439
const inlineGetInitialState = getInitialState => {
428440
const functionExpressionCollection = j(getInitialState.value);
@@ -520,7 +532,7 @@ module.exports = (file, api, options) => {
520532
hasContextAccess = true;
521533
}
522534

523-
updatePropsAccess(getInitialState);
535+
updatePropsAndContextAccess(getInitialState);
524536
const constructorArgs = createConstructorArgs(hasContextAccess);
525537

526538
return [

0 commit comments

Comments
 (0)