Skip to content

Commit

Permalink
feature: add constraint in generator
Browse files Browse the repository at this point in the history
  • Loading branch information
mistlog committed Feb 16, 2021
1 parent 7445ed9 commit d8e4771
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/generator/generator.ts
Expand Up @@ -20,9 +20,11 @@ export type ITypeType = ITypeExpression
export function TSTypeAliasDeclarationWithParams(ast: ITypeFunctionDeclaration): t.TSTypeAliasDeclaration | t.ExportNamedDeclaration {
const _type = TSTypeAliasDeclaration(ast);
const type = t.isExportNamedDeclaration(_type) ? _type.declaration as t.TSTypeAliasDeclaration : _type;
const params = ast.declarator.initializer.params.map((param) =>
t.tSTypeParameter(null, null, (param as ITypeReference).typeName.name)
);
const params = ast.declarator.initializer.params.map((param) => {
const constraint = param.constraint ? TSType(param.constraint) : null;
const tsParam = t.tSTypeParameter(constraint , /* TODO */null, (param as ITypeReference).typeName.name)
return tsParam;
});

type.typeParameters = t.tsTypeParameterDeclaration(params);
return _type;
Expand Down
2 changes: 2 additions & 0 deletions test/__snapshots__/generator.test.ts.snap
Expand Up @@ -74,6 +74,8 @@ exports[`examples parseUserInfo 1`] = `
};"
`;
exports[`examples pick 1`] = `"export type pick<T, Keys extends keyof T> = { [K in Keys]: T[K] };"`;
exports[`ts-type ArrayType 1`] = `"string[]"`;
exports[`ts-type ArrayType: deep 1`] = `"string[][]"`;
Expand Down
87 changes: 87 additions & 0 deletions test/__snapshots__/parser.test.tsx.snap
Expand Up @@ -2919,3 +2919,90 @@ Object {
"kind": "TypeFunctionDeclaration",
}
`;

exports[`examples pick 1`] = `
Object {
"declarator": Object {
"initializer": Object {
"body": Object {
"body": Object {
"as": Object {
"kind": "TypeReference",
"typeName": Object {
"kind": "Identifier",
"name": "K",
},
},
"key": Object {
"kind": "Identifier",
"name": "K",
},
"keys": Object {
"kind": "TypeReference",
"typeName": Object {
"kind": "Identifier",
"name": "Keys",
},
},
"kind": "TypeForInStatement",
"value": Object {
"head": Object {
"kind": "TypeReference",
"typeName": Object {
"kind": "Identifier",
"name": "T",
},
},
"kind": "IndexType",
"members": Array [
Object {
"kind": "TypeReference",
"typeName": Object {
"kind": "Identifier",
"name": "K",
},
},
],
},
},
"kind": "MappedTypeExpression",
},
"kind": "TypeArrowFunctionExpression",
"params": Array [
Object {
"constraint": null,
"kind": "TypeReference",
"typeName": Object {
"kind": "Identifier",
"name": "T",
},
},
Object {
"constraint": Object {
"kind": "KeyOfType",
"operand": Object {
"kind": "TypeReference",
"typeName": Object {
"kind": "Identifier",
"name": "T",
},
},
},
"kind": "TypeReference",
"typeName": Object {
"kind": "Identifier",
"name": "Keys",
},
},
],
},
"kind": "TypeFunctionDeclarator",
"name": Object {
"kind": "Identifier",
"name": "pick",
},
},
"export": true,
"kind": "TypeFunctionDeclaration",
}
`;
84 changes: 84 additions & 0 deletions test/assets/ast/Example-pick.json
@@ -0,0 +1,84 @@
{
"kind": "TypeFunctionDeclaration",
"declarator": {
"kind": "TypeFunctionDeclarator",
"name": {
"kind": "Identifier",
"name": "pick"
},
"initializer": {
"kind": "TypeArrowFunctionExpression",
"params": [
{
"kind": "TypeReference",
"typeName": {
"kind": "Identifier",
"name": "T"
},
"constraint": null
},
{
"kind": "TypeReference",
"typeName": {
"kind": "Identifier",
"name": "Keys"
},
"constraint": {
"kind": "KeyOfType",
"operand": {
"kind": "TypeReference",
"typeName": {
"kind": "Identifier",
"name": "T"
}
}
}
}
],
"body": {
"kind": "MappedTypeExpression",
"body": {
"kind": "TypeForInStatement",
"key": {
"kind": "Identifier",
"name": "K"
},
"keys": {
"kind": "TypeReference",
"typeName": {
"kind": "Identifier",
"name": "Keys"
}
},
"as": {
"kind": "TypeReference",
"typeName": {
"kind": "Identifier",
"name": "K"
}
},
"value": {
"kind": "IndexType",
"head": {
"kind": "TypeReference",
"typeName": {
"kind": "Identifier",
"name": "T"
}
},
"members": [
{
"kind": "TypeReference",
"typeName": {
"kind": "Identifier",
"name": "K"
}
}
]
}
}
}
}
},
"export": true
}
1 change: 1 addition & 0 deletions test/assets/ts-type/Example-pick
@@ -0,0 +1 @@
export type pick<T, Keys extends keyof T> = { [K in Keys]: T[K] };
8 changes: 8 additions & 0 deletions test/assets/typetype/Example-pick
@@ -0,0 +1,8 @@
export type function pick = (T, Keys extends keyof T) => ^{
for(K in Keys) {
return {
key: K,
value: T[K]
}
}
}
5 changes: 5 additions & 0 deletions test/generator.test.ts
Expand Up @@ -173,6 +173,11 @@ describe("examples", () => {
return code;
}

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

test("parseURL", () => {
const name = "Example-parseURL";
expect(toCode(name)).toMatchSnapshot();
Expand Down
5 changes: 5 additions & 0 deletions test/parser.test.tsx
Expand Up @@ -577,6 +577,11 @@ describe("examples", () => {
return ast;
}

test("pick", () => {
const name = "Example-pick";
expect(toAST(name)).toMatchSnapshot();
})

test("parseURL", () => {
const name = "Example-parseURL";
expect(toAST(name)).toMatchSnapshot();
Expand Down

0 comments on commit d8e4771

Please sign in to comment.