From bc64e1ecf88d8e6c84de0f26448f52a76edb45a9 Mon Sep 17 00:00:00 2001 From: jrwebdev Date: Tue, 5 Sep 2017 09:15:31 +1200 Subject: [PATCH 1/2] Add failing test for extending props from an external .tsx file --- .../data/ExtendsExternalPropsComponent.tsx | 16 ++++++++++++++++ .../ExtendsExternalPropsComponentParentProps.tsx | 4 ++++ src/__tests__/parser.ts | 10 ++++++++++ 3 files changed, 30 insertions(+) create mode 100644 src/__tests__/data/ExtendsExternalPropsComponent.tsx create mode 100644 src/__tests__/data/ExtendsExternalPropsComponentParentProps.tsx diff --git a/src/__tests__/data/ExtendsExternalPropsComponent.tsx b/src/__tests__/data/ExtendsExternalPropsComponent.tsx new file mode 100644 index 00000000..318774ce --- /dev/null +++ b/src/__tests__/data/ExtendsExternalPropsComponent.tsx @@ -0,0 +1,16 @@ +import * as React from 'react'; +import { ParentProps } from './ExtendsExternalPropsComponentParentProps'; + +interface ExtendsExternalPropsComponentProps extends ParentProps { + /** prop2 */ + prop2?: string; +} + +/** + * ExtendsExternalPropsComponent description + */ +export class ExtendsExternalPropsComponent extends React.Component { + render() { + return
ExtendsExternalPropsComponent
+ } +} \ No newline at end of file diff --git a/src/__tests__/data/ExtendsExternalPropsComponentParentProps.tsx b/src/__tests__/data/ExtendsExternalPropsComponentParentProps.tsx new file mode 100644 index 00000000..74c9d47b --- /dev/null +++ b/src/__tests__/data/ExtendsExternalPropsComponentParentProps.tsx @@ -0,0 +1,4 @@ +export interface ParentProps { + /** prop1 */ + prop1?: number; +} diff --git a/src/__tests__/parser.ts b/src/__tests__/parser.ts index 0e5b9c33..888587aa 100644 --- a/src/__tests__/parser.ts +++ b/src/__tests__/parser.ts @@ -102,6 +102,16 @@ describe('parser', () => { }); }); + it('should parse react component with properties extended from an external .tsx file', function(){ + check('ExtendsExternalPropsComponent', { + ExtendsExternalPropsComponent: { + children, + prop1: { type: 'number', required: false, description: 'prop1' }, + prop2: { type: 'string', required: false, description: 'prop2' }, + } + }); + }); + it('should parse react component with properties defined as type', function(){ check('FlippableImage', { FlippableImage: { From 2e4452a396f1c6090461a64af8dbdc8bcb091396 Mon Sep 17 00:00:00 2001 From: Pavel Vasek Date: Fri, 8 Sep 2017 18:11:30 +0200 Subject: [PATCH 2/2] Imports from tsx files are not working - #43 fixed --- README.md | 8 +++++++- package.json | 2 +- src/__tests__/parser.ts | 2 +- src/index.ts | 4 ++++ src/parser.ts | 23 ++++++++++++++++++----- 5 files changed, 31 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 61b86c9e..0d3d0003 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package.json b/package.json index 2ca3174d..f6e2784d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-docgen-typescript", - "version": "1.0.0", + "version": "1.0.1", "description": "", "main": "lib/index.js", "scripts": { diff --git a/src/__tests__/parser.ts b/src/__tests__/parser.ts index 888587aa..ded9e80e 100644 --- a/src/__tests__/parser.ts +++ b/src/__tests__/parser.ts @@ -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, diff --git a/src/index.ts b/src/index.ts index af6dcf58..743cdd72 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,7 @@ import { parse, + withDefaultConfig, + withCustomConfig, ComponentDoc, Props, PropItem, @@ -8,6 +10,8 @@ import { export { parse, + withDefaultConfig, + withCustomConfig, ComponentDoc, Props, PropItem, diff --git a/src/parser.ts b/src/parser.ts index 3fe4211f..7e4d907b 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -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); @@ -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); @@ -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); @@ -160,6 +171,8 @@ class Parser { const result: Props = {}; propertiesOfProps.forEach(prop => { + console.log("parser.getPropsInfo prop: ", prop.name); + const propName = prop.getName();