Skip to content

Commit a763063

Browse files
authored
Exclude types with comments (#199)
1 parent 47978d9 commit a763063

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
lines changed

.changeset/fifty-apples-marry.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'pretty-proptypes': minor
3+
---
4+
5+
Props that have comments which start with `eslint-ignore` or `@ts-` are no longer rendered,
6+
other surrounding comments are still rendered for the prop however.

.changeset/pretty-carrots-raise.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'pretty-proptypes': minor
3+
---
4+
5+
Props that have comments which contain `@internal` or `@access private` are no longer rendered to the props table,
6+
essentially having the prop and all of its comments hidden.

packages/pretty-proptypes/src/PropType/index.js

+40-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,39 @@ import React from 'react';
44
import convert, { getKind, reduceToObj } from 'kind2string';
55
import allComponents from '../components';
66

7+
const IGNORE_COMMENTS_STARTING_WITH = ['eslint-disable', '@ts-'];
8+
const HIDE_PROPS_THAT_CONTAIN = ['@internal', '@access private'];
9+
10+
const shouldIgnoreComment = comment => {
11+
if (!comment) {
12+
return false;
13+
}
14+
15+
for (let i = 0; i < IGNORE_COMMENTS_STARTING_WITH.length; i++) {
16+
const value = IGNORE_COMMENTS_STARTING_WITH[i];
17+
if (comment.startsWith(value)) {
18+
return true;
19+
}
20+
}
21+
22+
return false;
23+
};
24+
25+
const shouldHideProp = comment => {
26+
if (!comment) {
27+
return false;
28+
}
29+
30+
for (let i = 0; i < HIDE_PROPS_THAT_CONTAIN.length; i++) {
31+
const value = HIDE_PROPS_THAT_CONTAIN[i];
32+
if (comment.includes(value)) {
33+
return true;
34+
}
35+
}
36+
37+
return false;
38+
};
39+
740
const renderPropType = (
841
propType: any,
942
{ overrides = {}, shouldCollapseProps, components }: any,
@@ -28,7 +61,9 @@ const renderPropType = (
2861

2962
let description;
3063
if (propType.leadingComments) {
31-
description = propType.leadingComments.reduce((acc, { value }) => acc.concat(`\n${value}`), '');
64+
description = propType.leadingComments
65+
.filter(({ value }) => !shouldIgnoreComment(value))
66+
.reduce((acc, { value }) => acc.concat(`\n${value}`), '');
3267
}
3368

3469
if (!propType.value) {
@@ -41,6 +76,10 @@ const renderPropType = (
4176
return null;
4277
}
4378

79+
if (shouldHideProp(description)) {
80+
return null;
81+
}
82+
4483
const name = propType.kind === 'spread' ? '...' : convert(propType.key);
4584
const OverrideComponent = overrides[name];
4685
const commonProps = {

stories/FlowComponent.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import React from 'react';
77

88
type FlowComponentProps = {
99
// This prop is required as it is not optional and has no default
10+
// eslint-disable-next-line
1011
requiredProp: any,
1112
// This prop is a string
13+
// @ts-ignore
1214
stringProp: string,
1315
// This prop is a number
1416
numberProp: number,
@@ -27,7 +29,9 @@ type FlowComponentProps = {
2729
// This prop is a mixed
2830
mixedProp: mixed,
2931
// This prop is a union
30-
unionProp: 'hello' | 'world'
32+
unionProp: 'hello' | 'world',
33+
// @internal
34+
hideProp: Boolean
3135
};
3236

3337
const FlowComponent = (props: FlowComponentProps) => <p>Hello World</p>;

stories/TypeScriptComponent.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ interface DummyInterface {
1111

1212
type TypeScriptComponentProps = {
1313
// This prop is required as it is not optional and has no default
14+
// eslint-disable-next-line
1415
requiredProp: any;
1516
// This prop is a string
17+
// @ts-ignore
1618
stringProp: string;
1719
// This prop is a number
1820
numberProp: number;
@@ -36,6 +38,8 @@ type TypeScriptComponentProps = {
3638
unsupportedProp: keyof DummyInterface;
3739
// This prop uses hyphens, so the type uses quotations around the key
3840
'quoted-prop': any;
41+
// @internal
42+
hideProp: Boolean;
3943
};
4044

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

0 commit comments

Comments
 (0)