Skip to content

Commit 46a75e5

Browse files
committed
additional fixes and cases
1 parent eff64d1 commit 46a75e5

File tree

4 files changed

+72
-7
lines changed

4 files changed

+72
-7
lines changed

src/__tests__/data/StatelessStaticComponentsNamedObjectExport.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ interface LabelProps {
55
title: string;
66
}
77

8-
/** StatelessStaticComponents.Label description */
8+
/** SubComponent description */
99
const SubComponent = (props: LabelProps) => (
1010
<div>My Property = {props.title}</div>
1111
);
@@ -20,7 +20,7 @@ const StatelessStaticComponents = (props: StatelessStaticComponentsProps) => (
2020
<div>My Property = {props.myProp}</div>
2121
);
2222

23-
export const Components = {
23+
export const Record = {
2424
StatelessStaticComponents,
2525
SubComponent
2626
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import * as React from 'react';
2+
3+
interface LabelProps {
4+
/** title description */
5+
title: string;
6+
}
7+
8+
/** SubComponent description */
9+
const SubComponent = (props: LabelProps) => (
10+
<div>My Property = {props.title}</div>
11+
);
12+
13+
interface StatelessStaticComponentsProps {
14+
/** myProp description */
15+
myProp: string;
16+
}
17+
18+
/** StatelessStaticComponents description */
19+
const StatelessStaticComponents = (props: StatelessStaticComponentsProps) => (
20+
<div>My Property = {props.myProp}</div>
21+
);
22+
23+
export const Record = {
24+
Comp1: StatelessStaticComponents,
25+
Comp2: SubComponent
26+
};

src/__tests__/parser.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,17 @@ describe('parser', () => {
278278
);
279279
});
280280

281+
it('should parse static sub components exported from named object with keys', () => {
282+
check('StatelessStaticComponentsNamedObjectExportAsKeys', {
283+
StatelessStaticComponents: {
284+
myProp: { type: 'string' }
285+
},
286+
SubComponent: {
287+
title: { type: 'string' }
288+
}
289+
});
290+
});
291+
281292
it('should parse static sub components on class components', () => {
282293
check('ColumnWithStaticComponents', {
283294
Column: {

src/parser.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,20 @@ export class Parser {
307307
}
308308

309309
private getComponentFromExpression(exp: ts.Symbol) {
310-
const declaration = exp.valueDeclaration || exp.declarations![0];
310+
let declaration = exp.valueDeclaration || exp.declarations![0];
311+
// Lookup component if it's a property assignment
312+
if (declaration && ts.isPropertyAssignment(declaration)) {
313+
if (ts.isIdentifier(declaration.initializer)) {
314+
const newSymbol = this.checker.getSymbolAtLocation(
315+
declaration.initializer
316+
);
317+
if (newSymbol) {
318+
exp = newSymbol;
319+
declaration = exp.valueDeclaration || exp.declarations![0];
320+
}
321+
}
322+
}
323+
311324
const type = this.checker.getTypeOfSymbolAtLocation(exp, declaration);
312325
const typeSymbol = type.symbol || type.aliasSymbol;
313326

@@ -1260,7 +1273,7 @@ function getTextValueOfFunctionProperty(
12601273
source: ts.SourceFile,
12611274
propertyName: string
12621275
) {
1263-
const [textValue] = source.statements
1276+
const identifierStatements: [ts.__String, string][] = source.statements
12641277
.filter(statement => ts.isExpressionStatement(statement))
12651278
.filter(statement => {
12661279
const expr = (statement as ts.ExpressionStatement)
@@ -1279,11 +1292,25 @@ function getTextValueOfFunctionProperty(
12791292
);
12801293
})
12811294
.map(statement => {
1282-
return (((statement as ts.ExpressionStatement)
1283-
.expression as ts.BinaryExpression).right as ts.Identifier).text;
1295+
const expressionStatement = (statement as ts.ExpressionStatement)
1296+
.expression as ts.BinaryExpression;
1297+
const name = ((expressionStatement.left as ts.PropertyAccessExpression)
1298+
.expression as ts.Identifier).escapedText;
1299+
const value = (expressionStatement.right as ts.Identifier).text;
1300+
return [name, value];
12841301
});
12851302

1286-
return textValue || '';
1303+
if (identifierStatements.length > 0) {
1304+
const locatedStatement = identifierStatements.find(
1305+
statement => statement[0] === exp.escapedName
1306+
);
1307+
if (locatedStatement) {
1308+
return locatedStatement[1];
1309+
}
1310+
return identifierStatements[0][1] || '';
1311+
}
1312+
1313+
return '';
12871314
}
12881315

12891316
function computeComponentName(
@@ -1292,6 +1319,7 @@ function computeComponentName(
12921319
customComponentTypes: ParserOptions['customComponentTypes'] = []
12931320
) {
12941321
const exportName = exp.getName();
1322+
12951323
const statelessDisplayName = getTextValueOfFunctionProperty(
12961324
exp,
12971325
source,

0 commit comments

Comments
 (0)