Skip to content

Conversation

brettjurgens
Copy link
Contributor

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:
screen shot 2017-11-08 at 9 17 26 pm

screen shot 2017-11-08 at 9 16 16 pm

Copy link
Contributor Author

@brettjurgens brettjurgens left a 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
Copy link
Contributor Author

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 {
Copy link
Contributor Author

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)); }
Copy link
Contributor Author

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
Copy link
Contributor Author

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();
Copy link
Contributor Author

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

@pvasek pvasek merged commit 992cada into styleguidist:master Nov 10, 2017
@pvasek
Copy link
Collaborator

pvasek commented Nov 10, 2017

It's perfect. It's really nice feature and thanks to you will be included in next release :)
Thanks a lot!

@pvasek
Copy link
Collaborator

pvasek commented Nov 10, 2017

I am just thinking what it's the correct version for that new release.
The current version is 1.0.2. There is no breaking change from user perspective but maybe it would make sense to move it to 1.1.0. What do you think about it? Should we go with 1.0.3 or 1.1.0?

@JakeSidSmith
Copy link
Contributor

Just went to have a look at adding this feature and discovered this. Thanks a lot @brettjurgens. 😄

@pvasek I'd release under 1.1.0 as it is a new feature: http://semver.org/

Any idea when you will release?

@pvasek
Copy link
Collaborator

pvasek commented Nov 10, 2017

@JakeSidSmith I agree, I will create it right now.

@pvasek
Copy link
Collaborator

pvasek commented Nov 10, 2017

Version v1.1.0 including this fix has just been created.

@brettjurgens
Copy link
Contributor Author

Awesome! Thanks @pvasek!

@brettjurgens brettjurgens deleted the add_default_props branch November 10, 2017 21:14
@hipstersmoothie
Copy link
Contributor

Does this work still?

@pjweisberg
Copy link

pjweisberg commented Jun 12, 2019

It does seem to work, but only under very specific circumstances. For instance, if you had this:

export default class MyClass<MyClassProps> {
   ...etc....
}
export const OtherName = MyClass;

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 export default withStyles(styles)(MyClass) the parser doesn't understand that at all. (It's not really reasonable to expect that last one to work, since it's only parsing one file and thus can't know what withStyles is. It's just too bad because most of my exports look like that.)

let defaultValue = null;

if (defaultProps[propName] !== undefined) {
defaultValue = { value: defaultProps[propName] };
Copy link
Member

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Property defaults value
6 participants