Skip to content
This repository has been archived by the owner on Feb 20, 2020. It is now read-only.

Commit

Permalink
feat(utils): add parse_type() (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatyang authored Aug 2, 2017
1 parent b61a3e0 commit ae0192f
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
73 changes: 73 additions & 0 deletions src/__tests__/__snapshots__/parse.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,78 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`parse_type should return correctly 1`] = `
Object {
"generics": Array [
Object {
"defalut": undefined,
"extends": undefined,
"kind": 13,
"name": "T",
},
Object {
"defalut": undefined,
"extends": undefined,
"kind": 13,
"name": "U",
},
],
"kind": 11,
"parameters": Array [
Object {
"kind": 32,
"name": "x",
"optional": undefined,
"rest": undefined,
"type": Object {
"generics": undefined,
"kind": 12,
"name": "T",
"parents": undefined,
},
},
Object {
"kind": 32,
"name": "y",
"optional": undefined,
"rest": undefined,
"type": Object {
"generics": undefined,
"kind": 12,
"name": "U",
"parents": undefined,
},
},
],
"return": Object {
"kind": 41,
"types": Array [
Object {
"generics": undefined,
"kind": 12,
"name": "T",
"parents": undefined,
},
Object {
"generics": undefined,
"kind": 12,
"name": "U",
"parents": undefined,
},
Object {
"kind": 29,
"type": TokenObject {
"end": -1,
"flags": 8,
"kind": 105,
"parent": undefined,
"pos": -1,
},
},
],
},
}
`;

exports[`should return correctly 1`] = `
"/// <reference path=\\"some-path\\" />
/// <reference types=\\"some-types\\" />
Expand Down
12 changes: 11 additions & 1 deletion src/__tests__/parse.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as ts from 'typescript';
import { emit } from '../emit';
import { parse, parse_native } from '../parse';
import { parse, parse_native, parse_type } from '../parse';

const code = `
/// <reference path="some-path" />
Expand Down Expand Up @@ -138,3 +138,13 @@ it('should return correctly', () => {
it('should throw error with unexpected kind', () => {
expect(() => parse_native(ts.createToken(-1))).toThrowError();
});

describe('parse_type', () => {
it('should return correctly', () => {
expect(
parse_type(`
<T, U>(x: T, y: U) => T | U | void
`),
).toMatchSnapshot();
});
});
9 changes: 9 additions & 0 deletions src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as ts from 'typescript';
import { IType } from './collections';
import {
any_type,
boolean_type,
Expand All @@ -12,6 +13,7 @@ import {
undefined_type,
void_type,
} from './constants';
import { ITypeDeclaration } from './declarations/type-declaration';
import { IElement } from './element';
import { emit_native } from './emit';
import { ITopLevelElement } from './others/top-level-element';
Expand Down Expand Up @@ -132,3 +134,10 @@ export const parse = (code: string) =>
/*setParentNodes */ false,
),
) as ITopLevelElement;

export const parse_type = (type_string: string) => {
const code = `type X = ${type_string}`;
const top_level_element = parse(code);
const type_declaration = top_level_element.members[0] as ITypeDeclaration;
return type_declaration.type!;
};

0 comments on commit ae0192f

Please sign in to comment.