reactjs / react-docgen

A CLI and library to extract information from React component files for documentation generation purposes.
https://react-docgen.dev
MIT License
3.61k stars 291 forks source link

Infer displayName from function/class/variable name #30

Open iamdustan opened 8 years ago

iamdustan commented 8 years ago

Is there a reason this wasn’t already included? Having a displayName with each description would be beneficial for tools that are using docgen without having to re-read the js file to detect the display name again.

This wasn’t that big of a deal with React.createClass or class extends React.Component since those are pretty limited call sites, but the stateless function components can get a bit gnarly in trying to infer their display names. The detection for these are already pretty solid in the #28 branch.

fkling commented 8 years ago

You mean why it isn't listed in the README? Probably just an oversight from my side.

iamdustan commented 8 years ago

wait. does the displayName already appear in there?

iamdustan commented 8 years ago

I don’t think it does... this is the result of console.logging

docgen.parse(fs.readFileSync('./my-component.js'))
{ description: '@private',
  props:
   { to: { type: [Object], required: true, description: '' },
     name: { type: [Object], required: true, description: '' } } }
fkling commented 8 years ago

We do have https://github.com/reactjs/react-docgen/blob/master/src/handlers/displayNameHandler.js though.

fkling commented 8 years ago

I can't repro this btw. The displayName shows up in the result. What does your ./my-component.js look like?

iamdustan commented 8 years ago

Hmm..

so I have stateless components not getting a displayName as well as

import React, {PropTypes, Component} from 'react';
class Tabs extends Component {
  ....
}

export default Tabs;
fkling commented 8 years ago

Oh, the displayName handler only looks for explicit displayName properties. You want it to also return the inferred name from the class or function name, right?

iamdustan commented 8 years ago

:+1:

...does babel still follow that approach for displayName handling?

mik01aj commented 8 years ago

@fkling, yes! (I know the question wasn't to me, but I'd love to have it this way :))

fkling commented 8 years ago

I don't know, will have to look into this. Seems reasonable to me though. PRs are also welcome ;)

iamdustan commented 8 years ago

I vote @mik01aj for those one :)

AlanFoster commented 8 years ago

Babel will generate the display name for you when it is not supplied explicitly.

:+1: to implementing a similar inferrance rule to react-docgen

AlanFoster commented 8 years ago

@iamdustan Would this be something that is feasible to implement?

mik01aj commented 8 years ago

Maybe you could just use this plugin: https://github.com/gajus/babel-plugin-react-display-name

NogsMPLS commented 8 years ago

I would love for react-docgen to be able to infer the name of a component based on the Component.name.

Babel only creates a displayName for the .createClass() convention, not for the es6 classes or stateless functional components.

I'll dig around react-docgen today and see if I can figure out a PR that might accomplish this.