Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tags to ComponentDoc #307

Merged
merged 1 commit into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"lint-staged": "^7.3.0",
"lodash": "^4.17.15",
"mocha": "^5.2.0",
"prettier": "^1.10.2",
"prettier": "^1.19.1",
"prop-types": "^15.6.2",
"react": "^16.4.2",
"source-map-support": "^0.5.6",
Expand Down
73 changes: 34 additions & 39 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface ComponentDoc {
description: string;
props: Props;
methods: Method[];
tags?: {};
}

export interface Props extends StringIndexedObject<PropItem> {}
Expand Down Expand Up @@ -141,9 +142,7 @@ export function withCustomConfig(

if (error !== undefined) {
// tslint:disable-next-line: max-line-length
const errorText = `Cannot load custom tsconfig.json from provided path: ${tsconfigPath}, with error code: ${
error.code
}, message: ${error.messageText}`;
const errorText = `Cannot load custom tsconfig.json from provided path: ${tsconfigPath}, with error code: ${error.code}, message: ${error.messageText}`;
throw new Error(errorText);
}

Expand Down Expand Up @@ -231,9 +230,7 @@ export class Parser {
this.savePropValueAsString = Boolean(savePropValueAsString);
}

private getComponentFromExpression(
exp: ts.Symbol,
) {
private getComponentFromExpression(exp: ts.Symbol) {
const declaration = exp.valueDeclaration || exp.declarations![0];
const type = this.checker.getTypeOfSymbolAtLocation(exp, declaration);
const typeSymbol = type.symbol || type.aliasSymbol;
Expand All @@ -242,11 +239,11 @@ export class Parser {
return exp;
}

const symbolName = typeSymbol.getName()
const symbolName = typeSymbol.getName();

if (
(symbolName === "MemoExoticComponent" ||
symbolName === "ForwardRefExoticComponent") &&
(symbolName === 'MemoExoticComponent' ||
symbolName === 'ForwardRefExoticComponent') &&
exp.valueDeclaration &&
ts.isExportAssignment(exp.valueDeclaration) &&
ts.isCallExpression(exp.valueDeclaration.expression)
Expand Down Expand Up @@ -332,7 +329,9 @@ export class Parser {
const resolvedComponentName = componentNameResolver(nameSource, source);
const { description, tags } = this.findDocComment(commentSource);
const displayName =
resolvedComponentName || tags.visibleName || computeComponentName(nameSource, source);
resolvedComponentName ||
tags.visibleName ||
computeComponentName(nameSource, source);
const methods = this.getMethodsInfo(type);

if (propsType) {
Expand All @@ -352,15 +351,16 @@ export class Parser {
delete props[propName];
}
}

return {
tags,
description,
displayName,
methods,
props
};
} else if (description && displayName) {
return {
tags,
description,
displayName,
methods,
Expand Down Expand Up @@ -817,9 +817,9 @@ export class Parser {
let propMap = {};

if (properties) {
propMap = this.getPropMap(properties as ts.NodeArray<
ts.PropertyAssignment
>);
propMap = this.getPropMap(
properties as ts.NodeArray<ts.PropertyAssignment>
);
}

return {
Expand Down Expand Up @@ -849,9 +849,9 @@ export class Parser {
if (right) {
const { properties } = right as ts.ObjectLiteralExpression;
if (properties) {
propMap = this.getPropMap(properties as ts.NodeArray<
ts.PropertyAssignment
>);
propMap = this.getPropMap(
properties as ts.NodeArray<ts.PropertyAssignment>
);
}
}
});
Expand Down Expand Up @@ -954,31 +954,26 @@ export class Parser {
public getPropMap(
properties: ts.NodeArray<ts.PropertyAssignment | ts.BindingElement>
): StringIndexedObject<string | boolean | number | null> {
const propMap = properties.reduce(
(acc, property) => {
if (ts.isSpreadAssignment(property) || !property.name) {
return acc;
}
const propMap = properties.reduce((acc, property) => {
if (ts.isSpreadAssignment(property) || !property.name) {
return acc;
}

const literalValue = this.getLiteralValueFromPropertyAssignment(
property
);
const propertyName = getPropertyName(property.name);
const literalValue = this.getLiteralValueFromPropertyAssignment(property);
const propertyName = getPropertyName(property.name);

if (
(typeof literalValue === 'string' ||
typeof literalValue === 'number' ||
typeof literalValue === 'boolean' ||
literalValue === null) &&
propertyName !== null
) {
acc[propertyName] = literalValue;
}
if (
(typeof literalValue === 'string' ||
typeof literalValue === 'number' ||
typeof literalValue === 'boolean' ||
literalValue === null) &&
propertyName !== null
) {
acc[propertyName] = literalValue;
}

return acc;
},
{} as StringIndexedObject<string | boolean | number | null>
);
return acc;
}, {} as StringIndexedObject<string | boolean | number | null>);

return propMap;
}
Expand Down