fix: escape special characters in Figma property names in generated template code#380
Open
jmlweb wants to merge 1 commit intofigma:mainfrom
Open
fix: escape special characters in Figma property names in generated template code#380jmlweb wants to merge 1 commit intofigma:mainfrom
jmlweb wants to merge 1 commit intofigma:mainfrom
Conversation
…e code
Figma property names containing apostrophes (e.g. "Tab's number") produce
broken JavaScript when interpolated into single-quoted string literals in
the generated template code. The Figma panel fails to render the example
even though `figma connect publish` succeeds without errors.
Before this fix, `intrinsicToString()` generated:
figma.currentLayer.__properties__.string('Tab's number')
which is a syntax error — the apostrophe closes the string literal.
After this fix:
figma.currentLayer.__properties__.string('Tab\'s number')
Changes:
- Rename `replaceNewlines` to `escapeForSingleQuotedString` with correct
escaping order (backslashes first, then quotes, then newlines)
- Add `escapeForDoubleQuotedString` for double-quoted contexts
- Apply escaping to all `figmaPropName` interpolations in `intrinsicToString`
- Apply escaping to layer names and mapping keys in double-quoted contexts
- Add test covering apostrophes in string/enum/boolean property names and
double quotes in enum mapping keys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Figma property names containing apostrophes (e.g.
Tab's number) produce broken JavaScript when interpolated into single-quoted string literals in the generated template code. The Figma Code Connect panel fails to render the example, even thoughfigma connect publishsucceeds without errors.Root cause
intrinsicToString()incli/src/connect/intrinsics.tsinterpolatesfigmaPropNamedirectly inside single-quoted JS template literals without escaping:Similarly,
valueMappingToString()wraps enum mapping keys in double quotes without escaping internal double quotes.Fix
replaceNewlines→escapeForSingleQuotedStringwith correct escaping order (backslashes first, then quotes, then newlines)escapeForDoubleQuotedStringfor double-quoted contexts (mapping keys, layer names, className strings)escapeForSingleQuotedStringto allfigmaPropNameinterpolations inintrinsicToString(String,Instance,Boolean,Enum,Slot)escapeForDoubleQuotedStringtoChildrenlayer names,ClassNamestrings,TextContentlayer names,NestedPropslayer names, andvalueMappingToStringkeysAffected intrinsic types
String,Instance,Boolean,Enum,Slot'${figmaPropName}'escapeForSingleQuotedStringChildren"${layerName}"escapeForDoubleQuotedStringClassName"${className}"escapeForDoubleQuotedStringTextContent'${layer}'escapeForSingleQuotedStringNestedProps"${layer}"escapeForDoubleQuotedStringvalueMappingToStringkeys"${key}"escapeForDoubleQuotedStringReproduction
Tab's numberfigma.string("Tab's number")figma connect publish— succeedsTest plan
SpecialCharProps.figma.tsxtest fixture with apostrophes infigma.string,figma.enum(prop name + mapping key with apostrophe),figma.boolean, and double quotes in an enum mapping keySpecialCharProps.expected_templatevalidating escaped outputtemplateData(raw prop names preserved) andtemplate(escaped output)🤖 Generated with Claude Code