react-native icon indicating copy to clipboard operation
react-native copied to clipboard

Escape string literals in codegen output

Open ide opened this issue 1 month ago • 2 comments

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

ide avatar Dec 16 '25 05:12 ide

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

cipolleschi avatar Dec 16 '25 09:12 cipolleschi

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

ide avatar Dec 16 '25 22:12 ide