Skip to content

Commit 4816ac8

Browse files
author
Andy Hanson
committed
Treat NoSubstitutionTemplateLiteral like StringLiteral in more places
1 parent 01f6093 commit 4816ac8

File tree

8 files changed

+16
-12
lines changed

8 files changed

+16
-12
lines changed

src/compiler/binder.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ namespace ts {
259259
if (name.kind === SyntaxKind.ComputedPropertyName) {
260260
const nameExpression = name.expression;
261261
// treat computed property names where expression is string/numeric literal as just string/numeric literal
262-
if (isStringOrNumericLiteral(nameExpression)) {
262+
if (isStringOrNumericLiteralLike(nameExpression)) {
263263
return escapeLeadingUnderscores(nameExpression.text);
264264
}
265265

src/compiler/checker.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4442,7 +4442,7 @@ namespace ts {
44424442
}
44434443

44444444
function isComputedNonLiteralName(name: PropertyName): boolean {
4445-
return name.kind === SyntaxKind.ComputedPropertyName && !isStringOrNumericLiteral(name.expression);
4445+
return name.kind === SyntaxKind.ComputedPropertyName && !isStringOrNumericLiteralLike(name.expression);
44464446
}
44474447

44484448
function getRestType(source: Type, properties: PropertyName[], symbol: Symbol | undefined): Type {
@@ -13715,7 +13715,7 @@ namespace ts {
1371513715
case SyntaxKind.Identifier:
1371613716
return idText(name);
1371713717
case SyntaxKind.ComputedPropertyName:
13718-
return isStringOrNumericLiteral(name.expression) ? name.expression.text : undefined;
13718+
return isStringOrNumericLiteralLike(name.expression) ? name.expression.text : undefined;
1371913719
case SyntaxKind.StringLiteral:
1372013720
case SyntaxKind.NumericLiteral:
1372113721
return name.text;

src/compiler/parser.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4465,7 +4465,7 @@ namespace ts {
44654465
}
44664466
else {
44674467
const argument = allowInAnd(parseExpression);
4468-
if (isStringOrNumericLiteral(argument)) {
4468+
if (isStringOrNumericLiteralLike(argument)) {
44694469
argument.text = internIdentifier(argument.text);
44704470
}
44714471
indexedAccess.argumentExpression = argument;

src/compiler/transformers/destructuring.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ namespace ts {
447447
const argumentExpression = ensureIdentifier(flattenContext, visitNode(propertyName.expression, flattenContext.visitor), /*reuseIdentifierExpressions*/ false, /*location*/ propertyName);
448448
return createElementAccess(value, argumentExpression);
449449
}
450-
else if (isStringOrNumericLiteral(propertyName)) {
450+
else if (isStringOrNumericLiteralLike(propertyName)) {
451451
const argumentExpression = getSynthesizedClone(propertyName);
452452
argumentExpression.text = argumentExpression.text;
453453
return createElementAccess(value, argumentExpression);

src/compiler/utilities.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -767,9 +767,9 @@ namespace ts {
767767
case SyntaxKind.NumericLiteral:
768768
return escapeLeadingUnderscores(name.text);
769769
case SyntaxKind.ComputedPropertyName:
770-
return isStringOrNumericLiteral(name.expression) ? escapeLeadingUnderscores(name.expression.text) : undefined!; // TODO: GH#18217 Almost all uses of this assume the result to be defined!
770+
return isStringOrNumericLiteralLike(name.expression) ? escapeLeadingUnderscores(name.expression.text) : undefined!; // TODO: GH#18217 Almost all uses of this assume the result to be defined!
771771
default:
772-
Debug.assertNever(name);
772+
return Debug.assertNever(name);
773773
}
774774
}
775775

@@ -2566,6 +2566,10 @@ namespace ts {
25662566
|| kind === SyntaxKind.NumericLiteral;
25672567
}
25682568

2569+
export function isStringOrNumericLiteralLike(node: Node): node is StringLiteralLike | NumericLiteral {
2570+
return isStringOrNumericLiteral(node) || isNoSubstitutionTemplateLiteral(node);
2571+
}
2572+
25692573
/**
25702574
* A declaration has a dynamic name if both of the following are true:
25712575
* 1. The declaration has a computed property name
@@ -2580,7 +2584,7 @@ namespace ts {
25802584

25812585
export function isDynamicName(name: DeclarationName): boolean {
25822586
return name.kind === SyntaxKind.ComputedPropertyName &&
2583-
!isStringOrNumericLiteral(name.expression) &&
2587+
!isStringOrNumericLiteralLike(name.expression) &&
25842588
!isWellKnownSymbolSyntactically(name.expression);
25852589
}
25862590

src/services/rename.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace ts.Rename {
2929
if (isStringLiteralLike(node) && tryGetImportFromModuleSpecifier(node)) return undefined;
3030

3131
const kind = SymbolDisplay.getSymbolKind(typeChecker, symbol, node);
32-
const specifierName = (isImportOrExportSpecifierName(node) || isStringOrNumericLiteral(node) && node.parent.kind === SyntaxKind.ComputedPropertyName)
32+
const specifierName = (isImportOrExportSpecifierName(node) || isStringOrNumericLiteralLike(node) && node.parent.kind === SyntaxKind.ComputedPropertyName)
3333
? stripQuotes(getTextOfIdentifierOrLiteral(node))
3434
: undefined;
3535
const displayName = specifierName || typeChecker.symbolToString(symbol);

src/services/services.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2142,7 +2142,7 @@ namespace ts {
21422142
function initializeNameTable(sourceFile: SourceFile): void {
21432143
const nameTable = sourceFile.nameTable = createUnderscoreEscapedMap<number>();
21442144
sourceFile.forEachChild(function walk(node) {
2145-
if (isIdentifier(node) && node.escapedText || isStringOrNumericLiteral(node) && literalIsName(node)) {
2145+
if (isIdentifier(node) && node.escapedText || isStringOrNumericLiteralLike(node) && literalIsName(node)) {
21462146
const text = getEscapedTextOfIdentifierOrLiteral(node);
21472147
nameTable.set(text, nameTable.get(text) === undefined ? node.pos : -1);
21482148
}
@@ -2162,7 +2162,7 @@ namespace ts {
21622162
* then we want 'something' to be in the name table. Similarly, if we have
21632163
* "a['propname']" then we want to store "propname" in the name table.
21642164
*/
2165-
function literalIsName(node: StringLiteral | NumericLiteral): boolean {
2165+
function literalIsName(node: StringLiteralLike | NumericLiteral): boolean {
21662166
return isDeclarationName(node) ||
21672167
node.parent.kind === SyntaxKind.ExternalModuleReference ||
21682168
isArgumentOfElementAccessExpression(node) ||

src/services/utilities.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,7 @@ namespace ts {
12401240
export function getNameFromPropertyName(name: PropertyName): string | undefined {
12411241
return name.kind === SyntaxKind.ComputedPropertyName
12421242
// treat computed property names where expression is string/numeric literal as just string/numeric literal
1243-
? isStringOrNumericLiteral(name.expression) ? name.expression.text : undefined
1243+
? isStringOrNumericLiteralLike(name.expression) ? name.expression.text : undefined
12441244
: getTextOfIdentifierOrLiteral(name);
12451245
}
12461246

0 commit comments

Comments
 (0)