Skip to content
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

Closed
wants to merge 29 commits into from

Conversation

bigaru
Copy link
Contributor

@bigaru bigaru commented Dec 9, 2018

Fixes #18267

}

function escapeText(content: string) {
return content.replace("`", "\`")
Copy link
Contributor

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

Copy link
Contributor Author

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

Copy link
Contributor Author

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());
Copy link
Contributor

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.

@bigaru bigaru changed the title add refactoring: string concatenation to template literals WIP: add refactoring: string concatenation to template literals Jan 19, 2019
@RyanCavanaugh
Copy link
Member

Closing WIP PRs for housekeeping - feel free to open a fresh one if you have something you think might be mergeable. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants