do not offer refactoring for tagged templates

This commit is contained in:
BigAru
2018-12-07 05:58:00 +01:00
parent 6fe4663d92
commit 6de23d766a
4 changed files with 32 additions and 5 deletions

View File

@@ -9,7 +9,6 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
const toStringConcatenationDescription = getLocaleSpecificMessage(Diagnostics.Convert_to_string_concatenation);
// TODO let a = 45 - 45 + " ee" - 33;
// TODO let a = tag `aaa`;
registerRefactor(refactorName, { getEditsForAction, getAvailableActions });
@@ -17,13 +16,14 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
const { file, startPosition } = context;
const node = getTokenAtPosition(file, startPosition);
const maybeBinary = getParentBinaryExpression(node); containsString(maybeBinary);
const maybeTemplateExpression = findAncestor(node, n => isTemplateExpression(n));
const actions: RefactorActionInfo[] = [];
if ((isBinaryExpression(maybeBinary) || isStringLiteral(maybeBinary)) && containsString(maybeBinary)) {
actions.push({ name: toTemplateLiteralActionName, description: toTemplateLiteralDescription });
}
if (isNoSubstitutionTemplateLiteral(node) || isTemplateHead(node) || isTemplateSpan(node.parent)) {
if ((isNoSubstitutionTemplateLiteral(node) && !isTaggedTemplateExpression(node.parent)) || (maybeTemplateExpression && !isTaggedTemplateExpression(maybeTemplateExpression.parent))) {
actions.push({ name: toStringConcatenationActionName, description: toStringConcatenationDescription });
}
@@ -46,8 +46,7 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
case toStringConcatenationActionName:
if (isNoSubstitutionTemplateLiteral(node)) {
const stringLiteral = createStringLiteral(node.text);
return { edits: textChanges.ChangeTracker.with(context, t => t.replaceNode(file, node, stringLiteral)) };
return { edits: textChanges.ChangeTracker.with(context, t => t.replaceNode(file, node, stringLiteral)) };
}
if (isTemplateExpression(node.parent) || isTemplateSpan(node.parent)) {

View File

@@ -27,4 +27,3 @@ verify.not.refactorAvailable("Convert string concatenation or template literal",
goTo.select("p", "o");
verify.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation");
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal");

View File

@@ -0,0 +1,17 @@
/// <reference path='fourslash.ts' />
//// function tag(literals: TemplateStringsArray, ...placeholders: string[]) { return "tagged" }
//// const alpha = tag/*z*/`/*y*/foobar`
//// const beta = tag/*x*/`/*w*/foobar ${/*v*/4/*u*/2}`
goTo.select("z", "y");
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");
goTo.select("x", "w");
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");
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");

View File

@@ -0,0 +1,12 @@
/// <reference path='fourslash.ts' />
//// const foo = /*x*/4/*y*/2 + 6 + 23 + 12 +" 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 = \`\${42 + 6 + 23 + 12} years old\``,
});