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

Allow filtering by parent interface or type declaration #111

Merged
merged 1 commit into from
Aug 18, 2018

Conversation

rkostrzewski
Copy link
Contributor

This PR adds the ability to filter props based on where they were declared.

Solves: #56 (comment), #49 (comment)

Problem

Currently we have no way of filtering props based on where they were declared. The common problem is props that extend HTMLAttributes<T> interface, e.g.:

import * as React from 'react';

export interface IButtonProps extends React.HTMLAttributes<HTMLButtonElement> {
  size: 'large' | 'medium' | 'small';
}

Filtering them using skipPropsWithoutDoc is not enough as some aria attributes have documentation and forces the users to write documentation for props. Using skipPropsWithName can cause problems if names of props are the same (e.g. custom onClick, onFocus handlers).

Proposed solution

TypeScript allows us to get the place where the prop was declared (I believe the only valid places are InterfaceDeclaration and TypeDeclaration). We could pass the name of the type the prop was declared in and filename where the declaration of the type is placed to propFilter so that users have more control of which props end up in documentation. Proposed change is to add parent object to PropItem interface of filter function ((props: PropItem, component: Component) => boolean)

{
  ...
  // can be undefined as the prop can be defined inline, e.g. `(props: { foo: string }) => (<div>{foo}</div>)`
  parent: {
    // Interface or TypeAlias name
    name: string;
    // full path to file where it was declared, e.g. /Users/foo/my-app/node_modules/@types/react/index.d.ts
    fileName: string;
  }
}

I've added test to cover use case of filtering HTMLAttributes and verified using StyleGuidist that they are not documented using custom interface extending from HTMLAttributes and using a component created via styled-components (which automatically add HTMLAttributes to html components)

Before:
before

After:
after

using following styleguide.config.js:

module.exports = {
  propsParser: require('react-docgen-typescript').withDefaultConfig({
    propFilter: (prop) => {
      if (prop.parent == null) {
        return true;
      }

      return prop.parent.fileName.indexOf('node_modules/@types/react') < 0;
    }
  }).parse,
  webpackConfig: require('react-scripts-ts/config/webpack.config.dev')
}

@plotnikovn
Copy link

@pvasek when release?)

@pvasek
Copy link
Collaborator

pvasek commented Aug 20, 2018

It has been released a second ago as v1.7.0.
@rkostrzewski thanks a lot!

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.

None yet

3 participants