Giter Club home page Giter Club logo

Comments (6)

dosentmatter avatar dosentmatter commented on June 15, 2024 4

I'm having the same problem. Styleguidist uses react-docgen to parse props by default.

I'm using Styleguidist and for .js files, I'm able to set the displayName multiple ways. Here is the order of precedence from the methods I've tried:

  1. Set displayName directly eg. Component.displayName = 'name1'

image

  1. Function name eg. const Component = function name2() {}

image

  1. variable name (for unamed functions such as arrow functions) eg. const name3 = () => {}

image

react-docgen-typescript doesn't use any of the three methods to get displayName. Below, I am setting displayName to 'Button' directly, the function name to AButton, and the variable name to Button, but the displayName gets set to lowercase button:
image

It's because displayName gets overriden here:

displayName: componentName,

const componentName = computeComponentName(exp, source);

exportName === '__function' ||
exportName === 'StatelessComponent'
) {
// Default export for a file: named after file
return path.basename(source.fileName, path.extname(source.fileName));

See how it gets set to the filename, path.basename(source.fileName, path.extname(source.fileName));. In my case, I was using 'StatelessComponent', but it would have been '__function' if I just used a regular fat arrow function. I don't know what 'default' is for.

At the minimum, we should be able to set displayName ourselves. It would be nice to be able to grab it from the function name and variable name. I prefer variable name because I use anonymous functions more than function declarations.

Styleguidist also has handlers option, but I don't really understand how to use it and I'm not sure if the example works with typescript or react-docgen-typescript.

from react-docgen-typescript.

argshook avatar argshook commented on June 15, 2024 1

@dosentmatter hello, thanks for thorough explanation

so the order should be like so:

  1. displayName
  2. function or class name
  3. function or class reference name

in other words, displayName is the king, if you set it manually, it must be important.
currently im not so interested in number 2 and 3, i think number 1 is most important. it should fix a lot of documentation for many.

im going to work on this today, hopefully with some results :)

from react-docgen-typescript.

pvasek avatar pvasek commented on June 15, 2024 1

This should be fixed in v1.4.0 release thanks to @argshook and his PR #87.

from react-docgen-typescript.

pvasek avatar pvasek commented on June 15, 2024

I am not sure if this is wrong thing. Does styleguidist (with js only) use display name instead? I see display name more as the friendly name for debugging. Am I wrong?

from react-docgen-typescript.

argshook avatar argshook commented on June 15, 2024

i consider this a bug since displayName value returned is wrong. don't know how styleguidist works in this regard, as i have not used it.

i am using react-docgen-typescript as a standalone tool. my components do have displayName set, however, react-docgen-typsecript returns me displayName: 'index' or something alike (filename).

another library that i use together with react-docgen-typescript is react-docgen, it is used to parse components written in good old js. this library does handle displayName and returns me same displayName that is set in component. i can later display that in documentation.

if i have js component and document it with react-docgen, all is good. now let's say i decide to rewrite js component to ts and use react-docgen-typescript this time. everything is ok, except from displayName. I used to get corret value, but now i get filename. it rarely is the same as displayName. thus, my documentation becomes falsy.

i think current behaviour is not correct and fixing it should do no harm.

tried to PR this but didn't finish yet, don't know typescript compiler well enough so stumbling around. ill try to find more time to tackle this

from react-docgen-typescript.

dosentmatter avatar dosentmatter commented on June 15, 2024

Hey @argshook. Yeah, I think displayName is the easiest to implement with the most flexibility. Actually, I did find another workaround. If you do export const, the name would show up correctly:
image
image

export default doesn't change the name for some reason.

from react-docgen-typescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.