fix(40671): suggest ConvertStringToTemplateLiteral refactoring for string with property/element acceses elements (#40942)

This commit is contained in:
Oleksandr T 2020-10-30 00:23:11 +02:00 committed by GitHub
parent b27d4bf3f6
commit eb6ddf6b29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 4 deletions

View file

@ -73,10 +73,19 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
}
function getParentBinaryExpression(expr: Node) {
while (isBinaryExpression(expr.parent) && isNotEqualsOperator(expr.parent)) {
expr = expr.parent;
}
return expr;
const container = findAncestor(expr.parent, n => {
switch (n.kind) {
case SyntaxKind.PropertyAccessExpression:
case SyntaxKind.ElementAccessExpression:
return false;
case SyntaxKind.BinaryExpression:
return !(isBinaryExpression(n.parent) && isNotEqualsOperator(n.parent));
default:
return "quit";
}
});
return container || expr;
}
function isStringConcatenationValid(node: Node): boolean {

View file

@ -0,0 +1,15 @@
/// <reference path='fourslash.ts' />
////const a = { prop: 1 };
////const b = /*x*/a["prop"]/*y*/ + "a" + "b";
goTo.select("x", "y");
edit.applyRefactor({
refactorName: "Convert to template string",
actionName: "Convert to template string",
actionDescription: ts.Diagnostics.Convert_to_template_string.message,
newContent: [
"const a = { prop: 1 };",
"const b = `${a[\"prop\"]}ab`;"
].join("\n")
});

View file

@ -0,0 +1,15 @@
/// <reference path='fourslash.ts' />
////const a = { prop: 1 };
////const b = /*x*/a.prop/*y*/ + "a" + "b";
goTo.select("x", "y");
edit.applyRefactor({
refactorName: "Convert to template string",
actionName: "Convert to template string",
actionDescription: ts.Diagnostics.Convert_to_template_string.message,
newContent: [
"const a = { prop: 1 };",
"const b = `${a.prop}ab`;"
].join("\n")
});