Skip to content

Commit 806eb12

Browse files
committed
add skeleton for handling octal escape
1 parent cf25c12 commit 806eb12

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/services/refactors/convertStringOrTemplateLiteral.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,16 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
135135

136136
function createHead(nodes: ReadonlyArray<Expression>): [number, TemplateHead] {
137137
let begin = 0;
138-
const head = createTemplateHead("");
138+
let text = "";
139139

140140
while (begin < nodes.length && isStringLiteral(nodes[begin])) {
141141
const next = nodes[begin] as StringLiteral;
142-
head.text = head.text + next.text;
142+
text = text + next.text;
143143
begin++;
144144
}
145145

146-
head.text = escapeText(head.text);
147-
return [begin, head];
146+
text = escapeText(text);
147+
return [begin, createTemplateHead(text)];
148148
}
149149

150150
function nodesToTemplate(nodes: ReadonlyArray<Expression>) {
@@ -176,8 +176,12 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
176176
}
177177

178178
function escapeText(content: string) {
179+
// back-tick
179180
return content.replace("`", "\`")
180-
.replace("\${", `$\\{`);
181+
// placeholder alike beginning
182+
.replace("\${", `$\\{`)
183+
// octal escape
184+
.replace(/\\([0-7]+)/g, (_whole, n) => "\\x" + parseInt(n, 8).toString(16));
181185
}
182186

183187
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
debugger;
4+
//// const foo = "/*x*/U/*y*/nicode \u0023 \u{0023} " + "Hex \x23 " + "Octal \43";
5+
6+
goTo.select("x", "y");
7+
edit.applyRefactor({
8+
refactorName: "Convert string concatenation or template literal",
9+
actionName: "Convert to template literal",
10+
actionDescription: "Convert to template literal",
11+
newContent:
12+
"const foo = `Unicode # # Hex # Octal #\`;",
13+
});

0 commit comments

Comments
 (0)