use stringLiteral property text instead of decodeRawString

This commit is contained in:
BigAru
2019-06-30 13:45:47 +02:00
parent 4b95c1fbf2
commit 04f96db89d
47 changed files with 5 additions and 78 deletions
@@ -192,7 +192,8 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
const indexes = [];
while (index < nodes.length && isStringLiteral(nodes[index])) {
text = text + decodeRawString(nodes[index].getText());
const stringNode = nodes[index] as StringLiteral;
text = text + stringNode.text;
indexes.push(index);
index++;
}
@@ -247,28 +248,9 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
return node;
}
const hexToUnicode = (_match: string, grp: string) => String.fromCharCode(parseInt(grp, 16));
const octalToUnicode = (_match: string, grp: string) => String.fromCharCode(parseInt(grp, 8));
function decodeRawString(content: string) {
const outerQuotes = /["']((.|\s)*)["']/;
const unicodeEscape = /\\u([a-fA-F0-9]{4})/gi;
const unicodeEscapeWithBraces = /\\u\{([0-9a-fA-F]{1,})\}/gi;
const hexEscape = /\\x([a-fA-F0-9]{2})/gi;
const octalEscape = /\\((?:[1-7][0-7]{0,2}|[0-7]{2,3}))/g;
return content.replace(outerQuotes, (_match, grp) => grp)
.replace(unicodeEscape, hexToUnicode)
.replace(unicodeEscapeWithBraces, hexToUnicode)
.replace(hexEscape, hexToUnicode)
.replace(octalEscape, octalToUnicode);
}
function escapeText(content: string) {
return content.replace("`", "\`") // back-tick
.replace("${", "$\\{"); // placeholder alike beginning
}
}
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
`console.log("foobar is " + 32 + " years old")`,
});
@@ -15,4 +15,3 @@ verify.not.refactorAvailable("Convert string concatenation or template literal",
goTo.select("v", "u");
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation");
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal");
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
`const foo = "foobar is " + (42 + 6) + " years old"`,
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
`const foo = /* H */ "HEAD " + /* C0 */ 42 /* C1 */ + " Span1 " + /* C2 */ 43 /* C3 */ + " Span2 " + /* C4 */ 44 /* C5 */ + " Span3" /* T */`,
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
`const foo = /* C0 */ 42 /* C1 */`,
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
`const foo = "HEAD | " + /* C0 */ 42 /* C1 */ + /* C2 */ 43 /* C3 */ + /* C4 */ 44 /* C5 */`,
});
@@ -14,4 +14,3 @@ edit.applyRefactor({
const name = "Eddy"
const foo = name + " is " + age + " years old"`,
});
@@ -12,4 +12,3 @@ edit.applyRefactor({
`const age = 42
const foo = \`foobar is a \${ age < 18 ? 'child' : "grown-up " + (age > 40 ? 'who needs probably assistance' : '') }\``,
});
@@ -12,4 +12,3 @@ edit.applyRefactor({
`const age = 42
const foo = \`foobar is a \${ "34" }\``,
});
@@ -12,4 +12,3 @@ edit.applyRefactor({
`const age = 42
const foo = "foobar is a " + (age < 18 ? 'child' : \`grown-up \${age > 40 ? 'who needs probaply assistance' : ''}\`)`,
});
@@ -12,4 +12,3 @@ edit.applyRefactor({
`const age = 42
const foo = "foobar is " + age + " years old"`,
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
`const foo = 42`,
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
`const foo = "foobar is " + 42 * 6 % 4 + " years old"`,
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
`const foo = "foobar is " + 42 + 6 + " years old"`,
});
@@ -12,4 +12,3 @@ edit.applyRefactor({
`const age = 42
const foo = "foobar is " + age + " years old"`,
});
@@ -12,4 +12,3 @@ edit.applyRefactor({
`const age = 42
const foo = "foobar is " + age + " years old " + false`,
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
`const foo = "foobar rocks"`,
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
`console.log(\`foobar is \${32} years old\`)`,
});
@@ -36,4 +36,4 @@ edit.applyRefactor({
`const age = 22
const name = "Eddy"
const foo = \`Mr \${name} is \${age * 4 / 2} years old\``,
});
});
@@ -23,4 +23,3 @@ verify.not.refactorAvailable("Convert string concatenation or template literal",
goTo.select("r", "q");
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation");
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal");
@@ -17,4 +17,3 @@ verify.not.refactorAvailable("Convert string concatenation or template literal",
goTo.select("t", "s");
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation");
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal");
@@ -30,4 +30,4 @@ edit.applyRefactor({
newContent:
`const age = 22
const foo = \`\${age * 4 - 2 / 4} years old\``,
});
});
@@ -22,4 +22,4 @@ edit.applyRefactor({
newContent:
`let foo = ""
foo = \`Mr Bar is\${42} years old\``,
});
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
"const foo = `with back\\`tick`",
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
`const foo = \`foobar is \${42 + 6} years old\``,
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
"const foo = `foobar is \${42 + 6}`",
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
"const foo = /* C0 */ `foo is\${ /* C1 */ /* C2 */42 /* C3 */} and bar is\${ /* C4 */ /* C5 */52 /* C6 */}`",
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
"const foo = /* C0 */ `foo is\${ /* C1 */ /* C2 */42 /* C3 */} and bar is\${ /* C4 */ /* C5 */52 /* C6 */}`",
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
"const foo = /* C0 */ `foo is` /* C1 */ /* C2 */ /* C3 */",
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
"const foo = `foobar is\${/* C1 */ 42 /* C2 */ /* C3 */} years old`",
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
"const foo = /* C0 */ `foo is\${ /* C1 */ /* C2 */42 /* C3 */}`",
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
"const foo = /* C0 */ `foo is\${ /* C1 */ /* C2 */42} years old` /* C3 */",
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
`const foo = \`foobar is \${42} years old and \${6} cars are missing\``,
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
"const foo = `with $\\\\{dollar}`",
});
@@ -14,4 +14,3 @@ edit.applyRefactor({
const name = "Eddy"
const foo = \`\${name} is \${age} years old\``,
});
@@ -1,13 +0,0 @@
/// <reference path='fourslash.ts' />
//// const foo = "/*x*/U/*y*/nicode \u0023 \u{0023} " + "Hex \x23 " + "Octal \43";
goTo.select("x", "y");
edit.applyRefactor({
refactorName: "Convert string concatenation or template literal",
actionName: "Convert to template literal",
actionDescription: "Convert to template literal",
newContent:
"const foo = `Unicode # # Hex # Octal #`;",
});
@@ -12,4 +12,3 @@ edit.applyRefactor({
`const age = 42
const foo = \`foobar is \${age} years old\``,
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
"const foo = `foobar rocks fantastically`",
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
`const foo = \`foobar is \${42 * 6 / 4} years old\``,
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
`const foo = \`foobar is \${42 + 6} years old\``,
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
`const foo = \`foobar is \${42 + 6} years old\``,
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
`const foo = "foobar is " + (\`\${42 + 6}str\`) + " years old"`,
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
`const foo = \`foobar is \${42}\${6} years old\``,
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
`const foo = \`\${42 - 6 * 4 + 23 / 12} years old\``,
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
`const foo = \`foobar rocks\``,
});
@@ -10,4 +10,3 @@ edit.applyRefactor({
newContent:
`const foo = \`foobar rocks\``,
});