Skip to content

Escape string literals in codegen output#54892

Open
ide wants to merge 3 commits into
react:mainfrom
ide:@ide/escapecodegenstrings
Open

Escape string literals in codegen output#54892
ide wants to merge 3 commits into
react:mainfrom
ide:@ide/escapecodegenstrings

Conversation

@ide

@ide ide commented Dec 16, 2025

Copy link
Copy Markdown
Contributor

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}):

return `  methodMap_["${toJavaString(propertyName)}"] = MethodMetadata {${argCount}, __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

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
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 16, 2025
@facebook-github-bot facebook-github-bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Dec 16, 2025

@cipolleschi cipolleschi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code looks great, thanks for the contribution @ide

@cipolleschi

Copy link
Copy Markdown
Contributor

Do you mind having a look at why the lint and the test_js are failing in CI and fix it, please?

@ide ide requested a review from cipolleschi December 16, 2025 22:25
@ide

ide commented Dec 16, 2025

Copy link
Copy Markdown
Contributor Author

@cipolleschi Thank you for the fast review! The lint errors are fixed, CI is green.

@react-native-bot

Copy link
Copy Markdown
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.

@react-native-bot react-native-bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Jun 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. Stale There has been a lack of activity on this issue and it may be closed soon.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants