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

Extracting type info from helper functions/utils?

Open jgodi opened this issue 3 years ago • 2 comments

Is it possible to extract the types and get info for the following styles and dataTestIds as well? When I run parse on the following snippet I only get the props.

import React from 'react';
import { ClassesType } from '../types';

export const styles = () => {
  return {
    /** Color of the root */
    root: {
      color: 'red'
    }
  };
};

export const DataTestIds = {
  /** ONE */
  ONE: 'one'
};

export interface Example1ComponentProps {
  /**
   * Text to pass to the component
   * @default undefined
   */
  text: string;

  /**
   * Classes to pass to the component
   */
  classes?: ClassesType<typeof styles>;

  /** @private */
  prv?: string;
}

export const Example1Component = ({ text }: Example1ComponentProps) => {
  const test = styles();
  return (
    <div style={test.root} data-testid={DataTestIds.ONE}>
      Example 2 Component: {text}
    </div>
  );
};

The output is this:

[
  {
    "tags": {},
    "filePath": "example/example.tsx",
    "description": "",
    "displayName": "Example1Component",
    "methods": [],
    "props": {
      "text": {
        "defaultValue": { "value": "undefined" },
        "description": "Text to pass to the component",
        "name": "text",
        "parent": {
          "fileName": "example/example.tsx",
          "name": "Example1ComponentProps"
        },
        "declarations": [
          {
            "fileName": "example/example.tsx",
            "name": "Example1ComponentProps"
          }
        ],
        "required": true,
        "type": { "name": "string" }
      },
      "classes": {
        "defaultValue": null,
        "description": "Classes to pass to the component",
        "name": "classes",
        "parent": {
          "fileName": "example/example.tsx",
          "name": "Example1ComponentProps"
        },
        "declarations": [
          {
            "fileName": "example/example.tsx",
            "name": "Example1ComponentProps"
          }
        ],
        "required": false,
        "type": { "name": "ClassesType<() => { root: { color: string; }; }>" }
      }
    }
  }
]

My parser file looks like this:

import path from 'path';
import fs from 'fs';
import globby from 'globby';
import { withDefaultConfig } from 'react-docgen-typescript';

const customParser = withDefaultConfig({
  savePropValueAsString: true,
  shouldExtractLiteralValuesFromEnum: true
});

const paths = globby.sync([
  path.resolve(__dirname, '../packages/lib1/src/example/example.tsx')
]);

const docGen = customParser.parse(paths);

fs.writeFileSync(
  path.resolve(__dirname, './props.json'),
  JSON.stringify(docGen)
);

I am hoping to get types and info for the styles and dataTestIds so that I can show that in my custom documentation for props/styles/testing. This works in our storybook setup, but we are wanting to do a custom doc site and I am having troubles getting this to work.

Thank you!

jgodi avatar Jul 20 '21 00:07 jgodi

I think this is pretty common complain. Unfortunately, I think this can be handled for all cases only with completely different tool, something like https://typedoc.org/.

The output of react-docgen-typescript is only simple structure that's required by Styleguidist (with some small extensions) and it's not possible to handle everything that's supported in typescipt.

pvasek avatar Oct 13 '21 19:10 pvasek

There was no activity for a long time. The issue will be closed soon.

github-actions[bot] avatar Oct 14 '22 10:10 github-actions[bot]

Closing this issue as obsolete.

github-actions[bot] avatar Oct 21 '22 10:10 github-actions[bot]