Skip to content
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

parse default values in immediately destructured props #338

Conversation

michelengelen
Copy link
Contributor

@michelengelen michelengelen commented Mar 31, 2021

We recently had the problem, that with the following component setup there were no defaultValues exported from the docgen:

import {
  PROPERTY1_DEFAULT,
  PROPERTY2_DEFAULT,
  PROPERTY3_DEFAULT
} from './SampleComponent.constants.ts';

const PROPERTY4_DEFAULT = "this is a string";
const PROPERTY5_DEFAULT = true;

type Property1Type = 'hello' | 'world';

type Props = {
  /** prop1 description */
  prop1?: Property1Type;
  /** prop2 description */
  prop2?: 'goodbye' | 'farewell';
  /** prop3 description */
  prop3?: number;
  /** prop4 description */
  prop4?: string;
  /** prop5 description */
  prop5?: boolean;
};

/** SampleComponent description */
const SampleComponent: React.FC<Props> = ({
  prop1 = PROPERTY1_DEFAULT,
  prop2 = PROPERTY2_DEFAULT,
  prop3 = PROPERTY3_DEFAULT,
  prop4 = PROPERTY4_DEFAULT,
  prop5 = PROPERTY5_DEFAULT
}) => ( ... );

export default SampleComponent;

This PR adds the ability to correctly follow initialisation with pre-defined (and/or imported) variables.

@michelengelen michelengelen changed the title Feature/parse default values in immediately destructured props WIP - parse default values in immediately destructured props Mar 31, 2021
@michelengelen
Copy link
Contributor Author

I do not know if this is the right approach, but I added a new file for testing import statements (and value extraction from those imports).

IMO there should be a separate folder with a constants file where all literal values are assigned to variables/constants and exported, so that those can be used in different tests. For now, and since it is only used by one test, I added it into the __tests__/data folder together with the test.

@michelengelen michelengelen changed the title WIP - parse default values in immediately destructured props parse default values in immediately destructured props Mar 31, 2021
@pvasek
Copy link
Collaborator

pvasek commented Apr 4, 2021

@michelengelen Thanks for your PR. It seems perfectly fine to add these constant next to the test. I maybe even prefer this approach as each test is "on it's own" (no meter if it is a single file or multiple files).

@pvasek pvasek merged commit 9ef6271 into styleguidist:master Apr 4, 2021
@pvasek
Copy link
Collaborator

pvasek commented Apr 4, 2021

@michelengelen thanks a lot! Published on npm as v1.22.0.

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.

2 participants