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

fix(typescript): Support type as expressions and type assertions #408

Merged
merged 1 commit into from Dec 11, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 28 additions & 5 deletions src/utils/__tests__/resolveToValue-test.js
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
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