Skip to content

Commit 93e5931

Browse files
author
Keyan Zhang
committed
added a test for ES6 export
1 parent 00790d4 commit 93e5931

File tree

3 files changed

+11
-30
lines changed

3 files changed

+11
-30
lines changed

README.md

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,7 @@ The constructor logic is as follows:
156156
* Pulls out all statics defined on `statics` plus the few special cased
157157
statics like `propTypes`, `childContextTypes`, `contextTypes`, and
158158
`displayName` and transforms them to `static` properties at the very top.
159-
like `static displayName = 'Counter';`
160-
* TODO do we bind stuff in the `static` object?
161-
* Takes `getDefaultProps` and inlines it as a static `defaultProps`.
159+
* Takes `getDefaultProps` and inlines it as `static defaultProps = ...;`.
162160
If `getDefaultProps` is defined as a function with a single statement that
163161
returns an object, it optimizes and transforms
164162
`getDefaultProps() { return {foo: 'bar'}; }` into
@@ -168,34 +166,20 @@ The constructor logic is as follows:
168166
will be executed only a single time per app-lifetime. In practice this
169167
hasn't caused any issues – `getDefaultProps` should not contain any
170168
side-effects.
169+
* If there exists references to `this.props` in `getInitialState` then it creates
170+
a constructor and converts `getInitialState` to an assignment to `this.state`;
171+
Otherwise it lifts `getInitialState` to a property initializer (`state = ...;`).
171172
* Transforms class methods to arrow functions as class property initializers
172173
(i.e., to bind them) if methods are referenced without being
173174
called directly. It checks for `this.foo` but also traces variable
174175
assignments like `var self = this; self.foo`. It does not bind functions
175176
from the React API (lifecycle methods) and ignores functions that are being
176177
called directly (unless it is both called directly and passed around to
177-
somewhere else)
178-
* TODO how do we handle `getInitialState`?
179-
* If we reference `this.props` in `getInitialState` then it
180-
has to be in the constructor
181-
* Otherwise it's simple and just make it a property initializer
182-
* TODO [???] When `--no-super-class` is passed it only optionally extends
178+
somewhere else).
179+
* TODO When `--no-super-class` is passed it only optionally extends
183180
`React.Component` when `setState` or `forceUpdate` are used within the
184181
class.
185182

186-
The constructor logic is as follows:
187-
188-
* Call `super(props, context)` if the base class needs to be extended.
189-
* Bind all functions that are passed around,
190-
like `this.foo = this.foo.bind(this)`
191-
* Inline `getInitialState` (and remove `getInitialState` from the spec). It
192-
also updates access of `this.props.foo` to `props.foo` and adds `props` as
193-
argument to the constructor. This is necessary in the case when the base
194-
class does not need to be extended where `this.props` will only be set by
195-
React after the constructor has been run.
196-
* Changes `return StateObject` from `getInitialState` to assign `this.state`
197-
directly.
198-
199183
### Recast Options
200184

201185
Options to [recast](https://github.com/benjamn/recast)'s printer can be provided

transforms/__testfixtures__/export-default-property-initializer.output.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@
55
import React from 'React';
66

77
export default class extends React.Component {
8-
constructor(props, context) {
9-
super(props, context);
10-
11-
this.state = {
12-
foo: 'bar',
13-
};
14-
}
15-
168
static propTypes = {
179
foo: React.PropTypes.string,
1810
};
1911

12+
state = {
13+
foo: 'bar',
14+
};
15+
2016
render() {
2117
return <div />;
2218
}

transforms/__tests__/property-initializer-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
const defineTest = require('jscodeshift/dist/testUtils').defineTest;
1414
defineTest(__dirname, 'property-initializer');
1515
defineTest(__dirname, 'property-initializer', null, 'property-initializer-2');
16+
defineTest(__dirname, 'property-initializer', null, 'export-default-property-initializer');

0 commit comments

Comments
 (0)