Skip to content

Commit 0b21d9b

Browse files
committed
DRAFT: Support renames with template literals
1 parent 1812d7c commit 0b21d9b

File tree

6 files changed

+44
-5
lines changed

6 files changed

+44
-5
lines changed

src/compiler/utilities.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,6 +1847,10 @@ namespace ts {
18471847
return getSourceTextOfNodeFromSourceFile(sourceFile, str).charCodeAt(0) === CharacterCodes.doubleQuote;
18481848
}
18491849

1850+
export function isSingleOrDoubleQuoteOrBacktick(charCode: number) {
1851+
return charCode === CharacterCodes.singleQuote || charCode === CharacterCodes.doubleQuote || charCode === CharacterCodes.backtick;
1852+
}
1853+
18501854
export function getDeclarationOfExpando(node: Node): Node | undefined {
18511855
if (!node.parent) {
18521856
return undefined;
@@ -3236,6 +3240,14 @@ namespace ts {
32363240
return isSingleOrDoubleQuote(name.charCodeAt(0));
32373241
}
32383242

3243+
export function stripQuotesOrBacktics(name: string) {
3244+
const length = name.length;
3245+
if (length >= 2 && name.charCodeAt(0) === name.charCodeAt(length - 1) && isSingleOrDoubleQuoteOrBacktick(name.charCodeAt(0))) {
3246+
return name.substring(1, length - 1);
3247+
}
3248+
return name;
3249+
}
3250+
32393251
function getReplacement(c: string, offset: number, input: string) {
32403252
if (c.charCodeAt(0) === CharacterCodes.nullCharacter) {
32413253
const lookAhead = input.charCodeAt(offset + c.length);

src/services/findAllReferences.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ namespace ts.FindAllReferences {
448448
function getTextSpan(node: Node, sourceFile: SourceFile, endNode?: Node): TextSpan {
449449
let start = node.getStart(sourceFile);
450450
let end = (endNode || node).getEnd();
451-
if (node.kind === SyntaxKind.StringLiteral) {
451+
if (isStringLiteralLike(node)) {
452452
Debug.assert(endNode === undefined);
453453
start += 1;
454454
end -= 1;
@@ -1234,8 +1234,9 @@ namespace ts.FindAllReferences.Core {
12341234
case SyntaxKind.Identifier:
12351235
return (node as Identifier).text.length === searchSymbolName.length;
12361236

1237+
case SyntaxKind.NoSubstitutionTemplateLiteral:
12371238
case SyntaxKind.StringLiteral: {
1238-
const str = node as StringLiteral;
1239+
const str = node as StringLiteralLike;
12391240
return (isLiteralNameOfPropertyDeclarationOrIndexAccess(str) || isNameOfModuleDeclaration(node) || isExpressionOfExternalModuleImportEqualsDeclaration(node) || (isCallExpression(node.parent) && isBindableObjectDefinePropertyCall(node.parent) && node.parent.arguments[1] === node)) &&
12401241
str.text.length === searchSymbolName.length;
12411242
}

src/services/rename.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace ts.Rename {
3131

3232
const kind = SymbolDisplay.getSymbolKind(typeChecker, symbol, node);
3333
const specifierName = (isImportOrExportSpecifierName(node) || isStringOrNumericLiteralLike(node) && node.parent.kind === SyntaxKind.ComputedPropertyName)
34-
? stripQuotes(getTextOfIdentifierOrLiteral(node))
34+
? stripQuotesOrBacktics(getTextOfIdentifierOrLiteral(node))
3535
: undefined;
3636
const displayName = specifierName || typeChecker.symbolToString(symbol);
3737
const fullDisplayName = specifierName || typeChecker.getFullyQualifiedName(symbol);
@@ -81,7 +81,7 @@ namespace ts.Rename {
8181
function createTriggerSpanForNode(node: Node, sourceFile: SourceFile) {
8282
let start = node.getStart(sourceFile);
8383
let width = node.getWidth(sourceFile);
84-
if (node.kind === SyntaxKind.StringLiteral) {
84+
if (isStringLiteralLike(node)) {
8585
// Exclude the quotes
8686
start += 1;
8787
width -= 2;
@@ -93,6 +93,7 @@ namespace ts.Rename {
9393
switch (node.kind) {
9494
case SyntaxKind.Identifier:
9595
case SyntaxKind.StringLiteral:
96+
case SyntaxKind.NoSubstitutionTemplateLiteral:
9697
case SyntaxKind.ThisKeyword:
9798
return true;
9899
case SyntaxKind.NumericLiteral:

src/services/services.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2227,6 +2227,7 @@ namespace ts {
22272227
function getContainingObjectLiteralElementWorker(node: Node): ObjectLiteralElement | undefined {
22282228
switch (node.kind) {
22292229
case SyntaxKind.StringLiteral:
2230+
case SyntaxKind.NoSubstitutionTemplateLiteral:
22302231
case SyntaxKind.NumericLiteral:
22312232
if (node.parent.kind === SyntaxKind.ComputedPropertyName) {
22322233
return isObjectLiteralElement(node.parent.parent) ? node.parent.parent : undefined;

src/services/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ namespace ts {
267267
isFunctionLike(node.parent) && (<FunctionLikeDeclaration>node.parent).name === node;
268268
}
269269

270-
export function isLiteralNameOfPropertyDeclarationOrIndexAccess(node: StringLiteral | NumericLiteral): boolean {
270+
export function isLiteralNameOfPropertyDeclarationOrIndexAccess(node: StringLiteral | NumericLiteral | NoSubstitutionTemplateLiteral): boolean {
271271
switch (node.parent.kind) {
272272
case SyntaxKind.PropertyDeclaration:
273273
case SyntaxKind.PropertySignature:
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////var o = {
4+
//// [|[`[|{| "contextRangeIndex": 0 |}tplProp|]`]: 0|],
5+
//// [|['[|{| "contextRangeIndex": 2 |}strProp|]']: 0|]
6+
////};
7+
////
8+
////o = {
9+
//// [|[`[|{| "contextRangeIndex": 4 |}tplProp|]`]: 1|],
10+
//// [|['[|{| "contextRangeIndex": 6 |}strProp|]']: 1|]
11+
////};
12+
////
13+
////o.[|tplProp|];
14+
////o['[|tplProp|]'];
15+
////o["[|tplProp|]"];
16+
////o[`[|tplProp|]`];
17+
////
18+
////o.[|strProp|];
19+
////o['[|strProp|]'];
20+
////o["[|strProp|]"];
21+
////o[`[|strProp|]`];
22+
23+
verify.rangesWithSameTextAreRenameLocations("tplProp");
24+
verify.rangesWithSameTextAreRenameLocations("strProp");

0 commit comments

Comments
 (0)