Skip to content

Commit

Permalink
Exclude types with comments (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
Madou authored Jul 4, 2021
1 parent 47978d9 commit a763063
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/fifty-apples-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'pretty-proptypes': minor
---

Props that have comments which start with `eslint-ignore` or `@ts-` are no longer rendered,
other surrounding comments are still rendered for the prop however.
6 changes: 6 additions & 0 deletions .changeset/pretty-carrots-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'pretty-proptypes': minor
---

Props that have comments which contain `@internal` or `@access private` are no longer rendered to the props table,
essentially having the prop and all of its comments hidden.
41 changes: 40 additions & 1 deletion packages/pretty-proptypes/src/PropType/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,39 @@ import React from 'react';
import convert, { getKind, reduceToObj } from 'kind2string';
import allComponents from '../components';

const IGNORE_COMMENTS_STARTING_WITH = ['eslint-disable', '@ts-'];
const HIDE_PROPS_THAT_CONTAIN = ['@internal', '@access private'];

const shouldIgnoreComment = comment => {
if (!comment) {
return false;
}

for (let i = 0; i < IGNORE_COMMENTS_STARTING_WITH.length; i++) {
const value = IGNORE_COMMENTS_STARTING_WITH[i];
if (comment.startsWith(value)) {
return true;
}
}

return false;
};

const shouldHideProp = comment => {
if (!comment) {
return false;
}

for (let i = 0; i < HIDE_PROPS_THAT_CONTAIN.length; i++) {
const value = HIDE_PROPS_THAT_CONTAIN[i];
if (comment.includes(value)) {
return true;
}
}

return false;
};

const renderPropType = (
propType: any,
{ overrides = {}, shouldCollapseProps, components }: any,
Expand All @@ -28,7 +61,9 @@ const renderPropType = (

let description;
if (propType.leadingComments) {
description = propType.leadingComments.reduce((acc, { value }) => acc.concat(`\n${value}`), '');
description = propType.leadingComments
.filter(({ value }) => !shouldIgnoreComment(value))
.reduce((acc, { value }) => acc.concat(`\n${value}`), '');
}

if (!propType.value) {
Expand All @@ -41,6 +76,10 @@ const renderPropType = (
return null;
}

if (shouldHideProp(description)) {
return null;
}

const name = propType.kind === 'spread' ? '...' : convert(propType.key);
const OverrideComponent = overrides[name];
const commonProps = {
Expand Down
6 changes: 5 additions & 1 deletion stories/FlowComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import React from 'react';

type FlowComponentProps = {
// This prop is required as it is not optional and has no default
// eslint-disable-next-line
requiredProp: any,
// This prop is a string
// @ts-ignore
stringProp: string,
// This prop is a number
numberProp: number,
Expand All @@ -27,7 +29,9 @@ type FlowComponentProps = {
// This prop is a mixed
mixedProp: mixed,
// This prop is a union
unionProp: 'hello' | 'world'
unionProp: 'hello' | 'world',
// @internal
hideProp: Boolean
};

const FlowComponent = (props: FlowComponentProps) => <p>Hello World</p>;
Expand Down
4 changes: 4 additions & 0 deletions stories/TypeScriptComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ interface DummyInterface {

type TypeScriptComponentProps = {
// This prop is required as it is not optional and has no default
// eslint-disable-next-line
requiredProp: any;
// This prop is a string
// @ts-ignore
stringProp: string;
// This prop is a number
numberProp: number;
Expand All @@ -36,6 +38,8 @@ type TypeScriptComponentProps = {
unsupportedProp: keyof DummyInterface;
// This prop uses hyphens, so the type uses quotations around the key
'quoted-prop': any;
// @internal
hideProp: Boolean;
};

const TypeScriptComponent = (props: TypeScriptComponentProps) => <p>Hello World</p>;
Expand Down

0 comments on commit a763063

Please sign in to comment.