Skip to content

Commit 4193b78

Browse files
committed
pass isInUnionType to parseFunctionOrConstructorTypeToError
1 parent e1c2a51 commit 4193b78

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

src/compiler/parser.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3570,15 +3570,31 @@ namespace ts {
35703570
}
35713571

35723572
function parseFunctionOrConstructorTypeToError(
3573-
functionTypeDiagnostic: DiagnosticMessage,
3574-
constructorTypeDiagnostic: DiagnosticMessage
3573+
isInUnionType: boolean
35753574
): TypeNode | undefined {
35763575
// the function type and constructor type shorthand notation
35773576
// are not allowed directly in unions and intersections, but we'll
35783577
// try to parse them gracefully and issue a helpful message.
35793578
if (isStartOfFunctionType() || token() === SyntaxKind.NewKeyword) {
35803579
const type = parseFunctionOrConstructorType();
3581-
parseErrorAtRange(type, isFunctionTypeNode(type) ? functionTypeDiagnostic : constructorTypeDiagnostic);
3580+
let diagnostic: DiagnosticMessage;
3581+
if (isFunctionTypeNode(type)) {
3582+
if (isInUnionType) {
3583+
diagnostic = Diagnostics.Function_type_notation_must_be_parenthesized_when_used_in_a_union_type;
3584+
}
3585+
else {
3586+
diagnostic = Diagnostics.Function_type_notation_must_be_parenthesized_when_used_in_an_intersection_type;
3587+
}
3588+
}
3589+
else {
3590+
if (isInUnionType) {
3591+
diagnostic = Diagnostics.Constructor_type_notation_must_be_parenthesized_when_used_in_a_union_type;
3592+
}
3593+
else {
3594+
diagnostic = Diagnostics.Constructor_type_notation_must_be_parenthesized_when_used_in_an_intersection_type;
3595+
}
3596+
}
3597+
parseErrorAtRange(type, diagnostic);
35823598
return type;
35833599
}
35843600
return undefined;
@@ -3587,43 +3603,29 @@ namespace ts {
35873603
function parseUnionOrIntersectionType(
35883604
operator: SyntaxKind.BarToken | SyntaxKind.AmpersandToken,
35893605
parseConstituentType: () => TypeNode,
3590-
createTypeNode: (types: NodeArray<TypeNode>) => UnionOrIntersectionTypeNode,
3591-
functionTypeDiagnostic: DiagnosticMessage,
3592-
constructorTypeDiagnostic: DiagnosticMessage
3606+
createTypeNode: (types: NodeArray<TypeNode>) => UnionOrIntersectionTypeNode
35933607
): TypeNode {
35943608
const pos = getNodePos();
35953609
const hasLeadingOperator = parseOptional(operator);
35963610
let type = hasLeadingOperator ?
3597-
parseFunctionOrConstructorTypeToError(functionTypeDiagnostic, constructorTypeDiagnostic) || parseConstituentType() :
3611+
parseFunctionOrConstructorTypeToError(operator === SyntaxKind.BarToken) || parseConstituentType() :
35983612
parseConstituentType();
35993613
if (token() === operator || hasLeadingOperator) {
36003614
const types = [type];
36013615
while (parseOptional(operator)) {
3602-
types.push(parseFunctionOrConstructorTypeToError(functionTypeDiagnostic, constructorTypeDiagnostic) || parseConstituentType());
3616+
types.push(parseFunctionOrConstructorTypeToError(operator === SyntaxKind.BarToken) || parseConstituentType());
36033617
}
36043618
type = finishNode(createTypeNode(createNodeArray(types, pos)), pos);
36053619
}
36063620
return type;
36073621
}
36083622

36093623
function parseIntersectionTypeOrHigher(): TypeNode {
3610-
return parseUnionOrIntersectionType(
3611-
SyntaxKind.AmpersandToken,
3612-
parseTypeOperatorOrHigher,
3613-
factory.createIntersectionTypeNode,
3614-
Diagnostics.Function_type_notation_must_be_parenthesized_when_used_in_an_intersection_type,
3615-
Diagnostics.Constructor_type_notation_must_be_parenthesized_when_used_in_an_intersection_type
3616-
);
3624+
return parseUnionOrIntersectionType(SyntaxKind.AmpersandToken, parseTypeOperatorOrHigher, factory.createIntersectionTypeNode);
36173625
}
36183626

36193627
function parseUnionTypeOrHigher(): TypeNode {
3620-
return parseUnionOrIntersectionType(
3621-
SyntaxKind.BarToken,
3622-
parseIntersectionTypeOrHigher,
3623-
factory.createUnionTypeNode,
3624-
Diagnostics.Function_type_notation_must_be_parenthesized_when_used_in_a_union_type,
3625-
Diagnostics.Constructor_type_notation_must_be_parenthesized_when_used_in_a_union_type
3626-
);
3628+
return parseUnionOrIntersectionType(SyntaxKind.BarToken, parseIntersectionTypeOrHigher, factory.createUnionTypeNode);
36273629
}
36283630

36293631
function isStartOfFunctionType(): boolean {

0 commit comments

Comments
 (0)