Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle reserved words #45

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@alloy-js/typescript"
---

Handle reserved words in typescript naming policy
101 changes: 99 additions & 2 deletions packages/typescript/src/name-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,118 @@ export type TypeScriptElements =
| "interface-member"
| "type";

// Reserved words
const GLOBAL_RESERVED_WORDS = new Set([
"break",
"case",
"catch",
"class",
"const",
"continue",
"debugger",
"default",
"delete",
"do",
"else",
"enum",
"export",
"extends",
"false",
"finally",
"for",
"function",
"if",
"import",
"in",
"instanceof",
"new",
"null",
"return",
"super",
"switch",
"this",
"throw",
"true",
"try",
"typeof",
"var",
"void",
"while",
"with",
"as",
"implements",
"interface",
"let",
"package",
"private",
"protected",
"public",
"static",
"yield",
"await",
]);

const CONTEXT_SAFE_WORDS = new Set([
"delete",
"default",
"super",
"this",
"typeof",
"instanceof",
]);

/**
* Ensures a valid TypeScript identifier for the given element kind.
* @param name - The name to validate.
* @param element - The TypeScript element kind.
* @returns A TypeScript-safe name.
*/
function ensureNonReservedName(
name: string,
element: TypeScriptElements,
): string {
const suffix = "_";

// Global reserved words always need handling
if (GLOBAL_RESERVED_WORDS.has(name)) {
return `${name}${suffix}`;
}

// Context-safe reserved words for properties
if (
CONTEXT_SAFE_WORDS.has(name) &&
(element.includes("member") || element.includes("object-member"))
) {
return name; // Safe as properties
}

return name;
}

export function createTSNamePolicy(): NamePolicy<TypeScriptElements> {
const caseOptions = {
prefixCharacters: "$_",
suffixCharacters: "$_",
};

return createNamePolicy((name, element) => {
let transformedName: string;

switch (element) {
case "class":
case "type":
case "interface":
case "enum":
return pascalCase(name, caseOptions);
case "enum-member":
transformedName = pascalCase(name, caseOptions);
break;

default:
return camelCase(name, caseOptions);
transformedName = camelCase(name, caseOptions);
break;
}

return ensureNonReservedName(transformedName, element);
});
}

Expand Down
28 changes: 28 additions & 0 deletions packages/typescript/test/name-policy.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,31 @@ it("keeps _ and $ prefix", () => {
$oneTwo;
`);
});

it("appends _ to reserved words", () => {
const ref1 = refkey({});
const ref2 = refkey({});

const namePolicy = createTSNamePolicy();
const res = render(
<Output namePolicy={namePolicy}>
<ts.SourceFile path="test.ts">
<ts.FunctionDeclaration name="default" refkey={ref1} parameters={{"await": "any"}} />
<ts.VarDeclaration name="super" refkey={ref2}>
"hello"
</ts.VarDeclaration>
<ts.Reference refkey={ref1} />;
<ts.Reference refkey={ref2} />;
</ts.SourceFile>
</Output>,
);

expect(res.contents[0].contents).toEqual(d`
function default_(await_: any) {

}
const super_ = "hello";
default_;
super_;
`);
});
Loading