Skip to content

Commit

Permalink
feature: implement ParenthesizedType
Browse files Browse the repository at this point in the history
  • Loading branch information
mistlog committed Mar 10, 2021
1 parent 40d3b09 commit f0fb41e
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/generator/generator.ts
@@ -1,6 +1,6 @@

import * as t from "@babel/types";
import { IInferType, IIdentifier, ITypeExpression, ITypeIfStatement, IStringTypeLiteral, IStringType, INeverType, ITypeReference, ITypeCallExpression, INumberTypeLiteral, ITupleType, INumberType, IConditionalTypeExpression, ITemplateTypeLiteral, ITypeFile, IDeclaration, ITypeFunctionDeclaration, IUnionType, IKeyOfType, IIndexType, IArrayType, IFunctionType, IMappedTypeExpression, ITypeForInStatement, IIntersectionType, ITypeObjectProperty, IAnyType, IReadonlyArray, IOperatorType, IReadonlyTuple, IRestType, IObjectTypeLiteral, ITypeArrowFunctionExpression, ITypeExpressionParam, IParamList, IBigIntType, IImportDeclaration, ITypeVariableDeclaration } from "../parser";
import { IInferType, IIdentifier, ITypeExpression, ITypeIfStatement, IStringTypeLiteral, IStringType, INeverType, ITypeReference, ITypeCallExpression, INumberTypeLiteral, ITupleType, INumberType, IConditionalTypeExpression, ITemplateTypeLiteral, ITypeFile, IDeclaration, ITypeFunctionDeclaration, IUnionType, IKeyOfType, IIndexType, IArrayType, IFunctionType, IMappedTypeExpression, ITypeForInStatement, IIntersectionType, ITypeObjectProperty, IAnyType, IReadonlyArray, IOperatorType, IReadonlyTuple, IRestType, IObjectTypeLiteral, ITypeArrowFunctionExpression, ITypeExpressionParam, IParamList, IBigIntType, IImportDeclaration, ITypeVariableDeclaration, IParenthesizedType } from "../parser";

export function TSFile(ast: ITypeFile): t.File {
const body = ast.body.map(each => {
Expand Down Expand Up @@ -245,6 +245,7 @@ type TypeInTS<T extends ITypeType> =
Kind<T> extends Kind<IReadonlyTuple> ? t.TSTypeOperator :
Kind<T> extends Kind<IIndexType> ? t.TSIndexedAccessType :
Kind<T> extends Kind<IFunctionType> ? t.TSFunctionType :
Kind<T> extends Kind<IParenthesizedType> ? t.TSParenthesizedType :
Kind<T> extends Kind<IConditionalTypeExpression> ? t.TSConditionalType :
Kind<T> extends Kind<IMappedTypeExpression> ? t.TSMappedType :
Kind<T> extends Kind<ITypeCallExpression> ? t.TSConditionalType : t.TSType;
Expand Down Expand Up @@ -298,6 +299,7 @@ export function TSType(ast: ITypeType): TypeInTS<typeof ast> {
case "ReadonlyTuple": return tsTypeOperator(ast, "readonly");
case "IndexType": return tsIndexType(ast);
case "FunctionType": return tsFunctionType(ast);
case "ParenthesizedType": return t.tsParenthesizedType(TSType(ast.param));
case "RestType": return t.tsRestType(TSType(ast.param));

default:
Expand Down
23 changes: 23 additions & 0 deletions src/parser/expression/expression.tsx
Expand Up @@ -16,10 +16,12 @@ export type ITypeExpression =
| IOperatorType
| IIndexType
| IFunctionType
| IParenthesizedType

export function TypeExpression() {
return (
<or>
<ParenthesizedType />
<FunctionType />
<OperatorType />
<IndexType />
Expand All @@ -35,6 +37,27 @@ export function TypeExpression() {
)
}

export interface IParenthesizedType {
kind: "ParenthesizedType"
param: ITypeExpression
}

export function ParenthesizedType() {
const action = ({ param }): IParenthesizedType => {
return {
kind: "ParenthesizedType",
param
}
}
return (
<pattern action={action}>
{Text("(")}
<TypeExpression label="param" />
{Text(")")}
</pattern>
)
}

export interface ITypeArrowFunctionExpression {
kind: "TypeArrowFunctionExpression"
params: IParamList
Expand Down
2 changes: 2 additions & 0 deletions test/__snapshots__/generator.test.ts.snap
Expand Up @@ -133,6 +133,8 @@ exports[`ts-type ObjectType: spread 1`] = `
}, obj]>"
`;
exports[`ts-type ParenthesizedType 1`] = `"((value: number) => void)"`;
exports[`ts-type StringTypeLiteral 1`] = `"\\"\\\\n\\""`;
exports[`ts-type TemplateTypeLiteral 1`] = `"\`hello \${World}\`"`;
Expand Down
26 changes: 26 additions & 0 deletions test/__snapshots__/parser.test.tsx.snap
Expand Up @@ -502,6 +502,32 @@ Array [

exports[`ParamList: empty 1`] = `Array []`;

exports[`ParenthesizedType 1`] = `
Object {
"kind": "ParenthesizedType",
"param": Object {
"kind": "FunctionType",
"params": Array [
Object {
"kind": "FunctionTypeParam",
"name": Object {
"kind": "Identifier",
"name": "value",
},
"type": Object {
"kind": "NumberType",
"value": "number",
},
},
],
"returnType": Object {
"kind": "VoidType",
"value": "void",
},
},
}
`;

exports[`TemplateElement 1`] = `
Object {
"kind": "TemplateElement",
Expand Down
23 changes: 23 additions & 0 deletions test/assets/ast/ParenthesizedType.json
@@ -0,0 +1,23 @@
{
"kind": "ParenthesizedType",
"param": {
"kind": "FunctionType",
"params": [
{
"kind": "FunctionTypeParam",
"name": {
"kind": "Identifier",
"name": "value"
},
"type": {
"kind": "NumberType",
"value": "number"
}
}
],
"returnType": {
"kind": "VoidType",
"value": "void"
}
}
}
1 change: 1 addition & 0 deletions test/assets/ts-type/ParenthesizedType
@@ -0,0 +1 @@
((value: number) => void)
5 changes: 5 additions & 0 deletions test/generator.test.ts
Expand Up @@ -69,6 +69,11 @@ describe("ts-type", () => {
return code;
}

test("ParenthesizedType", () => {
const name = "ParenthesizedType";
expect(toCode(name)).toMatchSnapshot();
})

test("MappedType", () => {
const name = "MappedType";
expect(toCode(name)).toMatchSnapshot();
Expand Down
7 changes: 7 additions & 0 deletions test/parser.test.tsx
Expand Up @@ -474,6 +474,13 @@ test("OperatorType: keyof", () => {
expect(ast).toMatchSnapshot();
})

test("ParenthesizedType", () => {
const parser = ReactPeg.render(<TypeExpression />);
const ast = parser.parse(`(type (value: number) => void)`);
saveAST(ast, "ParenthesizedType.json");
expect(ast).toMatchSnapshot();
})

test("IndexType", () => {
const parser = ReactPeg.render(<IndexType />);
const ast = parser.parse(`obj["a"]`);
Expand Down

0 comments on commit f0fb41e

Please sign in to comment.