1
1
import * as ts from 'typescript' ;
2
2
import * as t from './types' ;
3
3
4
+ /**
5
+ * Options that specify how the parser should act
6
+ */
7
+ interface ParserOptions {
8
+ /**
9
+ * Called before a PropType is added to a component
10
+ * @return true to include the PropType, false to skip it
11
+ * @default () => true
12
+ */
13
+ shouldInclude ?: ( data : { name : string } ) => boolean ;
14
+ }
15
+
4
16
/**
5
17
* A wrapper for `ts.createProgram`
6
18
* @param files The files to later be parsed with `parseFromProgram`
@@ -15,18 +27,30 @@ export function createProgram(files: string[], options: ts.CompilerOptions) {
15
27
* use `createProgram` and `parseFromProgram` for better performance
16
28
* @param filePath The file to parse
17
29
* @param options The options from `loadConfig`
30
+ * @param parserOptions Options that specify how the parser should act
18
31
*/
19
- export function parseFile ( filePath : string , options : ts . CompilerOptions ) {
32
+ export function parseFile (
33
+ filePath : string ,
34
+ options : ts . CompilerOptions ,
35
+ parserOptions : ParserOptions = { } ,
36
+ ) {
20
37
const program = ts . createProgram ( [ filePath ] , options ) ;
21
- return parseFromProgram ( filePath , program ) ;
38
+ return parseFromProgram ( filePath , program , parserOptions ) ;
22
39
}
23
40
24
41
/**
25
42
* Parses the specified file and returns the PropTypes as an AST
26
43
* @param filePath The file to get the PropTypes from
27
44
* @param program The program object returned by `createProgram`
45
+ * @param parserOptions Options that specify how the parser should act
28
46
*/
29
- export function parseFromProgram ( filePath : string , program : ts . Program ) {
47
+ export function parseFromProgram (
48
+ filePath : string ,
49
+ program : ts . Program ,
50
+ parserOptions : ParserOptions = { } ,
51
+ ) {
52
+ const { shouldInclude = ( ) => true } = parserOptions ;
53
+
30
54
const checker = program . getTypeChecker ( ) ;
31
55
const sourceFile = program . getSourceFile ( filePath ) ;
32
56
@@ -178,7 +202,9 @@ export function parseFromProgram(filePath: string, program: ts.Program) {
178
202
}
179
203
180
204
function parsePropsType ( name : string , type : ts . Type ) {
181
- const properties = type . getProperties ( ) ;
205
+ const properties = type
206
+ . getProperties ( )
207
+ . filter ( symbol => shouldInclude ( { name : symbol . getName ( ) } ) ) ;
182
208
if ( properties . length === 0 ) {
183
209
return ;
184
210
}
0 commit comments