Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle computed properties correctly and do not fail generation (#340)
* Handle computed properties correctly and do not fail generation * Resolve identifiers and correctly use string/number literals * Fix lint
- Loading branch information
Showing
25 changed files
with
824 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
const Button = () => ( | ||
<div></div> | ||
); | ||
|
||
Button.propTypes = { | ||
/** This is a test */ | ||
[children]: PropTypes.string.isRequired, | ||
}; | ||
|
||
Button.defaultProps = { | ||
[children]: "default", | ||
}; | ||
|
||
export default Button; |
25 changes: 25 additions & 0 deletions
25
src/handlers/__tests__/__snapshots__/componentMethodsHandler-test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`componentMethodsHandler should handle and ignore computed methods 1`] = ` | ||
Array [ | ||
Object { | ||
"docblock": "The foo method", | ||
"modifiers": Array [], | ||
"name": "@computed#foo", | ||
"params": Array [ | ||
Object { | ||
"name": "bar", | ||
"optional": undefined, | ||
"type": Object { | ||
"name": "number", | ||
}, | ||
}, | ||
], | ||
"returns": Object { | ||
"type": Object { | ||
"name": "number", | ||
}, | ||
}, | ||
}, | ||
] | ||
`; |
238 changes: 238 additions & 0 deletions
238
src/handlers/__tests__/__snapshots__/defaultPropsHandler-test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,238 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`defaultPropsHandler ClassDeclaration with static defaultProps should find prop default values that are imported variables 1`] = ` | ||
Object { | ||
"foo": Object { | ||
"defaultValue": Object { | ||
"computed": true, | ||
"value": "ImportedComponent", | ||
}, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`defaultPropsHandler ClassDeclaration with static defaultProps should find prop default values that are literals 1`] = ` | ||
Object { | ||
"abc": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "{xyz: abc.def, 123: 42}", | ||
}, | ||
}, | ||
"bar": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "42", | ||
}, | ||
}, | ||
"baz": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "[\\"foo\\", \\"bar\\"]", | ||
}, | ||
}, | ||
"foo": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "\\"bar\\"", | ||
}, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`defaultPropsHandler ClassExpression with static defaultProps should find prop default values that are literals 1`] = ` | ||
Object { | ||
"abc": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "{xyz: abc.def, 123: 42}", | ||
}, | ||
}, | ||
"bar": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "42", | ||
}, | ||
}, | ||
"baz": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "[\\"foo\\", \\"bar\\"]", | ||
}, | ||
}, | ||
"foo": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "\\"bar\\"", | ||
}, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`defaultPropsHandler Functional components with default params should find default props that are literals 1`] = ` | ||
Object { | ||
"abc": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "{xyz: abc.def, 123: 42}", | ||
}, | ||
}, | ||
"bar": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "42", | ||
}, | ||
}, | ||
"baz": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "[\\"foo\\", \\"bar\\"]", | ||
}, | ||
}, | ||
"foo": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "\\"bar\\"", | ||
}, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`defaultPropsHandler Functional components with default params should find prop default values that are imported variables 1`] = ` | ||
Object { | ||
"foo": Object { | ||
"defaultValue": Object { | ||
"computed": true, | ||
"value": "ImportedComponent", | ||
}, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`defaultPropsHandler Functional components with default params should override with defaultProps if available 1`] = ` | ||
Object { | ||
"abc": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "{xyz: abc.def, 123: 42}", | ||
}, | ||
}, | ||
"bar": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "42", | ||
}, | ||
}, | ||
"baz": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "[\\"foo\\", \\"bar\\"]", | ||
}, | ||
}, | ||
"foo": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "\\"bar\\"", | ||
}, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`defaultPropsHandler Functional components with default params should work with aliases 1`] = ` | ||
Object { | ||
"abc": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "{xyz: abc.def, 123: 42}", | ||
}, | ||
}, | ||
"bar": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "42", | ||
}, | ||
}, | ||
"baz": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "[\\"foo\\", \\"bar\\"]", | ||
}, | ||
}, | ||
"foo": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "\\"bar\\"", | ||
}, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`defaultPropsHandler Functional components with default params should work with no defaults 1`] = `Object {}`; | ||
|
||
exports[`defaultPropsHandler ObjectExpression handles computed properties 1`] = ` | ||
Object { | ||
"@computed#bar": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "42", | ||
}, | ||
}, | ||
"foo": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "\\"bar\\"", | ||
}, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`defaultPropsHandler ObjectExpression ignores complex computed properties 1`] = ` | ||
Object { | ||
"foo": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "\\"bar\\"", | ||
}, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`defaultPropsHandler ObjectExpression should find prop default values that are literals 1`] = ` | ||
Object { | ||
"abc": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "{xyz: abc.def, 123: 42}", | ||
}, | ||
}, | ||
"bar": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "42", | ||
}, | ||
}, | ||
"baz": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "[\\"foo\\", \\"bar\\"]", | ||
}, | ||
}, | ||
"foo": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "\\"bar\\"", | ||
}, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`defaultPropsHandler should only consider Property nodes, not e.g. spread properties 1`] = ` | ||
Object { | ||
"bar": Object { | ||
"defaultValue": Object { | ||
"computed": false, | ||
"value": "42", | ||
}, | ||
}, | ||
} | ||
`; |
50 changes: 50 additions & 0 deletions
50
src/handlers/__tests__/__snapshots__/flowTypeHandler-test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.