fix(40671): suggest ConvertStringToTemplateLiteral refactoring for string with property/element acceses elements (#40942)
This commit is contained in:
parent
b27d4bf3f6
commit
eb6ddf6b29
|
@ -73,10 +73,19 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getParentBinaryExpression(expr: Node) {
|
function getParentBinaryExpression(expr: Node) {
|
||||||
while (isBinaryExpression(expr.parent) && isNotEqualsOperator(expr.parent)) {
|
const container = findAncestor(expr.parent, n => {
|
||||||
expr = expr.parent;
|
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 expr;
|
});
|
||||||
|
|
||||||
|
return container || expr;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isStringConcatenationValid(node: Node): boolean {
|
function isStringConcatenationValid(node: Node): boolean {
|
||||||
|
|
|
@ -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")
|
||||||
|
});
|
|
@ -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")
|
||||||
|
});
|
Loading…
Reference in a new issue