Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow PrivateIdentifier in QualifiedName #48640

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 41 additions & 22 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5490,16 +5490,20 @@ namespace ts {
qualifier = factory.updateIdentifier(qualifier, typeArguments);
}
else {
qualifier = factory.updateQualifiedName(qualifier,
qualifier.left,
factory.updateIdentifier(qualifier.right, typeArguments));
if (isIdentifier(qualifier.right)) {
qualifier = factory.updateQualifiedName(qualifier,
qualifier.left,
factory.updateIdentifier(qualifier.right, typeArguments));
}
}
}
typeArguments = ref.typeArguments;
// then move qualifiers
const ids = getAccessStack(ref);
for (const id of ids) {
qualifier = qualifier ? factory.createQualifiedName(qualifier, id) : id;
if (isIdentifier(id)) {
qualifier = qualifier ? factory.createQualifiedName(qualifier, id) : id;
}
}
return factory.updateImportTypeNode(
root,
Expand All @@ -5516,9 +5520,11 @@ namespace ts {
typeName = factory.updateIdentifier(typeName, typeArguments);
}
else {
typeName = factory.updateQualifiedName(typeName,
typeName.left,
factory.updateIdentifier(typeName.right, typeArguments));
if (isIdentifier(typeName.right)) {
typeName = factory.updateQualifiedName(typeName,
typeName.left,
factory.updateIdentifier(typeName.right, typeArguments));
}
}
typeArguments = ref.typeArguments;
// then move qualifiers
Expand All @@ -5533,7 +5539,7 @@ namespace ts {
}
}

function getAccessStack(ref: TypeReferenceNode): Identifier[] {
function getAccessStack(ref: TypeReferenceNode): MemberName[] {
let state = ref.typeName;
const ids = [];
while (!isIdentifier(state)) {
Expand Down Expand Up @@ -5920,12 +5926,7 @@ namespace ts {
const modifiers = !(context.flags & NodeBuilderFlags.OmitParameterModifiers) && preserveModifierFlags && parameterDeclaration && parameterDeclaration.modifiers ? parameterDeclaration.modifiers.map(factory.cloneNode) : undefined;
const isRest = parameterDeclaration && isRestParameter(parameterDeclaration) || getCheckFlags(parameterSymbol) & CheckFlags.RestParameter;
const dotDotDotToken = isRest ? factory.createToken(SyntaxKind.DotDotDotToken) : undefined;
const name = parameterDeclaration ? parameterDeclaration.name ?
parameterDeclaration.name.kind === SyntaxKind.Identifier ? setEmitFlags(factory.cloneNode(parameterDeclaration.name), EmitFlags.NoAsciiEscaping) :
parameterDeclaration.name.kind === SyntaxKind.QualifiedName ? setEmitFlags(factory.cloneNode(parameterDeclaration.name.right), EmitFlags.NoAsciiEscaping) :
cloneBindingName(parameterDeclaration.name) :
symbolName(parameterSymbol) :
symbolName(parameterSymbol);
const name = createParameterName(parameterSymbol, parameterDeclaration);
const isOptional = parameterDeclaration && isOptionalParameter(parameterDeclaration) || getCheckFlags(parameterSymbol) & CheckFlags.OptionalParameter;
const questionToken = isOptional ? factory.createToken(SyntaxKind.QuestionToken) : undefined;
const parameterNode = factory.createParameterDeclaration(
Expand All @@ -5939,6 +5940,19 @@ namespace ts {
context.approximateLength += symbolName(parameterSymbol).length + 3;
return parameterNode;

function createParameterName(symbol: Symbol, declaration: ParameterDeclaration | JSDocParameterTag | undefined): string | BindingName {
if (declaration && declaration.name) {
if (isIdentifier(declaration.name)) {
return setEmitFlags(factory.cloneNode(declaration.name), EmitFlags.NoAsciiEscaping);
}
if (isQualifiedName(declaration.name)) {
return isIdentifier(declaration.name.right) ? setEmitFlags(factory.cloneNode(declaration.name.right), EmitFlags.NoAsciiEscaping) : symbolName(symbol);
}
return cloneBindingName(declaration.name);
}
return symbolName(symbol);
}

function cloneBindingName(node: BindingName): BindingName {
return elideInitializerAndSetEmitFlags(node) as BindingName;
function elideInitializerAndSetEmitFlags(node: Node): Node {
Expand Down Expand Up @@ -6241,7 +6255,9 @@ namespace ts {
if (!nonRootParts || isEntityName(nonRootParts)) {
if (nonRootParts) {
const lastId = isIdentifier(nonRootParts) ? nonRootParts : nonRootParts.right;
lastId.typeArguments = undefined;
if (isIdentifier(lastId)) {
lastId.typeArguments = undefined;
}
}
return factory.createImportTypeNode(lit, assertion, nonRootParts as EntityName, typeParameterNodes as readonly TypeNode[], isTypeOf);
}
Expand All @@ -6261,9 +6277,12 @@ namespace ts {
}
else {
const lastId = isIdentifier(entityName) ? entityName : entityName.right;
const lastTypeArgs = lastId.typeArguments;
lastId.typeArguments = undefined;
return factory.createTypeReferenceNode(entityName, lastTypeArgs as NodeArray<TypeNode>);
let lastTypeArgs;
if (isIdentifier(lastId)) {
lastTypeArgs = lastId.typeArguments as NodeArray<TypeNode>;
lastId.typeArguments = undefined;
}
return factory.createTypeReferenceNode(entityName, lastTypeArgs);
}

function createAccessFromSymbolChain(chain: Symbol[], index: number, stopper: number): EntityName | IndexedAccessTypeNode {
Expand Down Expand Up @@ -16152,7 +16171,7 @@ namespace ts {
return links.resolvedType;
}

function getIdentifierChain(node: EntityName): Identifier[] {
function getIdentifierChain(node: EntityName): MemberName[] {
if (isIdentifier(node)) {
return [node];
}
Expand Down Expand Up @@ -16183,9 +16202,9 @@ namespace ts {
}
const moduleSymbol = resolveExternalModuleSymbol(innerModuleSymbol, /*dontResolveAlias*/ false);
if (!nodeIsMissing(node.qualifier)) {
const nameStack: Identifier[] = getIdentifierChain(node.qualifier!);
const nameStack: MemberName[] = getIdentifierChain(node.qualifier!);
let currentNamespace = moduleSymbol;
let current: Identifier | undefined;
let current: MemberName | undefined;
while (current = nameStack.shift()) {
const meaning = nameStack.length ? SymbolFlags.Namespace : targetMeaning;
// typeof a.b.c is normally resolved using `checkExpression` which in turn defers to `checkQualifiedName`
Expand Down Expand Up @@ -29315,7 +29334,7 @@ namespace ts {
return symbolResult && symbolName(symbolResult);
}

function getSuggestedSymbolForNonexistentModule(name: Identifier, targetModule: Symbol): Symbol | undefined {
function getSuggestedSymbolForNonexistentModule(name: MemberName, targetModule: Symbol): Symbol | undefined {
return targetModule.exports && getSpellingSuggestionForName(idText(name), getExportsOfModuleAsArray(targetModule), SymbolFlags.ModuleMember);
}

Expand Down
5 changes: 2 additions & 3 deletions src/compiler/factory/nodeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1093,13 +1093,12 @@ namespace ts {
//

// @api
function createQualifiedName(left: EntityName, right: string | Identifier) {
function createQualifiedName(left: EntityName, right: string | MemberName) {
const node = createBaseNode<QualifiedName>(SyntaxKind.QualifiedName);
node.left = left;
node.right = asName(right);
node.transformFlags |=
propagateChildFlags(node.left) |
propagateIdentifierNameFlags(node.right);
propagateChildFlags(node.left) | (isIdentifier(node.right) ? propagateIdentifierNameFlags(node.right) : propagateChildFlags(node.right));
return node;
}

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2843,7 +2843,7 @@ namespace ts {
entity = finishNode(
factory.createQualifiedName(
entity,
parseRightSideOfDot(allowReservedWords, allowPrivateIdentifiers) as Identifier
parseRightSideOfDot(allowReservedWords, allowPrivateIdentifiers)
),
pos
);
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,7 @@ namespace ts {
export interface QualifiedName extends Node {
readonly kind: SyntaxKind.QualifiedName;
readonly left: EntityName;
readonly right: Identifier;
readonly right: MemberName;
/*@internal*/ jsdocDotPos?: number; // QualifiedName occurs in JSDoc-style generic: Id1.Id2.<T>
}

Expand Down Expand Up @@ -4326,7 +4326,7 @@ namespace ts {
/* @internal */ getSuggestionForNonexistentProperty(name: MemberName | string, containingType: Type): string | undefined;
/* @internal */ getSuggestedSymbolForNonexistentSymbol(location: Node, name: string, meaning: SymbolFlags): Symbol | undefined;
/* @internal */ getSuggestionForNonexistentSymbol(location: Node, name: string, meaning: SymbolFlags): string | undefined;
/* @internal */ getSuggestedSymbolForNonexistentModule(node: Identifier, target: Symbol): Symbol | undefined;
/* @internal */ getSuggestedSymbolForNonexistentModule(node: MemberName, target: Symbol): Symbol | undefined;
/* @internal */ getSuggestedSymbolForNonexistentClassMember(name: string, baseType: Type): Symbol | undefined;
/* @internal */ getSuggestionForNonexistentExport(node: Identifier, target: Symbol): string | undefined;
getBaseConstraintOfType(type: Type): Type | undefined;
Expand Down Expand Up @@ -7257,8 +7257,8 @@ namespace ts {
// Names
//

createQualifiedName(left: EntityName, right: string | Identifier): QualifiedName;
updateQualifiedName(node: QualifiedName, left: EntityName, right: Identifier): QualifiedName;
createQualifiedName(left: EntityName, right: string | MemberName): QualifiedName;
updateQualifiedName(node: QualifiedName, left: EntityName, right: MemberName): QualifiedName;
createComputedPropertyName(expression: Expression): ComputedPropertyName;
updateComputedPropertyName(node: ComputedPropertyName, expression: Expression): ComputedPropertyName;

Expand Down
10 changes: 5 additions & 5 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ declare namespace ts {
export interface QualifiedName extends Node {
readonly kind: SyntaxKind.QualifiedName;
readonly left: EntityName;
readonly right: Identifier;
readonly right: MemberName;
}
export type EntityName = Identifier | QualifiedName;
export type PropertyName = Identifier | StringLiteral | NumericLiteral | ComputedPropertyName | PrivateIdentifier;
Expand Down Expand Up @@ -3395,8 +3395,8 @@ declare namespace ts {
createFalse(): FalseLiteral;
createModifier<T extends ModifierSyntaxKind>(kind: T): ModifierToken<T>;
createModifiersFromModifierFlags(flags: ModifierFlags): Modifier[] | undefined;
createQualifiedName(left: EntityName, right: string | Identifier): QualifiedName;
updateQualifiedName(node: QualifiedName, left: EntityName, right: Identifier): QualifiedName;
createQualifiedName(left: EntityName, right: string | MemberName): QualifiedName;
updateQualifiedName(node: QualifiedName, left: EntityName, right: MemberName): QualifiedName;
createComputedPropertyName(expression: Expression): ComputedPropertyName;
updateComputedPropertyName(node: ComputedPropertyName, expression: Expression): ComputedPropertyName;
createTypeParameterDeclaration(modifiers: readonly Modifier[] | undefined, name: string | Identifier, constraint?: TypeNode, defaultType?: TypeNode): TypeParameterDeclaration;
Expand Down Expand Up @@ -10803,9 +10803,9 @@ declare namespace ts {
/** @deprecated Use `factory.createModifiersFromModifierFlags` or the factory supplied by your transformation context instead. */
const createModifiersFromModifierFlags: (flags: ModifierFlags) => Modifier[] | undefined;
/** @deprecated Use `factory.createQualifiedName` or the factory supplied by your transformation context instead. */
const createQualifiedName: (left: EntityName, right: string | Identifier) => QualifiedName;
const createQualifiedName: (left: EntityName, right: string | MemberName) => QualifiedName;
/** @deprecated Use `factory.updateQualifiedName` or the factory supplied by your transformation context instead. */
const updateQualifiedName: (node: QualifiedName, left: EntityName, right: Identifier) => QualifiedName;
const updateQualifiedName: (node: QualifiedName, left: EntityName, right: MemberName) => QualifiedName;
/** @deprecated Use `factory.createComputedPropertyName` or the factory supplied by your transformation context instead. */
const createComputedPropertyName: (expression: Expression) => ComputedPropertyName;
/** @deprecated Use `factory.updateComputedPropertyName` or the factory supplied by your transformation context instead. */
Expand Down
10 changes: 5 additions & 5 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ declare namespace ts {
export interface QualifiedName extends Node {
readonly kind: SyntaxKind.QualifiedName;
readonly left: EntityName;
readonly right: Identifier;
readonly right: MemberName;
}
export type EntityName = Identifier | QualifiedName;
export type PropertyName = Identifier | StringLiteral | NumericLiteral | ComputedPropertyName | PrivateIdentifier;
Expand Down Expand Up @@ -3395,8 +3395,8 @@ declare namespace ts {
createFalse(): FalseLiteral;
createModifier<T extends ModifierSyntaxKind>(kind: T): ModifierToken<T>;
createModifiersFromModifierFlags(flags: ModifierFlags): Modifier[] | undefined;
createQualifiedName(left: EntityName, right: string | Identifier): QualifiedName;
updateQualifiedName(node: QualifiedName, left: EntityName, right: Identifier): QualifiedName;
createQualifiedName(left: EntityName, right: string | MemberName): QualifiedName;
updateQualifiedName(node: QualifiedName, left: EntityName, right: MemberName): QualifiedName;
createComputedPropertyName(expression: Expression): ComputedPropertyName;
updateComputedPropertyName(node: ComputedPropertyName, expression: Expression): ComputedPropertyName;
createTypeParameterDeclaration(modifiers: readonly Modifier[] | undefined, name: string | Identifier, constraint?: TypeNode, defaultType?: TypeNode): TypeParameterDeclaration;
Expand Down Expand Up @@ -6952,9 +6952,9 @@ declare namespace ts {
/** @deprecated Use `factory.createModifiersFromModifierFlags` or the factory supplied by your transformation context instead. */
const createModifiersFromModifierFlags: (flags: ModifierFlags) => Modifier[] | undefined;
/** @deprecated Use `factory.createQualifiedName` or the factory supplied by your transformation context instead. */
const createQualifiedName: (left: EntityName, right: string | Identifier) => QualifiedName;
const createQualifiedName: (left: EntityName, right: string | MemberName) => QualifiedName;
/** @deprecated Use `factory.updateQualifiedName` or the factory supplied by your transformation context instead. */
const updateQualifiedName: (node: QualifiedName, left: EntityName, right: Identifier) => QualifiedName;
const updateQualifiedName: (node: QualifiedName, left: EntityName, right: MemberName) => QualifiedName;
/** @deprecated Use `factory.createComputedPropertyName` or the factory supplied by your transformation context instead. */
const createComputedPropertyName: (expression: Expression) => ComputedPropertyName;
/** @deprecated Use `factory.updateComputedPropertyName` or the factory supplied by your transformation context instead. */
Expand Down