-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: add refactoring: string concatenation to template literals #28923
Conversation
} | ||
|
||
function escapeText(content: string) { | ||
return content.replace("`", "\`") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function should also covert octal escape sequences to some other representation (hexadecimal?) as they are not allowed in template strings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you, I missed that one
@ajafff Do you know by chance how to get raw representation of normal string (not of template literal)?
Since escape sequences are already interpreted, it's not possible to catch with regex.
const ex7 = "Unicode \u0023 \u{0023} " + "Hex \x23 " + " Octal \43";
// Unicode # # Hex # Octal #
const transformed7 = ex7.replace(/\\([0-7]+)/g, (_whole, n) =>"eee");
// Unicode # # Hex # Octal #
// Therefore, regex fails
With String.raw it's possible to get raw representation
const rawVersion = String.raw`Unicode \u0023 \u{0023} Hex \x23 Octal \43`;
// Unicode \u0023 \u{0023} Hex \x23 Octal \43
But if I use String.raw as function, it behaves differently.
const ex9 = ["Unicode \u0023 \u{0023} Hex \x23 Octal \43"]
const transformed9 = String.raw({ raw: ex9, ...ex9 });
// Unicode # # Hex # Octal 43
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found a solution but I am not sure if it's the right way to solve it.
|
||
while (begin < nodes.length && isStringLiteral(nodes[begin])) { | ||
const next = nodes[begin] as StringLiteral; | ||
text = text + decodeRawString(next.getText()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
node.getText()
is the correct solution to get the raw string content. To avoid unnecessary work, consider passing the SourceFile as argument to it.
Closing WIP PRs for housekeeping - feel free to open a fresh one if you have something you think might be mergeable. Thanks! |
Fixes #18267