Skip to content

[tsgen] Quote non-identifier enum value names in module entry.#26814

Open
soilSpoon wants to merge 3 commits intoemscripten-core:mainfrom
soilSpoon:fix-tsgen-hyphen-enum-keys
Open

[tsgen] Quote non-identifier enum value names in module entry.#26814
soilSpoon wants to merge 3 commits intoemscripten-core:mainfrom
soilSpoon:fix-tsgen-hyphen-enum-keys

Conversation

@soilSpoon
Copy link
Copy Markdown

@soilSpoon soilSpoon commented Apr 29, 2026

enum_value_type::string (added in #25257) accepts arbitrary JS-facing names via .value("name", ...). The type union path quotes these correctly as string literals, but printModuleEntry emits them as bare object keys, producing invalid TypeScript when the name is not a valid JS identifier.

enum_<Foo>("Foo", enum_value_type::string).value("a-b", Foo::A);

Before:

Foo: { a-b: 'a-b' };  // bare hyphenated key — TS parser rejects

After:

Foo: { 'a-b': 'a-b' };

The same bare-key shape exists in all three valueType branches of printModuleEntry. The convention in object/number modes is to use C++ identifiers so the bug isn't reachable there in practice, but the fix applies to all three for symmetry with print, which already handles non-identifier names via string-literal quoting.

@soilSpoon soilSpoon force-pushed the fix-tsgen-hyphen-enum-keys branch from 7acd724 to b282d9a Compare April 29, 2026 01:57
`enum_value_type::string` (added in emscripten-core#25257) accepts arbitrary JS-facing
names via `.value("name", ...)`. The type union path quotes these as
string literals, but `printModuleEntry` emits them as bare object keys,
producing invalid TypeScript when the name is not a valid JS identifier
(e.g. hyphenated names).

The same bare-key shape exists in all three `valueType` branches. The
convention in `object`/`number` modes is to use C++ identifiers so the
bug isn't reachable there in practice, but the fix applies to all three
for symmetry with `print`, which already handles non-identifier names
via string-literal quoting.
@soilSpoon soilSpoon force-pushed the fix-tsgen-hyphen-enum-keys branch from b282d9a to ba193ad Compare April 29, 2026 02:01
@soilSpoon soilSpoon changed the title [embind] Quote non-identifier enum value names in printModuleEntry [tsgen] Quote non-identifier enum value names in module entry. Apr 29, 2026
@brendandahl brendandahl self-requested a review April 29, 2026 18:27
@brendandahl
Copy link
Copy Markdown
Collaborator

You'll want to run test/runner other.test_embind_tsgen* --rebaseline to update the other tests too.

…num.

The previous commit added a hyphenated enum to embind_tsgen.cpp to
exercise the printModuleEntry quoting fix, and updated the
embind_tsgen.d.ts baseline. The same .cpp is also compiled by
test_embind_tsgen_ignore_{1,2,3,7} and the embind_tsgen_module test, so
their baselines need the matching HyphenatedEnum entries.

Regenerated via:
  test/runner.py "other.test_embind_tsgen*" --rebaseline
@soilSpoon
Copy link
Copy Markdown
Author

@brendandahl Done in e3630d3. The new HyphenatedEnum propagates to five additional baselines that share embind_tsgen.cpp:

  • embind_tsgen_ignore_{1,2,3,7}.d.ts
  • embind_tsgen_module.d.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants