Repository: alloy-framework/alloy
Pull request #20 (2024-09-11 19:00:11)

Keep _ and $ in name prefix and suffix in TS name policy

pkg-pr-new[bot]: 
[Open in Stackblitz](https://pkg.pr.new/template/244fd997-2344-410f-b46d-4dd8ea224127)


<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@20
```

</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@20
```

</p></details>
      
    

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

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

</p></details>
      
    

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

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

</p></details>
      
    

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

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

</p></details>
      
    

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

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

</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@20
```

</p></details>
      
    

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

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

</p></details>
      
    

_commit: <a href="https://github.com/alloy-framework/alloy/runs/30012533830"><code>5fa2f13</code></a>_


Patch:
diff --git a/packages/typescript/src/name-policy.ts b/packages/typescript/src/name-policy.ts
index 9ee7c8b..053d99c 100644
--- a/packages/typescript/src/name-policy.ts
+++ b/packages/typescript/src/name-policy.ts
@@ -17,15 +17,19 @@ export type TypeScriptElements =
   | "type";
 
 export function createTSNamePolicy(): NamePolicy<TypeScriptElements> {
+  const caseOptions = {
+    prefixCharacters: "$_",
+    suffixCharacters: "$_",
+  };
   return createNamePolicy((name, element) => {
     switch (element) {
       case "class":
       case "type":
       case "interface":
       case "enum":
-        return pascalCase(name);
+        return pascalCase(name, caseOptions);
       default:
-        return camelCase(name);
+        return camelCase(name, caseOptions);
     }
   });
 }


Test patch:
diff --git a/packages/typescript/test/name-policy.test.tsx b/packages/typescript/test/name-policy.test.tsx
index 7696d4f..f0b6c99 100644
--- a/packages/typescript/test/name-policy.test.tsx
+++ b/packages/typescript/test/name-policy.test.tsx
@@ -32,3 +32,31 @@ it("applies to functions and variables", () => {
     oneTwo;
   `);
 });
+
+it("keeps _ and $ prefix", () => {
+  const ref1 = refkey({});
+  const ref2 = refkey({});
+
+  const namePolicy = createTSNamePolicy();
+  const res = render(
+    <Output namePolicy={namePolicy}>
+      <ts.SourceFile path="test.ts">
+        <ts.FunctionDeclaration name="_foo-bar" refkey={ref1} />
+        <ts.VarDeclaration name="$one-two" refkey={ref2}>
+          "hello"
+        </ts.VarDeclaration>
+        <ts.Reference refkey={ref1} />;
+        <ts.Reference refkey={ref2} />;
+      </ts.SourceFile>
+    </Output>,
+  );
+
+  expect(res.contents[0].contents).toEqual(d`
+    function _fooBar() {
+      
+    }
+    const $oneTwo = "hello";
+    _fooBar;
+    $oneTwo;
+  `);
+});