support single quotes when decoding raw string

This commit is contained in:
BigAru
2019-03-22 16:40:15 +01:00
parent 6a1df730c5
commit d2ab0bd05a
9 changed files with 48 additions and 12 deletions
@@ -124,10 +124,10 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
return { nodes: [node], containsString: false, areOperatorsValid: true };
}
const nodeOperatorValid = node.operatorToken.kind === SyntaxKind.PlusToken;
const isPlus = leftOperatorValid && nodeOperatorValid && rightOperatorValid;
const currentOperatorValid = node.operatorToken.kind === SyntaxKind.PlusToken;
const areOperatorsValid = leftOperatorValid && currentOperatorValid && rightOperatorValid;
return { nodes: leftNodes.concat(rightNodes), containsString: true, areOperatorsValid: isPlus };
return { nodes: leftNodes.concat(rightNodes), containsString: true, areOperatorsValid };
}
return { nodes: [node as Expression], containsString: isStringLiteral(node), areOperatorsValid: true };
@@ -179,10 +179,10 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
const octalToUnicode = (_match: string, grp: string) => String.fromCharCode(parseInt(grp, 8));
function decodeRawString(content: string) {
const outerQuotes = /"((.|\s)*)"/;
const unicodeEscape = /\\u([\d\w]+)/gi;
const unicodeEscapeWithBraces = /\\u\{([\d\w]+\})/gi;
const hexEscape = /\\x([\d\w]+)/gi;
const outerQuotes = /["']((.|\s)*)["']/;
const unicodeEscape = /\\u(\w+)/gi;
const unicodeEscapeWithBraces = /\\u\{(\w+)\}/gi;
const hexEscape = /\\x(\w+)/gi;
const octalEscape = /\\([0-7]+)/g;
return content.replace(outerQuotes, (_match, grp) => grp)
@@ -1,7 +1,7 @@
/// <reference path='fourslash.ts' />
//// const age = 42
//// const foo = `foobar is a ${ age < 18 ? 'child' : /*x*/`/*y*/grown-up ${ age > 40 ? 'who needs probaply assistance' : ''}` }`
//// const foo = `foobar is a ${ age < 18 ? 'child' : /*x*/`/*y*/grown-up ${ age > 40 ? 'who needs probably assistance' : ''}` }`
goTo.select("x", "y");
edit.applyRefactor({
@@ -10,5 +10,5 @@ edit.applyRefactor({
actionDescription: "Convert to string concatenation",
newContent:
`const age = 42
const foo = \`foobar is a \${ age < 18 ? 'child' : "grown-up " + (age > 40 ? 'who needs probaply assistance' : '') }\``,
const foo = \`foobar is a \${ age < 18 ? 'child' : "grown-up " + (age > 40 ? 'who needs probably assistance' : '') }\``,
});
@@ -0,0 +1,12 @@
/// <reference path='fourslash.ts' />
//// const foo = `/*x*/f/*y*/oobar is ${ 42 * 6 % 4} years old`
goTo.select("x", "y");
edit.applyRefactor({
refactorName: "Convert string concatenation or template literal",
actionName: "Convert to string concatenation",
actionDescription: "Convert to string concatenation",
newContent:
`const foo = "foobar is " + 42 * 6 % 4 + " years old"`,
});
@@ -2,7 +2,7 @@
//// const age = 22
//// const name = "Eddy"
//// const /*z*/f/*y*/oo = /*x*/"/*w*/M/*v*/r/*u*/ " /*t*/+/*s*/ /*r*/n/*q*/ame + " is " + /*p*/a/*o*/ge + " years old"
//// const /*z*/f/*y*/oo = /*x*/"/*w*/M/*v*/r/*u*/ " /*t*/+/*s*/ /*r*/n/*q*/ame + " is " + /*p*/a/*o*/ge * 4 / 2 + " years old"
goTo.select("z", "y");
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation");
@@ -0,0 +1,12 @@
/// <reference path='fourslash.ts' />
//// const foo = "/*x*/f/*y*/oobar is " + 42 * 6 / 4 + " years old"
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 = \`foobar is \${42 * 6 / 4} years old\``,
});
@@ -1,6 +1,6 @@
/// <reference path='fourslash.ts' />
//// const foo = /*x*/4/*y*/2 + 6 + 23 + 12 +" years old"
//// const foo = /*x*/4/*y*/2 - 6 * 4 + 23 / 12 +" years old"
goTo.select("x", "y");
edit.applyRefactor({
@@ -8,5 +8,5 @@ edit.applyRefactor({
actionName: "Convert to template literal",
actionDescription: "Convert to template literal",
newContent:
`const foo = \`\${42 + 6 + 23 + 12} years old\``,
`const foo = \`\${42 - 6 * 4 + 23 / 12} years old\``,
});
@@ -0,0 +1,12 @@
/// <reference path='fourslash.ts' />
//// const foo = '/*x*/f/*y*/oobar rocks'
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 = \`foobar rocks\``,
});