Skip to content

Commit 5d2a328

Browse files
petegleesonNoviny
authored andcommitted
Makes check for defaultProps more specific (#37)
* adds failing test for this expression example * does not try and convert all assignment expressions
1 parent d296a08 commit 5d2a328

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

__snapshots__/test.js.snap

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,36 @@ Object {
821821
}
822822
`;
823823

824+
exports[`flow class with this expression 1`] = `
825+
Object {
826+
"component": Object {
827+
"kind": "generic",
828+
"value": Object {
829+
"kind": "object",
830+
"members": Array [
831+
Object {
832+
"default": Object {
833+
"kind": "number",
834+
"value": 1,
835+
},
836+
"key": Object {
837+
"kind": "id",
838+
"name": "ok",
839+
},
840+
"kind": "property",
841+
"optional": false,
842+
"value": Object {
843+
"kind": "number",
844+
},
845+
},
846+
],
847+
"referenceIdName": "Props",
848+
},
849+
},
850+
"kind": "program",
851+
}
852+
`;
853+
824854
exports[`flow default class export 1`] = `
825855
Object {
826856
"component": Object {

index.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,8 @@ function convertReactComponentFunction(path, context) {
213213
path.hub.file.path.traverse({
214214
// look for MyComponent.defaultProps = ...
215215
AssignmentExpression(assignmentPath) {
216-
const component = convert(assignmentPath.get('left'), {
217-
...context,
218-
mode: 'value'
219-
});
220-
if (component.object.referenceIdName === name) {
216+
const left = assignmentPath.get('left.object');
217+
if (left.isIdentifier() && left.node.name === name) {
221218
let initialConversion = convert(assignmentPath.get('right'), {
222219
...context,
223220
mode: 'value'

test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,6 +1300,36 @@ const TESTS = [
13001300
}
13011301
}
13021302
`
1303+
},
1304+
{
1305+
name: 'flow class with this expression',
1306+
typeSystem: 'flow',
1307+
code: `
1308+
type Props = {
1309+
ok: number
1310+
}
1311+
1312+
class FieldInner extends React.Component<Props> {
1313+
unregisterField = () => {};
1314+
1315+
componentDidMount() {
1316+
this.unregisterField = this.register();
1317+
}
1318+
1319+
componentWillUnmount() {
1320+
this.unregisterField();
1321+
}
1322+
}
1323+
1324+
const Field = (props: Props) => <FieldInner {...props} />;
1325+
1326+
Field.defaultProps = {
1327+
ok: 1
1328+
};
1329+
1330+
export default Field;
1331+
1332+
`
13031333
}
13041334
];
13051335

0 commit comments

Comments
 (0)