diff --git a/src/__tests__/__snapshots__/parse.ts.snap b/src/__tests__/__snapshots__/parse.ts.snap index 45391ca..c07c483 100644 --- a/src/__tests__/__snapshots__/parse.ts.snap +++ b/src/__tests__/__snapshots__/parse.ts.snap @@ -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`] = ` "/// /// diff --git a/src/__tests__/parse.ts b/src/__tests__/parse.ts index 3469f9e..6ef0e9c 100644 --- a/src/__tests__/parse.ts +++ b/src/__tests__/parse.ts @@ -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 = ` /// @@ -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(` + (x: T, y: U) => T | U | void + `), + ).toMatchSnapshot(); + }); +}); diff --git a/src/parse.ts b/src/parse.ts index 5a4bf71..0a4a856 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -1,4 +1,5 @@ import * as ts from 'typescript'; +import { IType } from './collections'; import { any_type, boolean_type, @@ -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'; @@ -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!; +};