Escape string literals in codegen output#54892
Open
ide wants to merge 3 commits into
Open
Conversation
Why
===
The codegen emits string literals in generated C++, Java, and Objective-C code. These literals can contain characters that need escaping, such as quotes and backslashes, or non-ASCII characters that may cause compilation issues in native code.
Credit to Sundaram Shukla for pointing this out.
How
===
Added language-specific functions to emit string literals in `generators/Utils.js`. Internally it uses JSON.stringify which handles double quotes, backslashes, and ASCII control sequences. Applied this escaping to string literals emitted in the code generators.
Note: the code generator interpolates some values outside of string literals, like this (`__hostFunction_${hasteModuleName}SpecJSI_${propertyName}`):
```js
return ` methodMap_["${toJavaString(propertyName)}"] = MethodMetadata {${argCount}, __hostFunction_${hasteModuleName}SpecJSI_${propertyName}};`;
```
This commit does not cover those scenarios.
Test Plan
===
yarn jest packages/react-native-codegen
cipolleschi
approved these changes
Dec 16, 2025
cipolleschi
left a comment
Contributor
There was a problem hiding this comment.
Code looks great, thanks for the contribution @ide
Contributor
|
Do you mind having a look at why the |
Contributor
Author
|
@cipolleschi Thank you for the fast review! The lint errors are fixed, CI is green. |
Collaborator
|
This PR is stale because it has been open for 180 days with no activity. It will be closed in 7 days unless you comment on it or remove the "Stale" label. |
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:
The codegen emits string literals in generated C++, Java, and Objective-C code. These literals can contain characters that need escaping, such as quotes and backslashes, or non-ASCII characters that may cause compilation issues in native code. Credit to Akhilesh Chandra for pointing this out.
Added language-specific functions to emit string literals in
generators/Utils.js. Internally it uses JSON.stringify which handles double quotes, backslashes, and ASCII control sequences. Applied this escaping to string literals emitted in the code generators.Note: the code generator interpolates some values outside of string literals, like this (
__hostFunction_${hasteModuleName}SpecJSI_${propertyName}):This commit does not cover those scenarios.
Changelog:
[GENERAL] [FIXED] - Escape values interpolated in string literals for Turbo Native Module codegen
Test Plan:
Added a unit test for the escaping functions.
yarn jest packages/react-native-codegen