Skip to content

Commit

Permalink
Merge pull request #398 from TrevorBurnham/trevorburnham/export-compo…
Browse files Browse the repository at this point in the history
…nent-identifier

feat: Provide raw expression as part of parser output
  • Loading branch information
pvasek authored Dec 1, 2021
2 parents eea08dc + a784601 commit 429f602
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
14 changes: 14 additions & 0 deletions src/__tests__/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
18 changes: 14 additions & 4 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface StringIndexedObject<T> {
}

export interface ComponentDoc {
expression?: ts.Symbol;
displayName: string;
filePath: string;
description: string;
Expand Down Expand Up @@ -88,6 +89,7 @@ export interface ParserOptions {
skipChildrenPropWithoutDoc?: boolean;
savePropValueAsString?: boolean;
shouldIncludePropTagMap?: boolean;
shouldIncludeExpression?: boolean;
customComponentTypes?: string[];
}

Expand Down Expand Up @@ -219,14 +221,16 @@ 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 {
savePropValueAsString,
shouldExtractLiteralValuesFromEnum,
shouldRemoveUndefinedFromOptional,
shouldExtractValuesFromUnion,
shouldIncludePropTagMap
shouldIncludePropTagMap,
shouldIncludeExpression
} = opts;
this.checker = program.getTypeChecker();
this.propFilter = buildFilter(opts);
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -367,7 +373,7 @@ export class Parser {
delete props[propName];
}
}
return {
result = {
tags,
filePath,
description,
Expand All @@ -376,7 +382,7 @@ export class Parser {
props
};
} else if (description && displayName) {
return {
result = {
tags,
filePath,
description,
Expand All @@ -386,7 +392,11 @@ export class Parser {
};
}

return null;
if (result !== null && this.shouldIncludeExpression) {
result.expression = rootExp;
}

return result;
}

public extractPropsFromTypeIfStatelessComponent(
Expand Down

0 comments on commit 429f602

Please sign in to comment.