Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Commit

Permalink
fix: codegen ts keyword (#1035)
Browse files Browse the repository at this point in the history
Co-authored-by: T6 <t6@t6.fyi>
  • Loading branch information
kratico and tjjfvi authored Jun 12, 2023
1 parent 4d762e3 commit b136ac9
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion util/normalize.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export function normalizeIdent(ident: string) {
if (ident.startsWith("r#")) ident = ident.slice(2)
return ident.replace(/(?:[^\p{ID_Continue}]|_)+(.)/gu, (_, $1: string) => $1.toUpperCase())
return normalizeKeyword(
ident.replace(/(?:[^\p{ID_Continue}]|_)+(.)/gu, (_, $1: string) => $1.toUpperCase()),
)
}

export function normalizeDocs(docs: string[] | undefined): string {
Expand All @@ -18,3 +20,55 @@ export function normalizeTypeName(name: string) {
export function normalizeVariableName(name: string) {
return normalizeIdent(name).replace(/^./, (x) => x.toLowerCase())
}

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#reserved_words
const keywords = [
"break",
"case",
"catch",
"class",
"const",
"continue",
"debugger",
"delete",
"do",
"else",
"export",
"extends",
"false",
"finally",
"for",
"function",
"if",
"import",
"in",
"instanceof",
"new",
"null",
"return",
"super",
"switch",
"this",
"throw",
"true",
"try",
"typeof",
"var",
"void",
"while",
"with",
"let",
"static",
"yield",
"await",
"enum",
"implements",
"interface",
"package",
"private",
"protected",
"public",
]
function normalizeKeyword(ident: string) {
return keywords.includes(ident) ? ident + "_" : ident
}

0 comments on commit b136ac9

Please sign in to comment.