This repository was archived by the owner on Mar 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
docs(Props): improve table with props #1634
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
589d5f8
chore: improve docgen
levithomason c34a800
remove changes in type
layershifter 8fc9589
fix types
layershifter 4eb4c6c
add changelog entry
layershifter e15ad2f
fix review comment
layershifter 0bd3449
fix review comment: improve code
layershifter 83c1577
fix review comment: fix handling of ShorthandCollection
layershifter 9956115
fix handling of component links
layershifter 19ba052
Merge branches 'chore/prop-generator' and 'master' of https://github.…
layershifter 6f0f129
Merge branch 'master' into chore/prop-generator
layershifter 6ae5737
Merge branches 'chore/prop-generator' and 'master' of https://github.…
layershifter e5964a2
update changelog
layershifter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,71 @@ | ||
| import * as Babel from '@babel/core' | ||
| import { NodePath } from '@babel/traverse' | ||
| import * as t from '@babel/types' | ||
|
|
||
| import { ComponentInfo } from 'docs/src/types' | ||
|
|
||
| type ShorthandInfo = Required< | ||
| Pick<ComponentInfo, 'implementsCreateShorthand' | 'mappedShorthandProp'> | ||
| > | ||
|
|
||
| /** | ||
| * Checks that an expression matches signature: | ||
| * [componentName].create = createShorthandFactory([config]) | ||
| */ | ||
| const isShorthandExpression = ( | ||
| componentName: string, | ||
| path: NodePath<t.AssignmentExpression>, | ||
| ): boolean => { | ||
| const left = path.get('left') | ||
| const right = path.get('right') | ||
|
|
||
| if (!left.isMemberExpression() || !right.isCallExpression()) { | ||
| return false | ||
| } | ||
|
|
||
| const object = left.get('object') | ||
| const property = left.get('property') as NodePath<t.Identifier> | ||
| const callee = right.get('callee') | ||
|
|
||
| return ( | ||
| object.isIdentifier({ name: componentName }) && | ||
| property.isIdentifier({ name: 'create' }) && | ||
| callee.isIdentifier({ name: 'createShorthandFactory' }) | ||
bmdalex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| } | ||
|
|
||
| const getShorthandInfo = (componentFile: t.File, componentName: string): ShorthandInfo => { | ||
| let implementsCreateShorthand = false | ||
| let mappedShorthandProp = undefined | ||
|
|
||
| Babel.traverse(componentFile, { | ||
| AssignmentExpression: path => { | ||
| if (isShorthandExpression(componentName, path)) { | ||
| implementsCreateShorthand = true | ||
|
|
||
| const config = path.get('right.arguments.0') as NodePath<t.ObjectExpression> | ||
| config.assertObjectExpression() | ||
|
|
||
| const mappedProperty = config.node.properties.find((property: t.ObjectProperty) => { | ||
| return t.isIdentifier(property.key, { name: 'mappedProp' }) | ||
| }) as t.ObjectProperty | null | ||
|
|
||
| if (mappedProperty) { | ||
| // @ts-ignore | ||
| t.assertStringLiteral(mappedProperty.value) | ||
| mappedShorthandProp = (mappedProperty.value as t.StringLiteral).value | ||
| } else { | ||
| // `mappedProp` is optional in `createShorthandFactory()` | ||
| mappedShorthandProp = 'children' | ||
| } | ||
| } | ||
| }, | ||
| }) | ||
|
|
||
| return { | ||
| implementsCreateShorthand, | ||
| mappedShorthandProp, | ||
| } | ||
| } | ||
|
|
||
| export default getShorthandInfo | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,5 +1,45 @@ | ||
| import _ from 'lodash' | ||
| import * as React from 'react' | ||
|
|
||
| // export default propDef => _.get(propDef, 'defaultValue.value', undefined) | ||
| export default (propDef, typeName) => | ||
| _.get(propDef, 'defaultValue.value', typeName === 'boolean' ? 'false' : 'undefined') | ||
| import { ComponentPropType } from 'docs/src/types' | ||
| import { PropItem } from './docgen' | ||
|
|
||
| const parseDefaultValue = ( | ||
| Component: React.ComponentType, | ||
| propDef: PropItem, | ||
| types: ComponentPropType[], | ||
| ) => { | ||
| if (Component.defaultProps && _.has(Component.defaultProps, propDef.name)) { | ||
| const defaultValue = Component.defaultProps[propDef.name] | ||
|
|
||
| if (_.isFunction(defaultValue)) { | ||
| return defaultValue.name | ||
| } | ||
|
|
||
| if (_.isNumber(defaultValue) || _.isString(defaultValue) || _.isBoolean(defaultValue)) { | ||
| return defaultValue | ||
| } | ||
|
|
||
| if (_.isPlainObject(defaultValue)) { | ||
| return defaultValue | ||
| } | ||
|
|
||
| if (_.isNull(defaultValue)) { | ||
| return null | ||
| } | ||
|
|
||
| throw new Error(`Can't parse a value in "${Component.name}.defaultProps.${propDef.name}"`) | ||
| } | ||
|
|
||
| if (propDef.name === 'as') { | ||
| return 'div' | ||
| } | ||
|
|
||
| if (types.length === 1 && types[0].name === 'boolean') { | ||
| return false | ||
| } | ||
|
|
||
| return undefined | ||
| } | ||
|
|
||
| export default parseDefaultValue |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,11 +1,13 @@ | ||
| import * as doctrine from 'doctrine' | ||
|
|
||
| export default docblock => { | ||
| const parseDocblock = (docblock: string) => { | ||
| const { description = '', tags = [], ...rest } = doctrine.parse(docblock || '', { unwrap: true }) | ||
|
|
||
| return { | ||
| ...rest, | ||
| description, | ||
| tags, | ||
| description: description.split('\n'), | ||
| } | ||
| } | ||
|
|
||
| export default parseDocblock |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.