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

Add null as reserved word in enum emit, other follow-ups #657

Merged
merged 1 commit into from
Oct 18, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/util/isIdentifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const RESERVED_WORDS = new Set([
"else",
"export",
"extends",
"false",
"finally",
"for",
"function",
Expand All @@ -35,7 +34,6 @@ const RESERVED_WORDS = new Set([
"switch",
"this",
"throw",
"true",
"try",
"typeof",
"var",
Expand All @@ -54,8 +52,19 @@ const RESERVED_WORDS = new Set([
"public",
"static",
"await",
// Literals that cannot be used as identifiers
"false",
"null",
"true",
]);

/**
* Determine if the given name is a legal variable name.
*
* This is needed when transforming TypeScript enums; if an enum key is a valid
* variable name, it might be referenced later in the enum, so we need to
* declare a variable.
*/
export default function isIdentifier(name: string): boolean {
if (name.length === 0) {
return false;
Expand Down
21 changes: 21 additions & 0 deletions test/typescript-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,27 @@ describe("typescript transform", () => {
);
});

it("handles reserved literals when transforming enums", () => {
assertTypeScriptESMResult(
`
enum Foo {
true,
false,
null,
undefined
}
`,
`
var Foo; (function (Foo) {
Foo[Foo["true"] = 0] = "true";
Foo[Foo["false"] = Foo["true"] + 1] = "false";
Foo[Foo["null"] = Foo["false"] + 1] = "null";
const undefined = Foo["null"] + 1; Foo[Foo["undefined"] = undefined] = "undefined";
})(Foo || (Foo = {}));
`,
);
});

it("removes functions without bodies", () => {
assertTypeScriptResult(
`
Expand Down