This repository has been archived by the owner on Feb 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support conditional type and infer type (#94)
* feat: support conditional type * feat: support infer type * test: add tests * refactor: fix linting
- Loading branch information
Showing
13 changed files
with
203 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import * as ts from 'typescript'; | ||
import { parse_native } from '../parse'; | ||
import { | ||
create_conditional_type, | ||
IConditionalType, | ||
} from '../types/conditional-type'; | ||
|
||
export const parse_conditional_type = ( | ||
node: ts.ConditionalTypeNode, | ||
): IConditionalType => | ||
create_conditional_type({ | ||
check: parse_native(node.checkType), | ||
extends: parse_native(node.extendsType), | ||
true: parse_native(node.trueType), | ||
false: parse_native(node.falseType), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import * as ts from 'typescript'; | ||
import { IGenericDeclaration } from '../declarations/generic-declaration'; | ||
import { parse_native } from '../parse'; | ||
import { create_infer_type, IInferType } from '../types/infer-type'; | ||
|
||
export const parse_infer_type = (node: ts.InferTypeNode): IInferType => | ||
create_infer_type({ | ||
generic: parse_native(node.typeParameter) as IGenericDeclaration, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`should return correctly 1`] = `"T extends string ? boolean : number"`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`should return correctly 1`] = `"infer T"`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { boolean_type, number_type, string_type } from '../../constants'; | ||
import { emit } from '../../emit'; | ||
import { | ||
create_conditional_type, | ||
is_conditional_type, | ||
} from '../conditional-type'; | ||
import { create_general_type } from '../general-type'; | ||
|
||
it('should return correctly', () => { | ||
expect( | ||
emit( | ||
create_conditional_type({ | ||
check: create_general_type({ name: 'T' }), | ||
extends: string_type, | ||
true: boolean_type, | ||
false: number_type, | ||
}), | ||
), | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
describe('is_conditional_type', () => { | ||
it('should return correctly', () => { | ||
const element = create_conditional_type({ | ||
check: create_general_type({ name: 'T' }), | ||
extends: string_type, | ||
true: boolean_type, | ||
false: number_type, | ||
}); | ||
expect(is_conditional_type(element)).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { boolean_type, number_type, string_type } from '../../constants'; | ||
import { create_generic_declaration } from '../../declarations/generic-declaration'; | ||
import { emit } from '../../emit'; | ||
import { create_infer_type, is_infer_type } from '../infer-type'; | ||
|
||
it('should return correctly', () => { | ||
expect( | ||
emit( | ||
create_infer_type({ | ||
generic: create_generic_declaration({ name: 'T' }), | ||
}), | ||
), | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
describe('is_infer_type', () => { | ||
it('should return correctly', () => { | ||
const element = create_infer_type({ | ||
generic: create_generic_declaration({ name: 'T' }), | ||
}); | ||
expect(is_infer_type(element)).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import * as ts from 'typescript'; | ||
import { IType } from '../collections'; | ||
import { ElementKind } from '../constants'; | ||
import { | ||
create_element, | ||
is_element, | ||
IElement, | ||
IElementOptions, | ||
} from '../element'; | ||
import { transform } from '../transform'; | ||
|
||
export interface IConditionalTypeOptions extends IElementOptions { | ||
check: IType; | ||
extends: IType; | ||
true: IType; | ||
false: IType; | ||
} | ||
|
||
export interface IConditionalType | ||
extends IElement<ElementKind.ConditionalType>, | ||
IConditionalTypeOptions {} | ||
|
||
export const create_conditional_type = ( | ||
options: IConditionalTypeOptions, | ||
): IConditionalType => ({ | ||
...create_element(ElementKind.ConditionalType), | ||
...options, | ||
}); | ||
|
||
export const is_conditional_type = (value: any): value is IConditionalType => | ||
is_element(value) && value.kind === ElementKind.ConditionalType; | ||
|
||
/** | ||
* @hidden | ||
*/ | ||
export const transform_conditional_type = ( | ||
element: IConditionalType, | ||
path: IElement<any>[], | ||
) => | ||
ts.createConditionalTypeNode( | ||
/* checkType */ transform(element.check, path) as ts.TypeNode, | ||
/* extendsType */ transform(element.extends, path) as ts.TypeNode, | ||
/* trueType */ transform(element.true, path) as ts.TypeNode, | ||
/* falseType */ transform(element.false, path) as ts.TypeNode, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import * as ts from 'typescript'; | ||
import { ElementKind } from '../constants'; | ||
import { | ||
transform_generic_declaration, | ||
IGenericDeclaration, | ||
} from '../declarations/generic-declaration'; | ||
import { | ||
create_element, | ||
is_element, | ||
IElement, | ||
IElementOptions, | ||
} from '../element'; | ||
|
||
export interface IInferTypeOptions extends IElementOptions { | ||
generic: IGenericDeclaration; | ||
} | ||
|
||
export interface IInferType | ||
extends IElement<ElementKind.InferType>, | ||
IInferTypeOptions {} | ||
|
||
export const create_infer_type = (options: IInferTypeOptions): IInferType => ({ | ||
...create_element(ElementKind.InferType), | ||
...options, | ||
}); | ||
|
||
export const is_infer_type = (value: any): value is IInferType => | ||
is_element(value) && value.kind === ElementKind.InferType; | ||
|
||
/** | ||
* @hidden | ||
*/ | ||
export const transform_infer_type = ( | ||
element: IInferType, | ||
path: IElement<any>[], | ||
) => | ||
ts.createInferTypeNode( | ||
/* typeParameter */ transform_generic_declaration(element.generic, path), | ||
); |