Skip to content

Commit 91c8936

Browse files
authored
feat: Support type cast expression when detecting components (#279)
1 parent 081d3fe commit 91c8936

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/utils/__tests__/resolveToValue-test.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import * as utils from '../../../tests/utils';
1818

1919
describe('resolveToValue', () => {
2020
function parse(src) {
21-
const root = utils.parse(src);
21+
const root = utils.parse(src.trim());
2222
return root.get('body', root.node.body.length - 1, 'expression');
2323
}
2424

@@ -61,23 +61,27 @@ describe('resolveToValue', () => {
6161
});
6262

6363
it('resolves to class declarations', () => {
64-
const program = utils.parse(`
64+
const path = parse(`
6565
class Foo {}
6666
Foo;
6767
`);
68-
expect(resolveToValue(program.get('body', 1, 'expression')).node.type).toBe(
69-
'ClassDeclaration',
70-
);
68+
expect(resolveToValue(path).node.type).toBe('ClassDeclaration');
7169
});
7270

7371
it('resolves to class function declaration', () => {
74-
const program = utils.parse(`
72+
const path = parse(`
7573
function foo() {}
7674
foo;
7775
`);
78-
expect(resolveToValue(program.get('body', 1, 'expression')).node.type).toBe(
79-
'FunctionDeclaration',
80-
);
76+
expect(resolveToValue(path).node.type).toBe('FunctionDeclaration');
77+
});
78+
79+
it('resolves type cast expressions', () => {
80+
const path = parse(`
81+
function foo() {}
82+
(foo: any);
83+
`);
84+
expect(resolveToValue(path).node.type).toBe('FunctionDeclaration');
8185
});
8286

8387
describe('assignments', () => {

src/utils/resolveToValue.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ export default function resolveToValue(path: NodePath): NodePath {
141141
if (node.operator === '=') {
142142
return resolveToValue(path.get('right'));
143143
}
144+
} else if (types.TypeCastExpression.check(node)) {
145+
return resolveToValue(path.get('expression'));
144146
} else if (types.Identifier.check(node)) {
145147
if (
146148
(types.ClassDeclaration.check(path.parentPath.node) ||

0 commit comments

Comments
 (0)