-
-
Notifications
You must be signed in to change notification settings - Fork 312
Fix intersection and union flow types #118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,6 @@ | |
* | ||
*/ | ||
|
||
import getPropertyName from './getPropertyName'; | ||
import getTypeAnnotation from '../utils/getTypeAnnotation'; | ||
import getMemberValuePath from '../utils/getMemberValuePath'; | ||
import isReactComponentClass from '../utils/isReactComponentClass'; | ||
|
@@ -51,7 +50,48 @@ export default (path: NodePath): ?NodePath => { | |
if (typePath && types.GenericTypeAnnotation.check(typePath.node)) { | ||
typePath = resolveToValue(typePath.get('id')); | ||
if ( | ||
!typePath || | ||
!typePath || | ||
types.Identifier.check(typePath.node) || | ||
isUnreachableFlowType(typePath) | ||
) { | ||
return; | ||
} | ||
|
||
typePath = typePath.get('right'); | ||
} | ||
|
||
return typePath; | ||
} | ||
|
||
export function applyToFlowTypeProperties( | ||
path: NodePath, | ||
callback: (propertyPath: NodePath) => void | ||
) { | ||
if (path.node.properties) { | ||
path.get('properties').each( | ||
propertyPath => callback(propertyPath) | ||
); | ||
} else if (path.node.type === 'IntersectionTypeAnnotation') { | ||
path.get('types').each( | ||
typesPath => applyToFlowTypeProperties(typesPath, callback) | ||
); | ||
} else if (path.node.type !== 'UnionTypeAnnotation') { | ||
// The react-docgen output format does not currently allow | ||
// for the expression of union types | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would we have to do to make this work? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This depends on how exactly we want to go for this. Quoting the flow docs:
So intersection was quite straight forward, just merge all objects and the result is the object with all possible props. With union this is more complex. In this example, one can see that when using union for prop-types it is one of the objects to be "unioned" (with additional properties allowed). So in the simple case of
The resulting docs would need to be: "prop a is required and needs to be a number, or it is a string but then also b is required and needs to be a string" This might make sense in some react components, but I'm not sure yet how we would document this cases. We maybe could only add the raw value. P.S.: I might be mistaken here, but I think it should be correct. Never used union and intersect in these cases myself. :) |
||
let typePath = resolveGenericTypeAnnotation(path); | ||
if (typePath) { | ||
applyToFlowTypeProperties(typePath, callback); | ||
} | ||
} | ||
} | ||
|
||
function resolveGenericTypeAnnotation(path: NodePath): ?NodePath { | ||
// If the node doesn't have types or properties, try to get the type. | ||
let typePath: ?NodePath; | ||
if (path && types.GenericTypeAnnotation.check(path.node)) { | ||
typePath = resolveToValue(path.get('id')); | ||
if ( | ||
!typePath || | ||
types.Identifier.check(typePath.node) || | ||
isUnreachableFlowType(typePath) | ||
) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this not throw as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch. The test wasn't executed because of wrong nesting. Fixed it.