Skip to content

Commit

Permalink
Imports from tsx files are not working - #43 fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
pvasek committed Sep 8, 2017
1 parent 24b71a7 commit 2e4452a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ npm install --save-dev react-docgen-typescript
Include following line in your `styleguide.config.js`:

```javascript
propsParser: require('react-docgen-typescript').parse
propsParser: require('react-docgen-typescript').withDefaultConfig()
```

or if you want to use custom tsconfig file

```javascript
propsParser: require('react-docgen-typescript').withCustomConfig('./tsconfig.json')
```

## Example
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-docgen-typescript",
"version": "1.0.0",
"version": "1.0.1",
"description": "",
"main": "lib/index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('parser', () => {
});
});

it('should parse react component with properties extended from an external .tsx file', function(){
it.only('should parse react component with properties extended from an external .tsx file', function(){
check('ExtendsExternalPropsComponent', {
ExtendsExternalPropsComponent: {
children,
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
parse,
withDefaultConfig,
withCustomConfig,
ComponentDoc,
Props,
PropItem,
Expand All @@ -8,6 +10,8 @@ import {

export {
parse,
withDefaultConfig,
withCustomConfig,
ComponentDoc,
Props,
PropItem,
Expand Down
23 changes: 18 additions & 5 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,35 @@ export interface PropItemType {
value?: any;
}

export interface FileParser {
parse(filePath: string): ComponentDoc[];
}

const defaultOptions: ts.CompilerOptions = {
target: ts.ScriptTarget.Latest,
module: ts.ModuleKind.CommonJS
module: ts.ModuleKind.CommonJS,
jsx: ts.JsxEmit.React,
};

/**
* Parses a file with default TS options
* @param filePath
* @param filePath component file that should be parsed
*/
export function parse(filePath: string) {
return withCompilerOptions(defaultOptions).parse(filePath);
}

/**
* Constructs a parser for a default configuration.
*/
export function withDefaultConfig(): FileParser {
return withCompilerOptions(defaultOptions);
}

/**
* Constructs a parser for a specified tsconfig file.
*/
export function withConfig(tsconfigPath: string) {
export function withCustomConfig(tsconfigPath: string): FileParser {
const configJson = JSON.parse(fs.readFileSync(tsconfigPath, 'utf8'));
const basePath = path.dirname(tsconfigPath);

Expand All @@ -58,7 +70,7 @@ export function withConfig(tsconfigPath: string) {
/**
* Constructs a parser for a specified set of TS compiler options.
*/
export function withCompilerOptions(compilerOptions: ts.CompilerOptions) {
export function withCompilerOptions(compilerOptions: ts.CompilerOptions): FileParser {
return {
parse(filePath: string): ComponentDoc[] {
const program = ts.createProgram([filePath], compilerOptions);
Expand Down Expand Up @@ -89,7 +101,6 @@ class Parser {

public getComponentInfo(exp: ts.Symbol, source: ts.SourceFile): ComponentDoc {
const type = this.checker.getTypeOfSymbolAtLocation(exp, exp.valueDeclaration);

let propsType = this.extractPropsFromTypeIfStatelessComponent(type);
if (!propsType) {
propsType = this.extractPropsFromTypeIfStatefulComponent(type);
Expand Down Expand Up @@ -160,6 +171,8 @@ class Parser {
const result: Props = {};

propertiesOfProps.forEach(prop => {
console.log("parser.getPropsInfo prop: ", prop.name);

const propName = prop.getName();


Expand Down

0 comments on commit 2e4452a

Please sign in to comment.