Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/services/refactors/inlineVariable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import {
isObjectLiteralExpression,
isPropertyAccessExpression,
isShorthandPropertyAssignment,
isStringLiteral,
isTaggedTemplateExpression,
isTemplateSpan,
isTypeQueryNode,
isVariableDeclarationInVariableStatement,
isVariableStatement,
Expand All @@ -33,7 +36,9 @@ import {
refactor,
some,
SourceFile,
StringLiteral,
SymbolFlags,
TemplateSpan,
textChanges,
textRangeContainsPositionInclusive,
TypeChecker,
Expand Down Expand Up @@ -115,7 +120,12 @@ registerRefactor(refactorName, {
const { references, declaration, replacement } = info;
const edits = textChanges.ChangeTracker.with(context, tracker => {
for (const node of references) {
tracker.replaceNode(file, node, getReplacementExpression(node, replacement));
if (isStringLiteral(replacement) && isIdentifier(node) && isTemplateSpan(node.parent) && !isTaggedTemplateExpression(node.parent.parent.parent)) {
replaceTemplateStringVariableWithLiteral(tracker, file, node.parent, replacement);
}
else {
tracker.replaceNode(file, node, getReplacementExpression(node, replacement));
}
}
tracker.delete(file, declaration);
});
Expand Down Expand Up @@ -255,3 +265,17 @@ function getReplacementExpression(reference: Node, replacement: Expression) {

return replacement;
}

function replaceTemplateStringVariableWithLiteral(tracker: textChanges.ChangeTracker, sourceFile: SourceFile, reference: TemplateSpan, replacement: StringLiteral) {
const templateExpression = reference.parent;
const index = templateExpression.templateSpans.indexOf(reference);
const prevNode = index === 0 ? templateExpression.head : templateExpression.templateSpans[index - 1];
tracker.replaceRangeWithText(
sourceFile,
{
pos: prevNode.getEnd() - 2,
end: reference.literal.getStart() + 1,
},
replacement.text.replace(/\\/g, "\\\\").replace(/`/g, "\\`"),
);
}
13 changes: 13 additions & 0 deletions tests/cases/fourslash/inlineVariableTemplateString1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path="fourslash.ts" />

////const /*a*/pizza/*b*/ = "🍕";
////export const prompt = `Hello, would you like some ${pizza}?`;

goTo.select("a", "b");
verify.refactorAvailable("Inline variable");
edit.applyRefactor({
refactorName: "Inline variable",
actionName: "Inline variable",
actionDescription: "Inline variable",
newContent: "export const prompt = `Hello, would you like some 🍕?`;"
});
13 changes: 13 additions & 0 deletions tests/cases/fourslash/inlineVariableTemplateString2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path="fourslash.ts" />

////const /*a*/codeText/*b*/ = "Code-formatted text looks `like this` and requires surrounding by backticks (\\`).";
////export const mdTutorial = `Let's talk about markdown.\n${codeText}?`;

goTo.select("a", "b");
verify.refactorAvailable("Inline variable");
edit.applyRefactor({
refactorName: "Inline variable",
actionName: "Inline variable",
actionDescription: "Inline variable",
newContent: "export const mdTutorial = `Let's talk about markdown.\\nCode-formatted text looks \\`like this\\` and requires surrounding by backticks (\\\\\\\`).?`;"
});
15 changes: 15 additions & 0 deletions tests/cases/fourslash/inlineVariableTemplateString3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// <reference path="fourslash.ts" />

////const /*a*/pizza/*b*/ = "🍕";
////export const prompt = `Hello, would you like some ${
//// pizza
//// }?`;

goTo.select("a", "b");
verify.refactorAvailable("Inline variable");
edit.applyRefactor({
refactorName: "Inline variable",
actionName: "Inline variable",
actionDescription: "Inline variable",
newContent: "export const prompt = `Hello, would you like some 🍕?`;"
});
13 changes: 13 additions & 0 deletions tests/cases/fourslash/inlineVariableTemplateString4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path="fourslash.ts" />

////const /*a*/pizza/*b*/ = "🍕";
////export const prompt = `Hello, would you like some ${pizza} or ${pizza}?`;

goTo.select("a", "b");
verify.refactorAvailable("Inline variable");
edit.applyRefactor({
refactorName: "Inline variable",
actionName: "Inline variable",
actionDescription: "Inline variable",
newContent: "export const prompt = `Hello, would you like some 🍕 or 🍕?`;"
});
13 changes: 13 additions & 0 deletions tests/cases/fourslash/inlineVariableTemplateString5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path="fourslash.ts" />

////const /*a*/pizza/*b*/ = "🍕";
////export const prompt = `Hello, would you like some ${pizza}`;

goTo.select("a", "b");
verify.refactorAvailable("Inline variable");
edit.applyRefactor({
refactorName: "Inline variable",
actionName: "Inline variable",
actionDescription: "Inline variable",
newContent: "export const prompt = `Hello, would you like some 🍕`;"
});
13 changes: 13 additions & 0 deletions tests/cases/fourslash/inlineVariableTemplateString6.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path="fourslash.ts" />

////const /*a*/x/*b*/ = "\\`";
////export const y = `${x}`;

goTo.select("a", "b");
verify.refactorAvailable("Inline variable");
edit.applyRefactor({
refactorName: "Inline variable",
actionName: "Inline variable",
actionDescription: "Inline variable",
newContent: "export const y = `\\\\\\``;"
});
13 changes: 13 additions & 0 deletions tests/cases/fourslash/inlineVariableTemplateString7.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path="fourslash.ts" />

////const /*a*/x/*b*/ = "`";
////export const y = `${x}`;

goTo.select("a", "b");
verify.refactorAvailable("Inline variable");
edit.applyRefactor({
refactorName: "Inline variable",
actionName: "Inline variable",
actionDescription: "Inline variable",
newContent: "export const y = `\\``;"
});
13 changes: 13 additions & 0 deletions tests/cases/fourslash/inlineVariableTemplateString8.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path="fourslash.ts" />

////const /*a*/message/*b*/ = "Hello, World!";
////await $`echo ${message}`;

goTo.select("a", "b");
verify.refactorAvailable("Inline variable");
edit.applyRefactor({
refactorName: "Inline variable",
actionName: "Inline variable",
actionDescription: "Inline variable",
newContent: 'await $`echo ${"Hello, World!"}`;',
});