Skip to content

Commit 6de23d7

Browse files
committed
do not offer refactoring for tagged templates
1 parent 6fe4663 commit 6de23d7

4 files changed

+32
-5
lines changed

src/services/refactors/convertStringOrTemplateLiteral.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
99
const toStringConcatenationDescription = getLocaleSpecificMessage(Diagnostics.Convert_to_string_concatenation);
1010

1111
// TODO let a = 45 - 45 + " ee" - 33;
12-
// TODO let a = tag `aaa`;
1312

1413
registerRefactor(refactorName, { getEditsForAction, getAvailableActions });
1514

1615
function getAvailableActions(context: RefactorContext): ReadonlyArray<ApplicableRefactorInfo> {
1716
const { file, startPosition } = context;
1817
const node = getTokenAtPosition(file, startPosition);
1918
const maybeBinary = getParentBinaryExpression(node); containsString(maybeBinary);
19+
const maybeTemplateExpression = findAncestor(node, n => isTemplateExpression(n));
2020
const actions: RefactorActionInfo[] = [];
2121

2222
if ((isBinaryExpression(maybeBinary) || isStringLiteral(maybeBinary)) && containsString(maybeBinary)) {
2323
actions.push({ name: toTemplateLiteralActionName, description: toTemplateLiteralDescription });
2424
}
2525

26-
if (isNoSubstitutionTemplateLiteral(node) || isTemplateHead(node) || isTemplateSpan(node.parent)) {
26+
if ((isNoSubstitutionTemplateLiteral(node) && !isTaggedTemplateExpression(node.parent)) || (maybeTemplateExpression && !isTaggedTemplateExpression(maybeTemplateExpression.parent))) {
2727
actions.push({ name: toStringConcatenationActionName, description: toStringConcatenationDescription });
2828
}
2929

@@ -46,8 +46,7 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
4646
case toStringConcatenationActionName:
4747
if (isNoSubstitutionTemplateLiteral(node)) {
4848
const stringLiteral = createStringLiteral(node.text);
49-
50-
return { edits: textChanges.ChangeTracker.with(context, t => t.replaceNode(file, node, stringLiteral)) };
49+
return { edits: textChanges.ChangeTracker.with(context, t => t.replaceNode(file, node, stringLiteral)) };
5150

5251
}
5352
if (isTemplateExpression(node.parent) || isTemplateSpan(node.parent)) {

tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringAvailability.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,3 @@ verify.not.refactorAvailable("Convert string concatenation or template literal",
2727
goTo.select("p", "o");
2828
verify.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation");
2929
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal");
30-
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// function tag(literals: TemplateStringsArray, ...placeholders: string[]) { return "tagged" }
4+
//// const alpha = tag/*z*/`/*y*/foobar`
5+
//// const beta = tag/*x*/`/*w*/foobar ${/*v*/4/*u*/2}`
6+
7+
goTo.select("z", "y");
8+
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation");
9+
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal");
10+
11+
goTo.select("x", "w");
12+
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation");
13+
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal");
14+
15+
goTo.select("v", "u");
16+
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation");
17+
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal");
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*/4/*y*/2 + 6 + 23 + 12 +" 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 = \`\${42 + 6 + 23 + 12} years old\``,
12+
});

0 commit comments

Comments
 (0)