Repository: alloy-framework/alloy
Pull request #24 (2024-09-18 17:47:36)

Add optional prop to parameter descriptor

pkg-pr-new[bot]: 
[Open in Stackblitz](https://pkg.pr.new/template/7b6ca38b-4665-42d0-8f44-8b21c52c376f)


<details><summary><b>@alloy-js/babel-plugin-alloy</b></summary><p>

```
pnpm add https://pkg.pr.new/alloy-framework/alloy/@alloy-js/babel-plugin-alloy@24
```

</p></details>
      
    

<details><summary><b>babel-preset-alloy</b></summary><p>

```
pnpm add https://pkg.pr.new/alloy-framework/alloy/babel-preset-alloy@24
```

</p></details>
      
    

<details><summary><b>@alloy-js/babel-plugin-jsx-dom-expressions</b></summary><p>

```
pnpm add https://pkg.pr.new/alloy-framework/alloy/@alloy-js/babel-plugin-jsx-dom-expressions@24
```

</p></details>
      
    

<details><summary><b>@alloy-js/csharp</b></summary><p>

```
pnpm add https://pkg.pr.new/alloy-framework/alloy/@alloy-js/csharp@24
```

</p></details>
      
    

<details><summary><b>@alloy-js/core</b></summary><p>

```
pnpm add https://pkg.pr.new/alloy-framework/alloy/@alloy-js/core@24
```

</p></details>
      
    

<details><summary><b>@alloy-js/java</b></summary><p>

```
pnpm add https://pkg.pr.new/alloy-framework/alloy/@alloy-js/java@24
```

</p></details>
      
    

<details><summary><b>@alloy-js/prettier-plugin-alloy</b></summary><p>

```
pnpm add https://pkg.pr.new/alloy-framework/alloy/@alloy-js/prettier-plugin-alloy@24
```

</p></details>
      
    

<details><summary><b>@alloy-js/typescript</b></summary><p>

```
pnpm add https://pkg.pr.new/alloy-framework/alloy/@alloy-js/typescript@24
```

</p></details>
      
    

_commit: <a href="https://github.com/alloy-framework/alloy/runs/30333638029"><code>5535a9c</code></a>_


Patch:
diff --git a/packages/typescript/src/components/FunctionDeclaration.tsx b/packages/typescript/src/components/FunctionDeclaration.tsx
index 3458706..ac13770 100644
--- a/packages/typescript/src/components/FunctionDeclaration.tsx
+++ b/packages/typescript/src/components/FunctionDeclaration.tsx
@@ -18,6 +18,7 @@ import { Name } from "./Name.js";
 export interface ParameterDescriptor {
   type: Children;
   refkey: Refkey;
+  optional?: boolean;
 }
 
 function isParameterDescriptor(
@@ -101,13 +102,14 @@ FunctionDeclaration.Parameters = taggedComponent(
               refkey: refkey(),
               type: value,
             };
+          const optionality = descriptor.optional ? "?" : "";
           const sym = createTSSymbol({
             name: key,
             refkey: descriptor.refkey,
             flags: TSSymbolFlags.ParameterSymbol,
           });
 
-          return <>{namePolicy.getName(sym.name, "parameter")}: {descriptor.type}</>;
+          return <>{namePolicy.getName(sym.name, "parameter")}{optionality}: {descriptor.type}</>;
         },
         { joiner: ", " },
       );


Test patch:
diff --git a/packages/typescript/test/function-declaration.test.tsx b/packages/typescript/test/function-declaration.test.tsx
index 58649bd..f98db0f 100644
--- a/packages/typescript/test/function-declaration.test.tsx
+++ b/packages/typescript/test/function-declaration.test.tsx
@@ -1,7 +1,11 @@
 import { Props, refkey } from "@alloy-js/core";
 import { d } from "@alloy-js/core/testing";
 import { describe, expect, it } from "vitest";
-import { FunctionDeclaration, VarDeclaration } from "../src/index.js";
+import {
+  FunctionDeclaration,
+  ParameterDescriptor,
+  VarDeclaration,
+} from "../src/index.js";
 import { toSourceText } from "./utils.js";
 
 it("works", () => {
@@ -174,4 +178,24 @@ describe("symbols", () => {
       }
     `);
   });
+
+  it("create optional parameters", () => {
+    const paramDesc: ParameterDescriptor = {
+      refkey: refkey(),
+      type: "any",
+      optional: true,
+    };
+    const decl =
+      <>
+        <FunctionDeclaration name="foo" parameters={{foo: paramDesc}}>
+          console.log(foo);
+        </FunctionDeclaration>
+      </>;
+
+    expect(toSourceText(decl)).toBe(d`
+      function foo(foo?: any) {
+        console.log(foo);
+      }
+    `);
+  });
 });