Skip to content

Commit 834c5df

Browse files
committed
add support for hex and unicode escapes
1 parent 8ef6990 commit 834c5df

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/services/refactors/convertStringOrTemplateLiteral.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,20 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
183183
return createTemplateExpression(templateHead, templateSpans);
184184
}
185185

186+
const hexToUnicode = (_match: string, grp: string) => String.fromCharCode(parseInt(grp, 16));
186187
const octalToUnicode = (_match: string, grp: string) => String.fromCharCode(parseInt(grp, 8));
187188

188189
function decodeRawString(content: string) {
189190
const outerQuotes = /["']((.|\s)*)["']/;
191+
const unicodeEscape = /\\u([a-fA-F0-9]{4})/gi;
192+
const unicodeEscapeWithBraces = /\\u\{([0-9a-fA-F]{1,})\}/gi;
193+
const hexEscape = /\\x([a-fA-F0-9]{2})/gi;
190194
const octalEscape = /\\((?:[1-7][0-7]{0,2}|[0-7]{2,3}))/g;
191195

192196
return content.replace(outerQuotes, (_match, grp) => grp)
197+
.replace(unicodeEscape, hexToUnicode)
198+
.replace(unicodeEscapeWithBraces, hexToUnicode)
199+
.replace(hexEscape, hexToUnicode)
193200
.replace(octalEscape, octalToUnicode);
194201

195202
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/// <reference path='fourslash.ts' />
22

3-
//// const foo = "/*x*/O/*y*/ctal \43";
3+
//// const foo = "/*x*/U/*y*/nicode \u0023 \u{0023} " + "Hex \x23 " + "Octal \43";
44

55
goTo.select("x", "y");
66
edit.applyRefactor({
77
refactorName: "Convert string concatenation or template literal",
88
actionName: "Convert to template literal",
99
actionDescription: "Convert to template literal",
1010
newContent:
11-
"const foo = `Octal #`;",
11+
"const foo = `Unicode # # Hex # Octal #`;",
1212
});
1313

0 commit comments

Comments
 (0)