Skip to content

Commit b1ec138

Browse files
authored
Merge pull request #2 from styleguidist/master
Sync with upstream
2 parents bc4ed7f + d2e4dbd commit b1ec138

File tree

10 files changed

+358
-41
lines changed

10 files changed

+358
-41
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,23 @@ npm install --save-dev react-docgen-typescript
1919
Include following line in your `styleguide.config.js`:
2020

2121
```javascript
22-
propsParser: require('react-docgen-typescript').withDefaultConfig().parse
22+
propsParser: require('react-docgen-typescript').withDefaultConfig([parserOptions]).parse
2323
```
2424

2525
or if you want to use custom tsconfig file
2626

2727
```javascript
28-
propsParser: require('react-docgen-typescript').withCustomConfig('./tsconfig.json').parse
28+
propsParser: require('react-docgen-typescript').withCustomConfig('./tsconfig.json', [parserOptions]).parse
2929
```
3030

31+
### parserOptions
32+
- `skipPropsWithName?: string[] | string;`
33+
- `skipPropsWithoutDoc?: boolean;`
34+
35+
or
36+
37+
`(props: PropItem, component: Component) => boolean`
38+
3139
## Example
3240

3341
In the example folder you can see React Styleguidist integration.
@@ -121,6 +129,8 @@ the part it's fixing.
121129

122130
@brettjurgens Brett Jurgens - adding support for default props
123131

132+
@dotcs Fabian Mueller - introduced parserOptions for skipping undocumented properties
133+
124134
## Thanks to others
125135

126136
The integration with React Styleguidist wouldn't be possible without [Vyacheslav Slinko](https://github.com/vslinko) pull request [#118](https://github.com/styleguidist/react-styleguidist/pull/118) at React Styleguidist.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-docgen-typescript",
3-
"version": "1.2.1",
3+
"version": "1.2.2",
44
"description": "",
55
"main": "lib/index.js",
66
"scripts": {

src/__tests__/buildFilter.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { assert, expect } from 'chai';
2+
import { buildFilter } from '../buildFilter';
3+
import { ParserOptions, Props, PropItem } from '../parser';
4+
5+
function createProp(name: string, required: boolean = false, defaultValue: any = undefined,
6+
description: string = '', type = {name: 'string'}): PropItem {
7+
return {
8+
name,
9+
type,
10+
required,
11+
defaultValue,
12+
description
13+
}
14+
}
15+
16+
describe('buildFilter', () => {
17+
18+
describe('default behaviour', () => {
19+
it('should skip "children" property if no description is set', () => {
20+
const prop1 = createProp('prop1', false, undefined, 'prop1 description');
21+
const prop2 = createProp('prop2', false, undefined, 'prop2 description');
22+
const children = createProp('children', false, undefined, '');
23+
const opts: ParserOptions = {};
24+
const filterFn = buildFilter(opts);
25+
expect([prop1, prop2, children].filter((prop) => filterFn(prop, {name: prop.name}))).to.eql([prop1, prop2]);
26+
});
27+
it('should not skip "children" property if description is set', () => {
28+
const prop1 = createProp('prop1', false, undefined, 'prop1 description');
29+
const prop2 = createProp('prop2', false, undefined, 'prop2 description');
30+
const children = createProp('children', false, undefined, 'children description');
31+
const opts: ParserOptions = {};
32+
const filterFn = buildFilter(opts);
33+
expect([prop1, prop2, children].filter((prop) => filterFn(prop, {name: prop.name}))).to.eql([prop1, prop2, children]);
34+
});
35+
});
36+
37+
describe('static prop filter', () => {
38+
39+
describe('skipPropsWithName', () => {
40+
it('should skip single prop by name', () => {
41+
const prop1 = createProp('prop1', false, undefined, 'prop1 description');
42+
const prop2 = createProp('prop2', false, undefined, 'prop2 description');
43+
const opts: ParserOptions = {
44+
propFilter: { skipPropsWithName: 'prop1' }
45+
};
46+
const filterFn = buildFilter(opts);
47+
expect([prop1, prop2].filter((prop) => filterFn(prop, {name: prop.name}))).to.eql([prop2]);
48+
});
49+
it('should skip multiple props by name', () => {
50+
const prop1 = createProp('prop1', false, undefined, 'prop1 description');
51+
const prop2 = createProp('prop2', false, undefined, 'prop2 description');
52+
const prop3 = createProp('prop3', false, undefined, 'prop3 description');
53+
const opts: ParserOptions = {
54+
propFilter: { skipPropsWithName: ['prop1', 'prop3'] }
55+
};
56+
const filterFn = buildFilter(opts);
57+
expect([prop1, prop2, prop3].filter((prop) => filterFn(prop, {name: prop.name}))).to.eql([prop2]);
58+
});
59+
});
60+
61+
describe('skipPropsWithoutDoc', () => {
62+
it('should skip children props with no documentation', () => {
63+
const prop1 = createProp('prop1', false, undefined, 'prop1 description');
64+
const prop2 = createProp('prop2', false, undefined, '');
65+
const opts: ParserOptions = {
66+
propFilter: { skipPropsWithoutDoc: true }
67+
};
68+
const filterFn = buildFilter(opts);
69+
expect([prop1, prop2].filter((prop) => filterFn(prop, {name: prop.name}))).to.eql([prop1]);
70+
});
71+
});
72+
73+
});
74+
75+
describe('dynamic prop filter', () => {
76+
it('should skip props based on dynamic filter rule', () => {
77+
const prop1 = createProp('foo', false, undefined, 'foo description');
78+
const prop2 = createProp('bar', false, undefined, 'bar description');
79+
const prop3 = createProp('foobar', false, undefined, 'foobar description');
80+
const opts: ParserOptions = {
81+
propFilter: (prop, component) => prop.name.indexOf('foo') === -1
82+
};
83+
const filterFn = buildFilter(opts);
84+
expect([prop1, prop2, prop3].filter((prop) => filterFn(prop, {name: prop.name}))).to.eql([prop2]);
85+
});
86+
87+
it('should get be possible to filter by component name', () => {
88+
const prop1 = createProp('foo', false, undefined, 'foo description');
89+
const prop2 = createProp('bar', false, undefined, 'bar description');
90+
const prop3 = createProp('foobar', false, undefined, 'foobar description');
91+
const opts: ParserOptions = {
92+
propFilter: (prop, component) => component.name.indexOf('BAR') === -1
93+
};
94+
const filterFn = buildFilter(opts);
95+
expect([prop1, prop2, prop3].filter((prop) => filterFn(prop, { name: prop.name.toUpperCase() }))).to.eql([prop1]);
96+
});
97+
});
98+
99+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as React from 'react';
2+
3+
/**
4+
* Column properties.
5+
*/
6+
export interface IColumnProps {
7+
/** prop1 description */
8+
prop1?: string;
9+
/** prop2 description */
10+
prop2: number;
11+
/**
12+
* prop3 description
13+
*/
14+
prop3: () => void;
15+
/** prop4 description */
16+
prop4: 'option1' | 'option2' | "option3";
17+
18+
/** children description */
19+
children?: React.ReactNode;
20+
}
21+
22+
/**
23+
* Column description
24+
*/
25+
export class Column extends React.Component<IColumnProps, {}> {
26+
public static defaultProps: Partial<IColumnProps> = {
27+
prop1: 'prop1'
28+
};
29+
30+
render() {
31+
const {prop1} = this.props;
32+
return <div>{prop1}</div>;
33+
}
34+
}
35+
36+
export default Column;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as React from 'react';
2+
3+
/**
4+
* Column properties.
5+
*/
6+
export interface IColumnProps {
7+
/** prop1 description */
8+
prop1?: string;
9+
/** prop2 description */
10+
prop2: number;
11+
}
12+
13+
/**
14+
* Column description
15+
*/
16+
export class Column extends React.Component<IColumnProps, {}> {
17+
public static defaultProps: Partial<IColumnProps> = {
18+
prop1: 'prop1'
19+
};
20+
21+
render() {
22+
const {prop1} = this.props;
23+
return <div>{prop1}</div>;
24+
}
25+
}
26+
27+
export default Column;

0 commit comments

Comments
 (0)