-
Notifications
You must be signed in to change notification settings - Fork 257
support default props via defaultProps or JSDoc #53
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
Conversation
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.
some comments
/** | ||
* Extracts a full JsDoc comment from a symbol, even | ||
* thought TypeScript has broken down the JsDoc comment into plain | ||
* though TypeScript has broken down the JsDoc comment into plain |
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.
found a typo
} | ||
|
||
findDocComment(symbol: ts.Symbol) { | ||
findDocComment(symbol: ts.Symbol): JSDoc { |
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.
i changed the return value of this from a string to this JSDoc
interface I created above. allows us to pass more metadata through (in this case, the tags)
const currentValue = tagMap[tag.name]; | ||
tagMap[tag.name] = currentValue ? currentValue + '\n' + trimmedText : trimmedText; | ||
|
||
if (tag.name !== 'default') { tagComments.push(formatTag(tag)); } |
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.
i figured we can avoid adding the @default whatever
to the description, as it's already represented by the defaultValue
case ts.SyntaxKind.NullKeyword: | ||
return 'null'; | ||
case ts.SyntaxKind.Identifier: | ||
// can potentially find other identifiers in the source and map those in the future |
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.
Identifier
seems to be used for a variable or undefined
. in the future, can probably grab literal values from the source doc but, while nice, seemed outside of the scope of this PR
return (initializer as ts.Identifier).text === 'undefined' ? 'undefined' : null; | ||
case ts.SyntaxKind.ObjectLiteralExpression: | ||
// return the source text for an object literal | ||
return (initializer as ts.ObjectLiteralExpression).getText(); |
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.
for an object literal, I'm just grabbing the raw text
It's perfect. It's really nice feature and thanks to you will be included in next release :) |
I am just thinking what it's the correct version for that new release. |
Just went to have a look at adding this feature and discovered this. Thanks a lot @brettjurgens. 😄 @pvasek I'd release under Any idea when you will release? |
@JakeSidSmith I agree, I will create it right now. |
Version v1.1.0 including this fix has just been created. |
Awesome! Thanks @pvasek! |
Does this work still? |
It does seem to work, but only under very specific circumstances. For instance, if you had this:
The parser would say that MyClass and OtherName both have the same props, but MyClass has default values and OtherName doesn't. And if you have |
let defaultValue = null; | ||
|
||
if (defaultProps[propName] !== undefined) { | ||
defaultValue = { value: defaultProps[propName] }; |
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.
I know it's an old code but I'm wondering why is the defaultValue
was made an object with value
key and not the value itself?
fixes #37
i've extended the parser to get default values from
Component.defaultProps
or JSDoc (@default
)i've also added two tests for class & stateless components, let me know if there are any other cases I should get.
here's a screenshot from one of my projects with a, rather exhaustive and contrived, default value list:
