Skip to content

Commit e0438db

Browse files
author
Arthur Ozga
committed
Parenthesize function types
1 parent ac68e8d commit e0438db

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/compiler/checker.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2359,6 +2359,8 @@ namespace ts {
23592359
context.InElementType = false;
23602360
const inTypeAlias = context.InTypeAlias;
23612361
context.InTypeAlias = false;
2362+
const inFirstTypeArgument = context.InFirstTypeArgument;
2363+
context.InFirstTypeArgument = false;
23622364

23632365
// TODO: should be assert?
23642366
if (!type) {
@@ -2367,8 +2369,6 @@ namespace ts {
23672369
return undefined;
23682370
}
23692371

2370-
2371-
23722372
if (type.flags & TypeFlags.Any) {
23732373
return createKeywordTypeNode(SyntaxKind.AnyKeyword);
23742374
}
@@ -2586,15 +2586,18 @@ namespace ts {
25862586

25872587
if (resolved.callSignatures.length === 1 && !resolved.constructSignatures.length) {
25882588
const signature = resolved.callSignatures[0];
2589-
const functionTypeNode = <FunctionTypeNode>signatureToSignatureDeclarationHelper(signature, SyntaxKind.FunctionType, context);
2589+
const signatureNode = <FunctionTypeNode>signatureToSignatureDeclarationHelper(signature, SyntaxKind.FunctionType, context);
25902590
return shouldAddParenthesisAroundFunctionType(signature, context) ?
2591-
createParenthesizedTypeNode(functionTypeNode) :
2592-
functionTypeNode;
2591+
createParenthesizedTypeNode(signatureNode) :
2592+
signatureNode;
25932593

25942594
}
25952595
if (resolved.constructSignatures.length === 1 && !resolved.callSignatures.length) {
25962596
const signature = resolved.constructSignatures[0];
2597-
return <ConstructorTypeNode>signatureToSignatureDeclarationHelper(signature, SyntaxKind.ConstructorType, context);
2597+
const signatureNode = <ConstructorTypeNode>signatureToSignatureDeclarationHelper(signature, SyntaxKind.ConstructorType, context);
2598+
return shouldAddParenthesisAroundFunctionType(signature, context) ?
2599+
createParenthesizedTypeNode(signatureNode) :
2600+
signatureNode;
25982601
}
25992602
}
26002603

@@ -2610,7 +2613,7 @@ namespace ts {
26102613
if (InElementType) {
26112614
return true;
26122615
}
2613-
else if (context.InFirstTypeArgument) {
2616+
else if (inFirstTypeArgument) {
26142617
// Add parenthesis around function type for the first type argument to avoid ambiguity
26152618
const typeParameters = callSignature.target && (context.flags & NodeBuilderFlags.WriteTypeArgumentsOfSignature) ?
26162619
callSignature.target.typeParameters : callSignature.typeParameters;
@@ -2690,7 +2693,14 @@ namespace ts {
26902693
entityName = nameIdentifier;
26912694
}
26922695
const typeParameterCount = (type.target.typeParameters || emptyArray).length;
2693-
const typeArgumentNodes = some(typeArguments) ? mapToTypeNodeArray(typeArguments.slice(i, typeParameterCount - i), /*addInElementTypeFlag*/ false) : undefined;
2696+
2697+
let typeArgumentNodes: TypeNode[] | undefined;
2698+
if (some(typeArguments)) {
2699+
const slice = typeArguments.slice(i, typeParameterCount - i);
2700+
context.InFirstTypeArgument = true;
2701+
typeArgumentNodes = mapToTypeNodeArray(slice, /*addInElementTypeFlag*/ false);
2702+
}
2703+
26942704
return createTypeReferenceNode(entityName, typeArgumentNodes);
26952705
}
26962706
}
@@ -2851,7 +2861,6 @@ namespace ts {
28512861
}
28522862
}
28532863

2854-
// TODO: add meaning: SymbolFlags argument.
28552864
// TODO: add SymbolFormatFlags?? Yes to add outer type parameters. Defer UseOnlyExternalAliasing until a separate symbolbuilder PR.
28562865
function symbolToName(symbol: Symbol, expectsIdentifier: true, meaning: SymbolFlags, context: NodeBuilderContext): Identifier;
28572866
function symbolToName(symbol: Symbol, expectsIdentifier: false, meaning: SymbolFlags, context: NodeBuilderContext): EntityName;

0 commit comments

Comments
 (0)