Skip to content

Commit 44f5e55

Browse files
committed
fix(typescript): Support type as expressions and type assertions
1 parent 13a8de9 commit 44f5e55

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/utils/__tests__/resolveToValue-test.js

+28-5
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import { parse } from '../../../tests/utils';
1111
import resolveToValue from '../resolveToValue';
1212

1313
describe('resolveToValue', () => {
14-
function parsePath(src) {
15-
const root = parse(src.trim());
14+
function parsePath(src, options = {}) {
15+
const root = parse(src.trim(), options);
1616
return root.get('body', root.node.body.length - 1, 'expression');
1717
}
1818

@@ -72,12 +72,35 @@ describe('resolveToValue', () => {
7272
expect(resolveToValue(path).node.type).toBe('FunctionDeclaration');
7373
});
7474

75-
it('resolves type cast expressions', () => {
76-
const path = parsePath(`
75+
describe('flow', () => {
76+
it('resolves type cast expressions', () => {
77+
const path = parsePath(`
7778
function foo() {}
7879
(foo: any);
7980
`);
80-
expect(resolveToValue(path).node.type).toBe('FunctionDeclaration');
81+
expect(resolveToValue(path).node.type).toBe('FunctionDeclaration');
82+
});
83+
});
84+
85+
describe('typescript', () => {
86+
const parseTypescript = src =>
87+
parsePath(src, { parserOptions: { plugins: ['typescript'] } });
88+
89+
it('resolves type as expressions', () => {
90+
const path = parseTypescript(`
91+
function foo() {}
92+
(foo as any);
93+
`);
94+
expect(resolveToValue(path).node.type).toBe('FunctionDeclaration');
95+
});
96+
97+
it('resolves type assertions', () => {
98+
const path = parseTypescript(`
99+
function foo() {}
100+
(<any> foo);
101+
`);
102+
expect(resolveToValue(path).node.type).toBe('FunctionDeclaration');
103+
});
81104
});
82105

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

src/utils/resolveToValue.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,11 @@ export default function resolveToValue(path: NodePath): NodePath {
137137
if (node.operator === '=') {
138138
return resolveToValue(path.get('right'));
139139
}
140-
} else if (t.TypeCastExpression.check(node)) {
140+
} else if (
141+
t.TypeCastExpression.check(node) ||
142+
t.TSAsExpression.check(node) ||
143+
t.TSTypeAssertion.check(node)
144+
) {
141145
return resolveToValue(path.get('expression'));
142146
} else if (t.Identifier.check(node)) {
143147
if (

0 commit comments

Comments
 (0)