Skip to content

Commit

Permalink
fix(typescript): Support type as expressions and type assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
danez committed Dec 11, 2019
1 parent 13a8de9 commit 44f5e55
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
33 changes: 28 additions & 5 deletions src/utils/__tests__/resolveToValue-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { parse } from '../../../tests/utils';
import resolveToValue from '../resolveToValue';

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

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

it('resolves type cast expressions', () => {
const path = parsePath(`
describe('flow', () => {
it('resolves type cast expressions', () => {
const path = parsePath(`
function foo() {}
(foo: any);
`);
expect(resolveToValue(path).node.type).toBe('FunctionDeclaration');
expect(resolveToValue(path).node.type).toBe('FunctionDeclaration');
});
});

describe('typescript', () => {
const parseTypescript = src =>
parsePath(src, { parserOptions: { plugins: ['typescript'] } });

it('resolves type as expressions', () => {
const path = parseTypescript(`
function foo() {}
(foo as any);
`);
expect(resolveToValue(path).node.type).toBe('FunctionDeclaration');
});

it('resolves type assertions', () => {
const path = parseTypescript(`
function foo() {}
(<any> foo);
`);
expect(resolveToValue(path).node.type).toBe('FunctionDeclaration');
});
});

describe('assignments', () => {
Expand Down
6 changes: 5 additions & 1 deletion src/utils/resolveToValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ export default function resolveToValue(path: NodePath): NodePath {
if (node.operator === '=') {
return resolveToValue(path.get('right'));
}
} else if (t.TypeCastExpression.check(node)) {
} else if (
t.TypeCastExpression.check(node) ||
t.TSAsExpression.check(node) ||
t.TSTypeAssertion.check(node)
) {
return resolveToValue(path.get('expression'));
} else if (t.Identifier.check(node)) {
if (
Expand Down

0 comments on commit 44f5e55

Please sign in to comment.