Skip to content

Replace String.fromCharCode() with String.fromCodePoint() for Unicode safety#34

Open
sonarqube-agent[bot] wants to merge 1 commit into
mainfrom
remediate-main-20260609-090127-e09dea26
Open

Replace String.fromCharCode() with String.fromCodePoint() for Unicode safety#34
sonarqube-agent[bot] wants to merge 1 commit into
mainfrom
remediate-main-20260609-090127-e09dea26

Conversation

@sonarqube-agent

Copy link
Copy Markdown

This PR was automatically created by the Remediation Agent's Scheduled backlog remediation feature.

Why these issues? This change addresses 5 instances of SonarQube rule S7758 across two functions in a single, well-scoped file. The grouped identical issues provide high automation ROI by modernizing the codebase's string handling to be Unicode-compliant with minimal refactoring effort.

Updated the handleSpecialCharacters module to use String.fromCodePoint() instead of String.fromCharCode() for generating special characters like carriage returns, tabs, and backslashes. String.fromCodePoint() is the modern Unicode-safe alternative that properly handles the full range of Unicode code points including characters requiring surrogate pairs.

View Project in SonarCloud


Fixed Issues

typescript:S7758 - Prefer `String.fromCodePoint()` over `String.fromCharCode()`. • MINORView issue

Location: src/handleSpecialCharacters/index.ts:13

Why is this an issue?

JavaScript strings use UTF-16 encoding internally. Some Unicode characters, like emojis and certain international characters, require two 16-bit code units (called surrogate pairs) to represent a single character.

What changed

This hunk replaces four occurrences of String.fromCharCode() with String.fromCodePoint() in the removeSpecialCharacters function. Specifically, it fixes: the String.fromCharCode(13) call on line 13 (replacing carriage return generation), the String.fromCharCode(9) call on line 14 (replacing tab generation), and both String.fromCharCode(92) and String.fromCharCode(92, 92) calls on line 15 (replacing backslash generation). The String.fromCharCode(10) on line 12 is also replaced though it wasn't flagged. Using String.fromCodePoint() is the modern, Unicode-safe alternative that properly handles surrogate pairs for characters outside the Basic Multilingual Plane.

