Skip to content

CLI-216 fix: use String.raw to avoid escaping backslashes in unix-agent-hook-templates from #136#137

Closed
sonarqube-agent[bot] wants to merge 1 commit intofeat/codexfrom
remediate-feat/codex-20260328-232133-30b0d640
Closed

CLI-216 fix: use String.raw to avoid escaping backslashes in unix-agent-hook-templates from #136#137
sonarqube-agent[bot] wants to merge 1 commit intofeat/codexfrom
remediate-feat/codex-20260328-232133-30b0d640

Conversation

@sonarqube-agent
Copy link
Copy Markdown

Addresses quality gate issues from #136.

Replaced concatenated template literal with String.raw to eliminate unnecessary backslash escaping in the unix-agent-hook-templates.ts file. This improves code readability and reduces error-prone double-escaping while resolving the SonarQube code smell.

View Project in SonarCloud


Fixed Issues

typescript:S7780 - `String.raw` should be used to avoid escaping `\`. • MINORView issue

Location: src/cli/commands/integrate/_common/unix-agent-hook-templates.ts:59

What changed

This hunk replaces a concatenated template literal containing multiple escaped backslashes (e.g., \\, \", \t, \r, \n) with a single String.raw template literal. By using String.raw, backslashes are treated literally and no longer need to be double-escaped, which directly resolves the code smell about using String.raw to avoid escaping \ in the file unix-agent-hook-templates.ts. The resulting code is more readable and less error-prone, as the awk script content is now expressed with single backslashes matching the intended output, and the awkward string concatenation trick (splitting the string to include a literal double-quote) is also eliminated.

--- a/src/cli/commands/integrate/_common/unix-agent-hook-templates.ts
+++ b/src/cli/commands/integrate/_common/unix-agent-hook-templates.ts
@@ -56,5 +56,1 @@ function sqaaHookOutputJsonEscapeAwk(): string {
-  return (
-    `BEGIN{ORS=""} {gsub(/\\/, "\\\\"); gsub(/"/, "\\` +
-    `"` +
-    `"); gsub(/${tab}/, "\\t"); gsub(/${cr}/, "\\r"); if(NR>1) printf "\\n"; print}`
-  );
+  return String.raw`BEGIN{ORS=""} {gsub(/\/, "\\"); gsub(/"/, "\""); gsub(/${tab}/, "\t"); gsub(/${cr}/, "\r"); if(NR>1) printf "\n"; print}`;

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


SonarQube Remediation Agent uses AI. Check for mistakes.

DISCLAIMER: Remediation Agent will not be triggered again on this (self authored) PR

Commit 1 of SonarQube suggestions

Fully fixed issues:
- [typescript:S7780] AZ02uYv8wdmNkoppS__H: `String.raw` should be used to avoid escaping `\`.

Generated by SonarQube Agent
@sonarqube-agent sonarqube-agent bot requested a review from kussberg March 28, 2026 23:21
@hashicorp-vault-sonar-prod hashicorp-vault-sonar-prod bot changed the title fix: use String.raw to avoid escaping backslashes in unix-agent-hook-templates from #136 CLI-216 fix: use String.raw to avoid escaping backslashes in unix-agent-hook-templates from #136 Mar 28, 2026
@hashicorp-vault-sonar-prod
Copy link
Copy Markdown

hashicorp-vault-sonar-prod bot commented Mar 28, 2026

CLI-216

@sonar-review-alpha
Copy link
Copy Markdown
Contributor

Summary

Single-file refactor using String.raw to resolve a SonarQube code smell. Replaces a concatenated template literal in the awk script generator with a cleaner String.raw expression. The generated awk output is functionally identical; the change is purely for code clarity and to eliminate unnecessary escaping complexity.

What reviewers should know

Where to focus: Only the sqaaHookOutputJsonEscapeAwk() function is affected. What to verify: The key difference is backslash handling—the old code used double backslashes (\\\\, \\n, \\t, etc.) to produce single backslashes in the output; the new code uses single backslashes directly within String.raw. Confirm the awk script output is identical between old and new. Why the change works: String.raw treats backslashes as literals, eliminating the escaping gymnastics and the string concatenation workaround (the triple-part \\ + \" + \" split) that existed to avoid escaping a literal double-quote. The change is low-risk—no runtime behavior changes, only code readability.


  • Generate Walkthrough
  • Generate Diagram

🗣️ Give feedback

@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown
Contributor

@sonar-review-alpha sonar-review-alpha bot left a comment

Choose a reason for hiding this comment

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

Conclusion: Clean, correct refactor. The generated awk string is byte-for-byte identical to the old concatenated approach — the fix is safe to merge pending the stale comment below.

🗣️ Give feedback

@@ -53,11 +53,7 @@ function sqaaHookOutputJsonEscapeAwk(): string {
const tab = '\t';
const cr = '\r';
// Split gsub(/"/, ...) so we avoid unnecessary `\"` escapes (typescript:S6535); keep same awk source as legacy template literals.
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.

The comment says "Split gsub(/"/,...) so we avoid unnecessary \" escapes" and describes the string-concatenation trick, but that trick no longer exists — String.raw handles it directly. The comment now describes removed code and will mislead future readers.

Suggested change
// Split gsub(/"/, ...) so we avoid unnecessary `\"` escapes (typescript:S6535); keep same awk source as legacy template literals.
// String.raw avoids double-escaping backslashes and the `\"` issue (typescript:S6535, S7780); ${tab}/${cr} interpolation still works normally.
  • Mark as noise

@kussberg
Copy link
Copy Markdown

is broken at the first gsub — it would produce an invalid awk script.

@kussberg kussberg closed this Mar 28, 2026
@sonarqube-agent sonarqube-agent bot deleted the remediate-feat/codex-20260328-232133-30b0d640 branch March 31, 2026 16:52
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