0
# Quick Parsing
1
2
Simple one-line parsing functionality for extracting React component documentation with default TypeScript configurations.
3
4
## Capabilities
5
6
### Parse Function
7
8
Parses React component files with default TypeScript options and returns component documentation.
9
10
```typescript { .api }
11
/**
12
* Parses a file with default TS options
13
* @param filePathOrPaths - Component file path(s) that should be parsed
14
* @param parserOpts - Options used to parse the files
15
* @returns Array of component documentation objects
16
*/
17
function parse(
18
filePathOrPaths: string | string[],
19
parserOpts: ParserOptions = defaultParserOpts
20
): ComponentDoc[];
21
```
22
23
**Usage Examples:**
24
25
```typescript
26
import { parse } from "react-docgen-typescript";
27
28
// Parse a single file
29
const docs = parse("./src/Button.tsx");
30
31
// Parse multiple files
32
const docs = parse([
33
"./src/Button.tsx",
34
"./src/Input.tsx",
35
"./src/Modal.tsx"
36
]);
37
38
// Parse with options
39
const docs = parse("./src/Button.tsx", {
40
savePropValueAsString: true,
41
shouldExtractLiteralValuesFromEnum: true,
42
propFilter: {
43
skipPropsWithName: ['className', 'style']
44
}
45
});
46
```
47
48
### Default Parser Options
49
50
The default parser options used when none are specified.
51
52
```typescript { .api }
53
const defaultParserOpts: ParserOptions;
54
```
55
56
### Default TypeScript Compiler Options
57
58
The default TypeScript compiler options used by the parse function.
59
60
```typescript { .api }
61
const defaultOptions: ts.CompilerOptions;
62
```
63
64
The default options include:
65
66
```typescript
67
{
68
jsx: ts.JsxEmit.React,
69
module: ts.ModuleKind.CommonJS,
70
target: ts.ScriptTarget.Latest,
71
esModuleInterop: true
72
}
73
```
74
75
## Component Detection
76
77
The parser automatically detects various types of React components:
78
79
- **Function Components**: Regular function declarations and arrow functions
80
- **Class Components**: Classes extending React.Component or React.PureComponent
81
- **Forward Ref Components**: Components wrapped with React.forwardRef
82
- **Memo Components**: Components wrapped with React.memo
83
- **Higher-Order Components**: Components returned from HOC patterns
84
- **Styled Components**: Components created with styled-components library
85
86
## Error Handling
87
88
The parse function will throw errors in the following cases:
89
90
- **File Not Found**: When specified file paths don't exist
91
- **TypeScript Compilation Errors**: When TypeScript cannot compile the source files
92
- **Invalid Component Syntax**: When component definitions are malformed
93
94
```typescript
95
import { parse } from "react-docgen-typescript";
96
97
try {
98
const docs = parse("./src/Component.tsx");
99
console.log(docs);
100
} catch (error) {
101
console.error("Parsing failed:", error.message);
102
}
103
```