Skip to content

Commit 8a52ead

Browse files
author
Andy
authored
Make getTextOfIdentifierOrLiteral and getEscapedTextOfIdentifierOrLiteral only accept Identifier | StringLiteralLike | NumericLiteral (microsoft#22002)
1 parent dda4bd0 commit 8a52ead

File tree

7 files changed

+43
-72
lines changed

7 files changed

+43
-72
lines changed

src/compiler/binder.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ namespace ts {
260260
const name = getNameOfDeclaration(node);
261261
if (name) {
262262
if (isAmbientModule(node)) {
263-
const moduleName = getTextOfIdentifierOrLiteral(<Identifier | LiteralExpression>name);
263+
const moduleName = getTextOfIdentifierOrLiteral(name as Identifier | StringLiteral);
264264
return (isGlobalScopeAugmentation(<ModuleDeclaration>node) ? "__global" : `"${moduleName}"`) as __String;
265265
}
266266
if (name.kind === SyntaxKind.ComputedPropertyName) {
@@ -273,7 +273,7 @@ namespace ts {
273273
Debug.assert(isWellKnownSymbolSyntactically(nameExpression));
274274
return getPropertyNameForKnownSymbolName(idText((<PropertyAccessExpression>nameExpression).name));
275275
}
276-
return getEscapedTextOfIdentifierOrLiteral(<Identifier | LiteralExpression>name);
276+
return isPropertyNameLiteral(name) ? getEscapedTextOfIdentifierOrLiteral(name) : undefined;
277277
}
278278
switch (node.kind) {
279279
case SyntaxKind.Constructor:

src/compiler/checker.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -23753,7 +23753,11 @@ namespace ts {
2375323753

2375423754
function checkExternalImportOrExportDeclaration(node: ImportDeclaration | ImportEqualsDeclaration | ExportDeclaration): boolean {
2375523755
const moduleName = getExternalModuleName(node);
23756-
if (!nodeIsMissing(moduleName) && moduleName.kind !== SyntaxKind.StringLiteral) {
23756+
if (nodeIsMissing(moduleName)) {
23757+
// Should be a parse error.
23758+
return false;
23759+
}
23760+
if (!isStringLiteral(moduleName)) {
2375723761
error(moduleName, Diagnostics.String_literal_expected);
2375823762
return false;
2375923763
}
@@ -23764,7 +23768,7 @@ namespace ts {
2376423768
Diagnostics.Import_declarations_in_a_namespace_cannot_reference_a_module);
2376523769
return false;
2376623770
}
23767-
if (inAmbientExternalModule && isExternalModuleNameRelative(getTextOfIdentifierOrLiteral(<LiteralExpression | Identifier>moduleName))) {
23771+
if (inAmbientExternalModule && isExternalModuleNameRelative(moduleName.text)) {
2376823772
// we have already reported errors on top level imports\exports in external module augmentations in checkModuleDeclaration
2376923773
// no need to do this again.
2377023774
if (!isTopLevelInExternalModuleAugmentation(node)) {

src/compiler/factory.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ namespace ts {
101101
return node;
102102
}
103103

104-
function createLiteralFromNode(sourceNode: StringLiteralLike | NumericLiteral | Identifier): StringLiteral {
104+
function createLiteralFromNode(sourceNode: PropertyNameLiteral): StringLiteral {
105105
const node = createStringLiteral(getTextOfIdentifierOrLiteral(sourceNode));
106106
node.textSourceNode = sourceNode;
107107
return node;

src/compiler/utilities.ts

+15-25
Original file line numberDiff line numberDiff line change
@@ -2159,34 +2159,24 @@ namespace ts {
21592159
return undefined;
21602160
}
21612161

2162-
export function getTextOfIdentifierOrLiteral(node: Identifier | LiteralLikeNode) {
2163-
if (node) {
2164-
if (node.kind === SyntaxKind.Identifier) {
2165-
return idText(node as Identifier);
2166-
}
2167-
if (node.kind === SyntaxKind.StringLiteral ||
2168-
node.kind === SyntaxKind.NumericLiteral) {
2169-
2170-
return node.text;
2171-
}
2162+
export type PropertyNameLiteral = Identifier | StringLiteralLike | NumericLiteral;
2163+
export function isPropertyNameLiteral(node: Node): node is PropertyNameLiteral {
2164+
switch (node.kind) {
2165+
case SyntaxKind.Identifier:
2166+
case SyntaxKind.StringLiteral:
2167+
case SyntaxKind.NoSubstitutionTemplateLiteral:
2168+
case SyntaxKind.NumericLiteral:
2169+
return true;
2170+
default:
2171+
return false;
21722172
}
2173-
2174-
return undefined;
2173+
}
2174+
export function getTextOfIdentifierOrLiteral(node: PropertyNameLiteral): string {
2175+
return node.kind === SyntaxKind.Identifier ? idText(node) : node.text;
21752176
}
21762177

2177-
export function getEscapedTextOfIdentifierOrLiteral(node: Identifier | LiteralLikeNode) {
2178-
if (node) {
2179-
if (node.kind === SyntaxKind.Identifier) {
2180-
return (node as Identifier).escapedText;
2181-
}
2182-
if (node.kind === SyntaxKind.StringLiteral ||
2183-
node.kind === SyntaxKind.NumericLiteral) {
2184-
2185-
return escapeLeadingUnderscores(node.text);
2186-
}
2187-
}
2188-
2189-
return undefined;
2178+
export function getEscapedTextOfIdentifierOrLiteral(node: PropertyNameLiteral): __String {
2179+
return node.kind === SyntaxKind.Identifier ? node.escapedText : escapeLeadingUnderscores(node.text);
21902180
}
21912181

21922182
export function getPropertyNameForKnownSymbolName(symbolName: string): __String {

src/services/completions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1999,7 +1999,7 @@ namespace ts.Completions {
19991999
// NOTE: if one only performs this step when m.name is an identifier,
20002000
// things like '__proto__' are not filtered out.
20012001
const name = getNameOfDeclaration(m);
2002-
existingName = getEscapedTextOfIdentifierOrLiteral(name as (Identifier | LiteralExpression));
2002+
existingName = isPropertyNameLiteral(name) ? getEscapedTextOfIdentifierOrLiteral(name) : undefined;
20032003
}
20042004

20052005
existingMemberNames.set(existingName, true);

src/services/navigateTo.ts

+16-24
Original file line numberDiff line numberDiff line change
@@ -89,45 +89,37 @@ namespace ts.NavigateTo {
8989
}
9090

9191
function tryAddSingleDeclarationName(declaration: Declaration, containers: string[]): boolean {
92-
if (declaration) {
93-
const name = getNameOfDeclaration(declaration);
94-
if (name) {
95-
const text = getTextOfIdentifierOrLiteral(name as (Identifier | LiteralExpression));
96-
if (text !== undefined) {
97-
containers.unshift(text);
98-
}
99-
else if (name.kind === SyntaxKind.ComputedPropertyName) {
100-
return tryAddComputedPropertyName(name.expression, containers, /*includeLastPortion*/ true);
101-
}
102-
else {
103-
// Don't know how to add this.
104-
return false;
105-
}
106-
}
92+
const name = getNameOfDeclaration(declaration);
93+
if (name && isPropertyNameLiteral(name)) {
94+
containers.unshift(getTextOfIdentifierOrLiteral(name));
95+
return true;
96+
}
97+
else if (name && name.kind === SyntaxKind.ComputedPropertyName) {
98+
return tryAddComputedPropertyName(name.expression, containers, /*includeLastPortion*/ true);
99+
}
100+
else {
101+
// Don't know how to add this.
102+
return false;
107103
}
108-
109-
return true;
110104
}
111105

112106
// Only added the names of computed properties if they're simple dotted expressions, like:
113107
//
114108
// [X.Y.Z]() { }
115109
function tryAddComputedPropertyName(expression: Expression, containers: string[], includeLastPortion: boolean): boolean {
116-
const text = getTextOfIdentifierOrLiteral(expression as LiteralExpression);
117-
if (text !== undefined) {
110+
if (isPropertyNameLiteral(expression)) {
111+
const text = getTextOfIdentifierOrLiteral(expression);
118112
if (includeLastPortion) {
119113
containers.unshift(text);
120114
}
121115
return true;
122116
}
123-
124-
if (expression.kind === SyntaxKind.PropertyAccessExpression) {
125-
const propertyAccess = <PropertyAccessExpression>expression;
117+
if (isPropertyAccessExpression(expression)) {
126118
if (includeLastPortion) {
127-
containers.unshift(propertyAccess.name.text);
119+
containers.unshift(expression.name.text);
128120
}
129121

130-
return tryAddComputedPropertyName(propertyAccess.expression, containers, /*includeLastPortion*/ true);
122+
return tryAddComputedPropertyName(expression.expression, containers, /*includeLastPortion*/ true);
131123
}
132124

133125
return false;

src/services/services.ts

+2-17
Original file line numberDiff line numberDiff line change
@@ -721,23 +721,8 @@ namespace ts {
721721

722722
function getDeclarationName(declaration: Declaration) {
723723
const name = getNameOfDeclaration(declaration);
724-
if (name) {
725-
const result = getTextOfIdentifierOrLiteral(name as (Identifier | LiteralExpression));
726-
if (result !== undefined) {
727-
return result;
728-
}
729-
730-
if (name.kind === SyntaxKind.ComputedPropertyName) {
731-
const expr = name.expression;
732-
if (expr.kind === SyntaxKind.PropertyAccessExpression) {
733-
return (<PropertyAccessExpression>expr).name.text;
734-
}
735-
736-
return getTextOfIdentifierOrLiteral(expr as (Identifier | LiteralExpression));
737-
}
738-
}
739-
740-
return undefined;
724+
return name && (isPropertyNameLiteral(name) ? getTextOfIdentifierOrLiteral(name) :
725+
name.kind === SyntaxKind.ComputedPropertyName && isPropertyAccessExpression(name.expression) ? name.expression.name.text : undefined);
741726
}
742727

743728
function visit(node: Node): void {

0 commit comments

Comments
 (0)