Skip to content

Commit

Permalink
feature: implement optional function param
Browse files Browse the repository at this point in the history
  • Loading branch information
mistlog committed Mar 12, 2021
1 parent 899a647 commit 8f9cfb1
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/generator/generator.ts
Expand Up @@ -194,6 +194,9 @@ function tsFunctionParams(params: IFunctionTypeParam[]) {
const identifier = Identifier(each.name);
const param = each.rest ? t.restElement(identifier) : identifier;
param.typeAnnotation = t.tsTypeAnnotation(TSType(each.type));
if (each.optional) {
param["optional"] = true
}
return param;
});
}
Expand Down
10 changes: 9 additions & 1 deletion src/parser/function/function.tsx
Expand Up @@ -69,10 +69,11 @@ export interface IFunctionTypeParam {
name: IIdentifier
type: ITypeExpression
rest?: boolean
optional?: boolean
}

export function FunctionTypeParam() {
const action = ({ name, type, rest }): IFunctionTypeParam => {
const action = ({ name, type, rest, optional }): IFunctionTypeParam => {
const param: IFunctionTypeParam = {
kind: "FunctionTypeParam",
name,
Expand All @@ -83,6 +84,10 @@ export function FunctionTypeParam() {
param.rest = true
}

if (optional) {
param.optional = true
}

return param
}

Expand All @@ -93,6 +98,9 @@ export function FunctionTypeParam() {
<text>...</text>
</opt>
<Identifier label="name" />
<opt label="optional">
{Text("?")}
</opt>
<opt label="type">
<pattern action={({ expression }) => expression}>
{Text(":")}
Expand Down
2 changes: 2 additions & 0 deletions test/__snapshots__/generator.test.ts.snap
Expand Up @@ -90,6 +90,8 @@ exports[`ts-type FunctionType 1`] = `"() => void"`;
exports[`ts-type FunctionType: nested 1`] = `"() => (a: number, b: string) => void"`;
exports[`ts-type FunctionType: optional param 1`] = `"(a: number, b?: string) => number"`;
exports[`ts-type FunctionType: params 1`] = `"(a: number, b: string) => number"`;
exports[`ts-type FunctionType: rest 1`] = `"(...args: any[]) => void"`;
Expand Down
32 changes: 32 additions & 0 deletions test/assets/ast/FunctionType-OptionalParam.json
@@ -0,0 +1,32 @@
{
"kind": "FunctionType",
"params": [
{
"kind": "FunctionTypeParam",
"name": {
"kind": "Identifier",
"name": "a"
},
"type": {
"kind": "NumberType",
"value": "number"
}
},
{
"kind": "FunctionTypeParam",
"name": {
"kind": "Identifier",
"name": "b"
},
"type": {
"kind": "StringType",
"value": "string"
},
"optional": true
}
],
"returnType": {
"kind": "NumberType",
"value": "number"
}
}
1 change: 1 addition & 0 deletions test/assets/ts-type/FunctionType-OptionalParam
@@ -0,0 +1 @@
(a: number, b?: string) => number
5 changes: 5 additions & 0 deletions test/generator.test.ts
Expand Up @@ -88,6 +88,11 @@ describe("ts-type", () => {
expect(toCode(name)).toMatchSnapshot();
})

test("FunctionType: optional param", () => {
const name = "FunctionType-OptionalParam";
expect(toCode(name)).toMatchSnapshot();
})

test("FunctionType: params", () => {
const name = "FunctionType-Params";
expect(toCode(name)).toMatchSnapshot();
Expand Down
35 changes: 35 additions & 0 deletions test/parser/__snapshots__/function.test.tsx.snap
Expand Up @@ -49,6 +49,41 @@ Object {
}
`;

exports[`parser: function FunctionType: optional param 1`] = `
Object {
"kind": "FunctionType",
"params": Array [
Object {
"kind": "FunctionTypeParam",
"name": Object {
"kind": "Identifier",
"name": "a",
},
"type": Object {
"kind": "NumberType",
"value": "number",
},
},
Object {
"kind": "FunctionTypeParam",
"name": Object {
"kind": "Identifier",
"name": "b",
},
"optional": true,
"type": Object {
"kind": "StringType",
"value": "string",
},
},
],
"returnType": Object {
"kind": "NumberType",
"value": "number",
},
}
`;

exports[`parser: function FunctionType: params 1`] = `
Object {
"kind": "FunctionType",
Expand Down
7 changes: 7 additions & 0 deletions test/parser/function.test.tsx
Expand Up @@ -10,6 +10,13 @@ describe("parser: function", () => {
expect(ast).toMatchSnapshot();
})

test("FunctionType: optional param", () => {
const parser = ReactPeg.render(<TypeExpression />);
const ast = parser.parse(`type (a:number, b?: string) => number`);
saveAST(ast, "FunctionType-OptionalParam.json");
expect(ast).toMatchSnapshot();
})

test("FunctionType: rest", () => {
const parser = ReactPeg.render(<TypeExpression />);
const ast = parser.parse(`type (...args: any[]) => void`);
Expand Down

0 comments on commit 8f9cfb1

Please sign in to comment.