Skip to content

fix(js): add fallback enum key when unicode values produce empty identifier#1358

Open
Adwaith-Jayan wants to merge 2 commits intoappwrite:masterfrom
Adwaith-Jayan:fix/enum-unicode-fallback
Open

fix(js): add fallback enum key when unicode values produce empty identifier#1358
Adwaith-Jayan wants to merge 2 commits intoappwrite:masterfrom
Adwaith-Jayan:fix/enum-unicode-fallback

Conversation

@Adwaith-Jayan
Copy link

@Adwaith-Jayan Adwaith-Jayan commented Feb 19, 2026

… unicode values

What does this PR do?

Fixes enum generation for the TypeScript SDK when enum values contain non-English (Unicode) characters.

Currently, when enum values contain characters outside [a-zA-Z0-9], the toCamelCase() transformation strips all characters, resulting in an empty enum identifier and invalid TypeScript output.

Example of current broken output:

export enum ProvinceType {
     = "រាជធានី",
}

Test Plan

  1. Identified that enum values containing only Unicode characters (e.g. Khmer text) produce an empty identifier due to the toCamelCase() transformation removing non [a-zA-Z0-9] characters.
  2. Updated templates/web/src/enums/enum.ts.twig to introduce a fallback when the processed enum key is empty.
  3. Ran composer lint-twig to ensure there are no Twig syntax errors.
  4. Verified that when enum values contain only Unicode characters, the generated enum key is replaced with a safe fallback (Value0, Value1, etc.).
  5. Confirmed that existing behavior for standard ASCII enum values remains unchanged and still generates proper PascalCase enum keys.

Related PRs and Issues

Closes #1256

Have you read the Contributing Guidelines on issues?

Yes.

Summary by CodeRabbit

  • Bug Fixes
    • Improved enum member naming for empty or invalid keys by assigning deterministic fallback identifiers, preventing missing or invalid members and ensuring consistent, predictable enum declarations.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 19, 2026

No actionable comments were generated in the recent review. 🎉


📝 Walkthrough

Walkthrough

Introduces an intermediate processed key variable in the enum template and uses it to derive enum member names; when the processed key is empty, the template falls back to a deterministic name of the form _Value{index} instead of emitting an empty member identifier.

Changes

Cohort / File(s) Summary
Enum Template Generation
templates/web/src/enums/enum.ts.twig
Added an intermediate processed key variable and a fallback enum member naming: if the processed value is empty, use _Value{index} as the member name; otherwise use the processed value. RHS enum value output remains escaped/unchanged.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 I nibbled a template, neat and spry,
When names went missing, I gave them a try.
_Value0, _Value1 — now none disappear,
Hopping through enums with a joyful cheer! 🥕✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix(js): add fallback enum key when unicode values produce empty identifier' directly and clearly describes the main change: adding a fallback for empty enum keys when unicode values are processed.
Linked Issues check ✅ Passed The PR successfully addresses issue #1256 by implementing a fallback enum key (Value0, Value1, etc.) when unicode/non-ASCII enum values produce empty identifiers, ensuring valid TypeScript enum generation.
Out of Scope Changes check ✅ Passed The changes are narrowly focused on the enum template file to fix unicode enum key generation, with no unrelated modifications outside the scope of issue #1256.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
templates/web/src/enums/enum.ts.twig (1)

4-4: Apply whitespace-control modifiers to prevent extra blank lines between enum members

The Twig environment in src/SDK/SDK.php does not enable trim_blocks or lstrip_blocks, so each {% ... %} tag will leave a trailing newline. This means adding line 4 introduces an extra blank line per enum member. The deno template already uses the {%~ modifier (e.g., line 2-3) as a best practice. Apply the same approach:

Suggested change
-    {% set processed = key | replace({'-': ''}) | caseEnumKey %}
+    {%- set processed = key | replace({'-': ''}) | caseEnumKey -%}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@templates/web/src/enums/enum.ts.twig` at line 4, The Twig "{% set processed =
key | replace({'-': ''}) | caseEnumKey %}" tag emits a trailing newline; update
the tag in enum.ts.twig to use Twig's whitespace-control modifier (~) on the tag
so the set tag does not produce an extra blank line per enum member (target the
"{% set processed ... %}" tag that defines the processed variable and apply the
'~' whitespace control to the tag).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@templates/web/src/enums/enum.ts.twig`:
- Around line 4-5: The fallback identifier for empty processed keys uses "Value"
+ loop.index0 which can collide with a legitimate processed key; update the
template logic so the fallback uses an unlikely prefix (e.g., "_Value" or
"UniqueValue") instead of "Value" (modify where processed is computed and the
ternary that emits {{ processed is empty ? 'Value' ~ loop.index0 : processed
}}), or implement conflict detection/resolution in the code that prepares the
keys (ensuring caseEnumKey-normalized names plus fallbacks are unique before
rendering).
- Line 5: The enum template emits raw single-quoted values which break
TypeScript when the value contains a single quote; update the template line in
templates/web/src/enums/enum.ts.twig to output a properly escaped/quoted string
literal (e.g. use Twig's json_encode or explicit replace) instead of raw '{{
value }}'. Concretely, replace the current "= '{{ value }}'," with a safe
expression such as "= {{ value|json_encode|raw }}," (or "= '{{
value|replace(\"'\",\"\\\\'\") }}'," if you prefer single quotes) so values like
"it's" are emitted as valid TypeScript string literals; keep the surrounding
identifier logic (processed / loop.index0) unchanged.

---

Nitpick comments:
In `@templates/web/src/enums/enum.ts.twig`:
- Line 4: The Twig "{% set processed = key | replace({'-': ''}) | caseEnumKey
%}" tag emits a trailing newline; update the tag in enum.ts.twig to use Twig's
whitespace-control modifier (~) on the tag so the set tag does not produce an
extra blank line per enum member (target the "{% set processed ... %}" tag that
defines the processed variable and apply the '~' whitespace control to the tag).

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.

🐛 Bug Report: type generation for enum of non English text missing Elements value

1 participant