--- a/src/handleSpecialCharacters/index.ts
+++ b/src/handleSpecialCharacters/index.ts
@@ -12,4 +12,4 @@ export const removeSpecialCharacters = (text: string): string => {
-    .replaceAll(String.fromCharCode(10), "")
-    .replaceAll(String.fromCharCode(13), "")
-    .replaceAll(String.fromCharCode(9), "\\t")
-    .replaceAll(String.fromCharCode(92), String.fromCharCode(92, 92));
+    .replaceAll(String.fromCodePoint(10), "")
+    .replaceAll(String.fromCodePoint(13), "")
+    .replaceAll(String.fromCodePoint(9), "\\t")
+    .replaceAll(String.fromCodePoint(92), String.fromCodePoint(92, 92));
typescript:S7758 - Prefer `String.fromCodePoint()` over `String.fromCharCode()`. • MINORView issue

Location: src/handleSpecialCharacters/index.ts:14

Why is this an issue?

JavaScript strings use UTF-16 encoding internally. Some Unicode characters, like emojis and certain international characters, require two 16-bit code units (called surrogate pairs) to represent a single character.

What changed

This hunk replaces four occurrences of String.fromCharCode() with String.fromCodePoint() in the removeSpecialCharacters function. Specifically, it fixes: the String.fromCharCode(13) call on line 13 (replacing carriage return generation), the String.fromCharCode(9) call on line 14 (replacing tab generation), and both String.fromCharCode(92) and String.fromCharCode(92, 92) calls on line 15 (replacing backslash generation). The String.fromCharCode(10) on line 12 is also replaced though it wasn't flagged. Using String.fromCodePoint() is the modern, Unicode-safe alternative that properly handles surrogate pairs for characters outside the Basic Multilingual Plane.

--- a/src/handleSpecialCharacters/index.ts
+++ b/src/handleSpecialCharacters/index.ts
@@ -12,4 +12,4 @@ export const removeSpecialCharacters = (text: string): string => {
-    .replaceAll(String.fromCharCode(10), "")
-    .replaceAll(String.fromCharCode(13), "")
-    .replaceAll(String.fromCharCode(9), "\\t")
-    .replaceAll(String.fromCharCode(92), String.fromCharCode(92, 92));
+    .replaceAll(String.fromCodePoint(10), "")
+    .replaceAll(String.fromCodePoint(13), "")
+    .replaceAll(String.fromCodePoint(9), "\\t")
+    .replaceAll(String.fromCodePoint(92), String.fromCodePoint(92, 92));
typescript:S7758 - Prefer `String.fromCodePoint()` over `String.fromCharCode()`. • MINORView issue

Location: src/handleSpecialCharacters/index.ts:31

Why is this an issue?

JavaScript strings use UTF-16 encoding internally. Some Unicode characters, like emojis and certain international characters, require two 16-bit code units (called surrogate pairs) to represent a single character.

What changed

This hunk replaces two occurrences of String.fromCharCode() with String.fromCodePoint() in the insertSpecialCharacters function. Specifically, it fixes the String.fromCharCode(9) call on line 31 and the String.fromCharCode(92, 92) / String.fromCharCode(92) calls on line 32. Using String.fromCodePoint() is the preferred modern method that correctly handles full Unicode code points including characters that require surrogate pairs.

--- a/src/handleSpecialCharacters/index.ts
+++ b/src/handleSpecialCharacters/index.ts
@@ -31,2 +31,2 @@ export const insertSpecialCharacters = (text: string): string => {
-    .replaceAll("\\t", String.fromCharCode(9))
-    .replaceAll(String.fromCharCode(92, 92), String.fromCharCode(92));
+    .replaceAll("\\t", String.fromCodePoint(9))
+    .replaceAll(String.fromCodePoint(92, 92), String.fromCodePoint(92));
typescript:S7758 - Prefer `String.fromCodePoint()` over `String.fromCharCode()`. • MINORView issue

Location: src/handleSpecialCharacters/index.ts:15

Why is this an issue?

JavaScript strings use UTF-16 encoding internally. Some Unicode characters, like emojis and certain international characters, require two 16-bit code units (called surrogate pairs) to represent a single character.

What changed

This hunk replaces four occurrences of String.fromCharCode() with String.fromCodePoint() in the removeSpecialCharacters function. Specifically, it fixes: the String.fromCharCode(13) call on line 13 (replacing carriage return generation), the String.fromCharCode(9) call on line 14 (replacing tab generation), and both String.fromCharCode(92) and String.fromCharCode(92, 92) calls on line 15 (replacing backslash generation). The String.fromCharCode(10) on line 12 is also replaced though it wasn't flagged. Using String.fromCodePoint() is the modern, Unicode-safe alternative that properly handles surrogate pairs for characters outside the Basic Multilingual Plane.

--- a/src/handleSpecialCharacters/index.ts
+++ b/src/handleSpecialCharacters/index.ts
@@ -12,4 +12,4 @@ export const removeSpecialCharacters = (text: string): string => {
-    .replaceAll(String.fromCharCode(10), "")
-    .replaceAll(String.fromCharCode(13), "")
-    .replaceAll(String.fromCharCode(9), "\\t")
-    .replaceAll(String.fromCharCode(92), String.fromCharCode(92, 92));
+    .replaceAll(String.fromCodePoint(10), "")
+    .replaceAll(String.fromCodePoint(13), "")
+    .replaceAll(String.fromCodePoint(9), "\\t")
+    .replaceAll(String.fromCodePoint(92), String.fromCodePoint(92, 92));
typescript:S7758 - Prefer `String.fromCodePoint()` over `String.fromCharCode()`. • MINORView issue

Location: src/handleSpecialCharacters/index.ts:15

Why is this an issue?

JavaScript strings use UTF-16 encoding internally. Some Unicode characters, like emojis and certain international characters, require two 16-bit code units (called surrogate pairs) to represent a single character.

What changed

This hunk replaces four occurrences of String.fromCharCode() with String.fromCodePoint() in the removeSpecialCharacters function. Specifically, it fixes: the String.fromCharCode(13) call on line 13 (replacing carriage return generation), the String.fromCharCode(9) call on line 14 (replacing tab generation), and both String.fromCharCode(92) and String.fromCharCode(92, 92) calls on line 15 (replacing backslash generation). The String.fromCharCode(10) on line 12 is also replaced though it wasn't flagged. Using String.fromCodePoint() is the modern, Unicode-safe alternative that properly handles surrogate pairs for characters outside the Basic Multilingual Plane.

--- a/src/handleSpecialCharacters/index.ts
+++ b/src/handleSpecialCharacters/index.ts
@@ -12,4 +12,4 @@ export const removeSpecialCharacters = (text: string): string => {
-    .replaceAll(String.fromCharCode(10), "")
-    .replaceAll(String.fromCharCode(13), "")
-    .replaceAll(String.fromCharCode(9), "\\t")
-    .replaceAll(String.fromCharCode(92), String.fromCharCode(92, 92));
+    .replaceAll(String.fromCodePoint(10), "")
+    .replaceAll(String.fromCodePoint(13), "")
+    .replaceAll(String.fromCodePoint(9), "\\t")
+    .replaceAll(String.fromCodePoint(92), String.fromCodePoint(92, 92));

Have a suggestion or found an issue? Share your feedback here.


SonarQube Remediation Agent uses AI. Check for mistakes.

Fixed issues:
- AZnd_k0U9LJ6Ep7RioAG for typescript:S7758 rule
- AZnd_k0U9LJ6Ep7RioAA for typescript:S7758 rule
- AZnd_k0U9LJ6Ep7RioAB for typescript:S7758 rule
- AZnd_k0U9LJ6Ep7RioAD for typescript:S7758 rule
- AZnd_k0U9LJ6Ep7RioAE for typescript:S7758 rule

Generated by SonarQube Agent (task: 512dddeb-dbb2-4973-ba30-dd2eb278c646)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant