diff --git a/src/__tests__/parser.ts b/src/__tests__/parser.ts index f1149790..e4ec11c8 100644 --- a/src/__tests__/parser.ts +++ b/src/__tests__/parser.ts @@ -1494,6 +1494,20 @@ describe('parser', () => { ); }); }); + + describe('shouldIncludeExpression', () => { + it('should be disabled by default', () => { + const [parsed] = parse(fixturePath('StatelessDisplayName')); + assert.equal(parsed.expression, undefined); + }); + + it('should cause the parser to return the component expression when set to true', () => { + const [parsed] = parse(fixturePath('StatelessDisplayName'), { + shouldIncludeExpression: true + }); + assert.equal(parsed.expression!.name, 'Stateless'); + }); + }); }); describe('withCustomConfig', () => { diff --git a/src/parser.ts b/src/parser.ts index 583cb107..d22f26ee 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -14,6 +14,7 @@ export interface StringIndexedObject { } export interface ComponentDoc { + expression?: ts.Symbol; displayName: string; filePath: string; description: string; @@ -88,6 +89,7 @@ export interface ParserOptions { skipChildrenPropWithoutDoc?: boolean; savePropValueAsString?: boolean; shouldIncludePropTagMap?: boolean; + shouldIncludeExpression?: boolean; customComponentTypes?: string[]; } @@ -219,6 +221,7 @@ export class Parser { private readonly shouldExtractValuesFromUnion: boolean; private readonly savePropValueAsString: boolean; private readonly shouldIncludePropTagMap: boolean; + private readonly shouldIncludeExpression: boolean; constructor(program: ts.Program, opts: ParserOptions) { const { @@ -226,7 +229,8 @@ export class Parser { shouldExtractLiteralValuesFromEnum, shouldRemoveUndefinedFromOptional, shouldExtractValuesFromUnion, - shouldIncludePropTagMap + shouldIncludePropTagMap, + shouldIncludeExpression } = opts; this.checker = program.getTypeChecker(); this.propFilter = buildFilter(opts); @@ -239,6 +243,7 @@ export class Parser { this.shouldExtractValuesFromUnion = Boolean(shouldExtractValuesFromUnion); this.savePropValueAsString = Boolean(savePropValueAsString); this.shouldIncludePropTagMap = Boolean(shouldIncludePropTagMap); + this.shouldIncludeExpression = Boolean(shouldIncludeExpression); } private getComponentFromExpression(exp: ts.Symbol) { @@ -350,6 +355,7 @@ export class Parser { computeComponentName(nameSource, source, customComponentTypes); const methods = this.getMethodsInfo(type); + let result: ComponentDoc | null = null; if (propsType) { if (!commentSource.valueDeclaration) { return null; @@ -367,7 +373,7 @@ export class Parser { delete props[propName]; } } - return { + result = { tags, filePath, description, @@ -376,7 +382,7 @@ export class Parser { props }; } else if (description && displayName) { - return { + result = { tags, filePath, description, @@ -386,7 +392,11 @@ export class Parser { }; } - return null; + if (result !== null && this.shouldIncludeExpression) { + result.expression = rootExp; + } + + return result; } public extractPropsFromTypeIfStatelessComponent(