react-docgen icon indicating copy to clipboard operation
react-docgen copied to clipboard

Infer displayName from function/class/variable name

Open iamdustan opened this issue 10 years ago • 15 comments

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.

iamdustan avatar Oct 14 '15 19:10 iamdustan

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

fkling avatar Oct 14 '15 20:10 fkling

wait. does the displayName already appear in there?

iamdustan avatar Oct 14 '15 20:10 iamdustan

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: '' } } }

iamdustan avatar Oct 14 '15 20:10 iamdustan

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

fkling avatar Oct 14 '15 20:10 fkling

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

fkling avatar Oct 30 '15 17:10 fkling

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;

iamdustan avatar Oct 30 '15 17:10 iamdustan

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?

fkling avatar Oct 30 '15 18:10 fkling

:+1:

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

iamdustan avatar Oct 30 '15 18:10 iamdustan

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

mik01aj avatar Oct 30 '15 18:10 mik01aj

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

fkling avatar Oct 30 '15 18:10 fkling

I vote @mik01aj for those one :)

iamdustan avatar Oct 30 '15 18:10 iamdustan

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 avatar Nov 04 '15 18:11 AlanFoster

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

AlanFoster avatar Nov 27 '15 15:11 AlanFoster

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

mik01aj avatar Nov 28 '15 06:11 mik01aj

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.

NogsMPLS avatar Mar 01 '16 19:03 NogsMPLS