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

Exclude types with comments #199

Merged
merged 8 commits into from
Jul 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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