Skip to content

Commit 2952e78

Browse files
committed
feat(parser): control included proptypes
1 parent 6324fc1 commit 2952e78

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

Diff for: src/parser.ts

+30-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
import * as ts from 'typescript';
22
import * as t from './types';
33

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+
416
/**
517
* A wrapper for `ts.createProgram`
618
* @param files The files to later be parsed with `parseFromProgram`
@@ -15,18 +27,30 @@ export function createProgram(files: string[], options: ts.CompilerOptions) {
1527
* use `createProgram` and `parseFromProgram` for better performance
1628
* @param filePath The file to parse
1729
* @param options The options from `loadConfig`
30+
* @param parserOptions Options that specify how the parser should act
1831
*/
19-
export function parseFile(filePath: string, options: ts.CompilerOptions) {
32+
export function parseFile(
33+
filePath: string,
34+
options: ts.CompilerOptions,
35+
parserOptions: ParserOptions = {},
36+
) {
2037
const program = ts.createProgram([filePath], options);
21-
return parseFromProgram(filePath, program);
38+
return parseFromProgram(filePath, program, parserOptions);
2239
}
2340

2441
/**
2542
* Parses the specified file and returns the PropTypes as an AST
2643
* @param filePath The file to get the PropTypes from
2744
* @param program The program object returned by `createProgram`
45+
* @param parserOptions Options that specify how the parser should act
2846
*/
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+
3054
const checker = program.getTypeChecker();
3155
const sourceFile = program.getSourceFile(filePath);
3256

@@ -178,7 +202,9 @@ export function parseFromProgram(filePath: string, program: ts.Program) {
178202
}
179203

180204
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() }));
182208
if (properties.length === 0) {
183209
return;
184210
}

0 commit comments

Comments
 (0)