mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-05 08:11:30 -06:00
Make getTextOfIdentifierOrLiteral and getEscapedTextOfIdentifierOrLiteral only accept Identifier | StringLiteralLike | NumericLiteral (#22002)
This commit is contained in:
parent
dda4bd0d0b
commit
8a52eade2e
@ -260,7 +260,7 @@ namespace ts {
|
||||
const name = getNameOfDeclaration(node);
|
||||
if (name) {
|
||||
if (isAmbientModule(node)) {
|
||||
const moduleName = getTextOfIdentifierOrLiteral(<Identifier | LiteralExpression>name);
|
||||
const moduleName = getTextOfIdentifierOrLiteral(name as Identifier | StringLiteral);
|
||||
return (isGlobalScopeAugmentation(<ModuleDeclaration>node) ? "__global" : `"${moduleName}"`) as __String;
|
||||
}
|
||||
if (name.kind === SyntaxKind.ComputedPropertyName) {
|
||||
@ -273,7 +273,7 @@ namespace ts {
|
||||
Debug.assert(isWellKnownSymbolSyntactically(nameExpression));
|
||||
return getPropertyNameForKnownSymbolName(idText((<PropertyAccessExpression>nameExpression).name));
|
||||
}
|
||||
return getEscapedTextOfIdentifierOrLiteral(<Identifier | LiteralExpression>name);
|
||||
return isPropertyNameLiteral(name) ? getEscapedTextOfIdentifierOrLiteral(name) : undefined;
|
||||
}
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.Constructor:
|
||||
|
||||
@ -23753,7 +23753,11 @@ namespace ts {
|
||||
|
||||
function checkExternalImportOrExportDeclaration(node: ImportDeclaration | ImportEqualsDeclaration | ExportDeclaration): boolean {
|
||||
const moduleName = getExternalModuleName(node);
|
||||
if (!nodeIsMissing(moduleName) && moduleName.kind !== SyntaxKind.StringLiteral) {
|
||||
if (nodeIsMissing(moduleName)) {
|
||||
// Should be a parse error.
|
||||
return false;
|
||||
}
|
||||
if (!isStringLiteral(moduleName)) {
|
||||
error(moduleName, Diagnostics.String_literal_expected);
|
||||
return false;
|
||||
}
|
||||
@ -23764,7 +23768,7 @@ namespace ts {
|
||||
Diagnostics.Import_declarations_in_a_namespace_cannot_reference_a_module);
|
||||
return false;
|
||||
}
|
||||
if (inAmbientExternalModule && isExternalModuleNameRelative(getTextOfIdentifierOrLiteral(<LiteralExpression | Identifier>moduleName))) {
|
||||
if (inAmbientExternalModule && isExternalModuleNameRelative(moduleName.text)) {
|
||||
// we have already reported errors on top level imports\exports in external module augmentations in checkModuleDeclaration
|
||||
// no need to do this again.
|
||||
if (!isTopLevelInExternalModuleAugmentation(node)) {
|
||||
|
||||
@ -101,7 +101,7 @@ namespace ts {
|
||||
return node;
|
||||
}
|
||||
|
||||
function createLiteralFromNode(sourceNode: StringLiteralLike | NumericLiteral | Identifier): StringLiteral {
|
||||
function createLiteralFromNode(sourceNode: PropertyNameLiteral): StringLiteral {
|
||||
const node = createStringLiteral(getTextOfIdentifierOrLiteral(sourceNode));
|
||||
node.textSourceNode = sourceNode;
|
||||
return node;
|
||||
|
||||
@ -2159,34 +2159,24 @@ namespace ts {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function getTextOfIdentifierOrLiteral(node: Identifier | LiteralLikeNode) {
|
||||
if (node) {
|
||||
if (node.kind === SyntaxKind.Identifier) {
|
||||
return idText(node as Identifier);
|
||||
}
|
||||
if (node.kind === SyntaxKind.StringLiteral ||
|
||||
node.kind === SyntaxKind.NumericLiteral) {
|
||||
|
||||
return node.text;
|
||||
}
|
||||
export type PropertyNameLiteral = Identifier | StringLiteralLike | NumericLiteral;
|
||||
export function isPropertyNameLiteral(node: Node): node is PropertyNameLiteral {
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.Identifier:
|
||||
case SyntaxKind.StringLiteral:
|
||||
case SyntaxKind.NoSubstitutionTemplateLiteral:
|
||||
case SyntaxKind.NumericLiteral:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
export function getTextOfIdentifierOrLiteral(node: PropertyNameLiteral): string {
|
||||
return node.kind === SyntaxKind.Identifier ? idText(node) : node.text;
|
||||
}
|
||||
|
||||
export function getEscapedTextOfIdentifierOrLiteral(node: Identifier | LiteralLikeNode) {
|
||||
if (node) {
|
||||
if (node.kind === SyntaxKind.Identifier) {
|
||||
return (node as Identifier).escapedText;
|
||||
}
|
||||
if (node.kind === SyntaxKind.StringLiteral ||
|
||||
node.kind === SyntaxKind.NumericLiteral) {
|
||||
|
||||
return escapeLeadingUnderscores(node.text);
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
export function getEscapedTextOfIdentifierOrLiteral(node: PropertyNameLiteral): __String {
|
||||
return node.kind === SyntaxKind.Identifier ? node.escapedText : escapeLeadingUnderscores(node.text);
|
||||
}
|
||||
|
||||
export function getPropertyNameForKnownSymbolName(symbolName: string): __String {
|
||||
|
||||
@ -1999,7 +1999,7 @@ namespace ts.Completions {
|
||||
// NOTE: if one only performs this step when m.name is an identifier,
|
||||
// things like '__proto__' are not filtered out.
|
||||
const name = getNameOfDeclaration(m);
|
||||
existingName = getEscapedTextOfIdentifierOrLiteral(name as (Identifier | LiteralExpression));
|
||||
existingName = isPropertyNameLiteral(name) ? getEscapedTextOfIdentifierOrLiteral(name) : undefined;
|
||||
}
|
||||
|
||||
existingMemberNames.set(existingName, true);
|
||||
|
||||
@ -89,45 +89,37 @@ namespace ts.NavigateTo {
|
||||
}
|
||||
|
||||
function tryAddSingleDeclarationName(declaration: Declaration, containers: string[]): boolean {
|
||||
if (declaration) {
|
||||
const name = getNameOfDeclaration(declaration);
|
||||
if (name) {
|
||||
const text = getTextOfIdentifierOrLiteral(name as (Identifier | LiteralExpression));
|
||||
if (text !== undefined) {
|
||||
containers.unshift(text);
|
||||
}
|
||||
else if (name.kind === SyntaxKind.ComputedPropertyName) {
|
||||
return tryAddComputedPropertyName(name.expression, containers, /*includeLastPortion*/ true);
|
||||
}
|
||||
else {
|
||||
// Don't know how to add this.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
const name = getNameOfDeclaration(declaration);
|
||||
if (name && isPropertyNameLiteral(name)) {
|
||||
containers.unshift(getTextOfIdentifierOrLiteral(name));
|
||||
return true;
|
||||
}
|
||||
else if (name && name.kind === SyntaxKind.ComputedPropertyName) {
|
||||
return tryAddComputedPropertyName(name.expression, containers, /*includeLastPortion*/ true);
|
||||
}
|
||||
else {
|
||||
// Don't know how to add this.
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Only added the names of computed properties if they're simple dotted expressions, like:
|
||||
//
|
||||
// [X.Y.Z]() { }
|
||||
function tryAddComputedPropertyName(expression: Expression, containers: string[], includeLastPortion: boolean): boolean {
|
||||
const text = getTextOfIdentifierOrLiteral(expression as LiteralExpression);
|
||||
if (text !== undefined) {
|
||||
if (isPropertyNameLiteral(expression)) {
|
||||
const text = getTextOfIdentifierOrLiteral(expression);
|
||||
if (includeLastPortion) {
|
||||
containers.unshift(text);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (expression.kind === SyntaxKind.PropertyAccessExpression) {
|
||||
const propertyAccess = <PropertyAccessExpression>expression;
|
||||
if (isPropertyAccessExpression(expression)) {
|
||||
if (includeLastPortion) {
|
||||
containers.unshift(propertyAccess.name.text);
|
||||
containers.unshift(expression.name.text);
|
||||
}
|
||||
|
||||
return tryAddComputedPropertyName(propertyAccess.expression, containers, /*includeLastPortion*/ true);
|
||||
return tryAddComputedPropertyName(expression.expression, containers, /*includeLastPortion*/ true);
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
@ -721,23 +721,8 @@ namespace ts {
|
||||
|
||||
function getDeclarationName(declaration: Declaration) {
|
||||
const name = getNameOfDeclaration(declaration);
|
||||
if (name) {
|
||||
const result = getTextOfIdentifierOrLiteral(name as (Identifier | LiteralExpression));
|
||||
if (result !== undefined) {
|
||||
return result;
|
||||
}
|
||||
|
||||
if (name.kind === SyntaxKind.ComputedPropertyName) {
|
||||
const expr = name.expression;
|
||||
if (expr.kind === SyntaxKind.PropertyAccessExpression) {
|
||||
return (<PropertyAccessExpression>expr).name.text;
|
||||
}
|
||||
|
||||
return getTextOfIdentifierOrLiteral(expr as (Identifier | LiteralExpression));
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
return name && (isPropertyNameLiteral(name) ? getTextOfIdentifierOrLiteral(name) :
|
||||
name.kind === SyntaxKind.ComputedPropertyName && isPropertyAccessExpression(name.expression) ? name.expression.name.text : undefined);
|
||||
}
|
||||
|
||||
function visit(node: Node): void {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user