Skip to content

Commit d2ab0bd

Browse files
committed
support single quotes when decoding raw string
1 parent 6a1df73 commit d2ab0bd

9 files changed

+48
-12
lines changed

src/services/refactors/convertStringOrTemplateLiteral.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,10 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
124124
return { nodes: [node], containsString: false, areOperatorsValid: true };
125125
}
126126

127-
const nodeOperatorValid = node.operatorToken.kind === SyntaxKind.PlusToken;
128-
const isPlus = leftOperatorValid && nodeOperatorValid && rightOperatorValid;
127+
const currentOperatorValid = node.operatorToken.kind === SyntaxKind.PlusToken;
128+
const areOperatorsValid = leftOperatorValid && currentOperatorValid && rightOperatorValid;
129129

130-
return { nodes: leftNodes.concat(rightNodes), containsString: true, areOperatorsValid: isPlus };
130+
return { nodes: leftNodes.concat(rightNodes), containsString: true, areOperatorsValid };
131131
}
132132

133133
return { nodes: [node as Expression], containsString: isStringLiteral(node), areOperatorsValid: true };
@@ -179,10 +179,10 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
179179
const octalToUnicode = (_match: string, grp: string) => String.fromCharCode(parseInt(grp, 8));
180180

181181
function decodeRawString(content: string) {
182-
const outerQuotes = /"((.|\s)*)"/;
183-
const unicodeEscape = /\\u([\d\w]+)/gi;
184-
const unicodeEscapeWithBraces = /\\u\{([\d\w]+\})/gi;
185-
const hexEscape = /\\x([\d\w]+)/gi;
182+
const outerQuotes = /["']((.|\s)*)["']/;
183+
const unicodeEscape = /\\u(\w+)/gi;
184+
const unicodeEscapeWithBraces = /\\u\{(\w+)\}/gi;
185+
const hexEscape = /\\x(\w+)/gi;
186186
const octalEscape = /\\([0-7]+)/g;
187187

188188
return content.replace(outerQuotes, (_match, grp) => grp)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// <reference path='fourslash.ts' />
22

33
//// const age = 42
4-
//// const foo = `foobar is a ${ age < 18 ? 'child' : /*x*/`/*y*/grown-up ${ age > 40 ? 'who needs probaply assistance' : ''}` }`
4+
//// const foo = `foobar is a ${ age < 18 ? 'child' : /*x*/`/*y*/grown-up ${ age > 40 ? 'who needs probably assistance' : ''}` }`
55

66
goTo.select("x", "y");
77
edit.applyRefactor({
@@ -10,5 +10,5 @@ edit.applyRefactor({
1010
actionDescription: "Convert to string concatenation",
1111
newContent:
1212
`const age = 42
13-
const foo = \`foobar is a \${ age < 18 ? 'child' : "grown-up " + (age > 40 ? 'who needs probaply assistance' : '') }\``,
13+
const foo = \`foobar is a \${ age < 18 ? 'child' : "grown-up " + (age > 40 ? 'who needs probably assistance' : '') }\``,
1414
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const foo = `/*x*/f/*y*/oobar is ${ 42 * 6 % 4} years old`
4+
5+
goTo.select("x", "y");
6+
edit.applyRefactor({
7+
refactorName: "Convert string concatenation or template literal",
8+
actionName: "Convert to string concatenation",
9+
actionDescription: "Convert to string concatenation",
10+
newContent:
11+
`const foo = "foobar is " + 42 * 6 % 4 + " years old"`,
12+
});

tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateAvailability.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
//// const age = 22
44
//// const name = "Eddy"
5-
//// const /*z*/f/*y*/oo = /*x*/"/*w*/M/*v*/r/*u*/ " /*t*/+/*s*/ /*r*/n/*q*/ame + " is " + /*p*/a/*o*/ge + " years old"
5+
//// 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"
66

77
goTo.select("z", "y");
88
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation");
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const foo = "/*x*/f/*y*/oobar is " + 42 * 6 / 4 + " years old"
4+
5+
goTo.select("x", "y");
6+
edit.applyRefactor({
7+
refactorName: "Convert string concatenation or template literal",
8+
actionName: "Convert to template literal",
9+
actionDescription: "Convert to template literal",
10+
newContent:
11+
`const foo = \`foobar is \${42 * 6 / 4} years old\``,
12+
});
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/// <reference path='fourslash.ts' />
22

3-
//// const foo = /*x*/4/*y*/2 + 6 + 23 + 12 +" years old"
3+
//// const foo = /*x*/4/*y*/2 - 6 * 4 + 23 / 12 +" years old"
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 = \`\${42 + 6 + 23 + 12} years old\``,
11+
`const foo = \`\${42 - 6 * 4 + 23 / 12} years old\``,
1212
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const foo = '/*x*/f/*y*/oobar rocks'
4+
5+
goTo.select("x", "y");
6+
edit.applyRefactor({
7+
refactorName: "Convert string concatenation or template literal",
8+
actionName: "Convert to template literal",
9+
actionDescription: "Convert to template literal",
10+
newContent:
11+
`const foo = \`foobar rocks\``,
12+
});

0 commit comments

Comments
 (0)