Skip to content

Commit 685900c

Browse files
committed
Fix declaration emit when first generic function type in type argument position specified using space
1 parent 06f54b9 commit 685900c

File tree

2 files changed

+17
-92
lines changed

2 files changed

+17
-92
lines changed

src/compiler/declarationEmitter.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,7 @@ namespace ts {
13771377
function emitSignatureDeclaration(node: SignatureDeclaration) {
13781378
const prevEnclosingDeclaration = enclosingDeclaration;
13791379
enclosingDeclaration = node;
1380+
let closeParenthesizedFunctionType = false;
13801381

13811382
if (node.kind === SyntaxKind.IndexSignature) {
13821383
// Index signature can have readonly modifier
@@ -1388,6 +1389,16 @@ namespace ts {
13881389
if (node.kind === SyntaxKind.ConstructSignature || node.kind === SyntaxKind.ConstructorType) {
13891390
write("new ");
13901391
}
1392+
else if (node.kind === SyntaxKind.FunctionType) {
1393+
const currentOutput = writer.getText();
1394+
// Do not generate incorrect type when function type with type parameters is type argument
1395+
// This could happen if user used space between two '<' making it error free
1396+
// e.g var x: A< <Tany>(a: Tany)=>Tany>;
1397+
if (node.typeParameters && currentOutput.charAt(currentOutput.length - 1) === "<") {
1398+
closeParenthesizedFunctionType = true;
1399+
write("(");
1400+
}
1401+
}
13911402
emitTypeParameters(node.typeParameters);
13921403
write("(");
13931404
}
@@ -1421,6 +1432,9 @@ namespace ts {
14211432
write(";");
14221433
writeLine();
14231434
}
1435+
else if (closeParenthesizedFunctionType) {
1436+
write(")");
1437+
}
14241438

14251439
function getReturnTypeVisibilityError(symbolAccessibilityResult: SymbolAccessibilityResult): SymbolAccessibilityDiagnostic {
14261440
let diagnosticMessage: DiagnosticMessage;

tests/baselines/reference/declarationEmitFirstTypeArgumentGenericFunctionType.js

Lines changed: 3 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -52,104 +52,15 @@ var prop4; // parenthesized first type argument
5252
//// [declarationEmitFirstTypeArgumentGenericFunctionType.d.ts]
5353
declare class X<A> {
5454
}
55-
declare var prop11: X<<Tany>() => Tany>;
55+
declare var prop11: X<(<Tany>() => Tany)>;
5656
declare var prop12: X<(<Tany>() => Tany)>;
5757
declare function f1(): X<(<Tany>() => Tany)>;
5858
declare function f2(): X<(<Tany>() => Tany)>;
59-
declare function f3(): X<<Tany>() => Tany>;
59+
declare function f3(): X<(<Tany>() => Tany)>;
6060
declare function f4(): X<(<Tany>() => Tany)>;
6161
declare class Y<A, B> {
6262
}
6363
declare var prop2: Y<string[], <Tany>() => Tany>;
6464
declare var prop2: Y<string[], <Tany>() => Tany>;
65-
declare var prop3: Y<<Tany>() => Tany, <Tany>() => Tany>;
65+
declare var prop3: Y<(<Tany>() => Tany), <Tany>() => Tany>;
6666
declare var prop4: Y<(<Tany>() => Tany), <Tany>() => Tany>;
67-
68-
69-
//// [DtsFileErrors]
70-
71-
72-
tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(3,21): error TS2314: Generic type 'X<A>' requires 1 type argument(s).
73-
tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(3,22): error TS1005: '=' expected.
74-
tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(3,24): error TS2304: Cannot find name 'Tany'.
75-
tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(3,30): error TS1109: Expression expected.
76-
tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(3,32): error TS1005: ';' expected.
77-
tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(3,35): error TS2304: Cannot find name 'Tany'.
78-
tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(3,40): error TS1109: Expression expected.
79-
tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(7,24): error TS2314: Generic type 'X<A>' requires 1 type argument(s).
80-
tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(7,25): error TS1144: '{' or ';' expected.
81-
tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(7,27): error TS2304: Cannot find name 'Tany'.
82-
tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(7,33): error TS1109: Expression expected.
83-
tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(7,35): error TS1005: ';' expected.
84-
tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(7,38): error TS2304: Cannot find name 'Tany'.
85-
tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(7,43): error TS1109: Expression expected.
86-
tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(13,20): error TS2314: Generic type 'Y<A, B>' requires 2 type argument(s).
87-
tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(13,21): error TS1005: '=' expected.
88-
tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(13,23): error TS2304: Cannot find name 'Tany'.
89-
tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(13,29): error TS1109: Expression expected.
90-
tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(13,31): error TS1005: ';' expected.
91-
tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(13,34): error TS2304: Cannot find name 'Tany'.
92-
tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(13,52): error TS2304: Cannot find name 'Tany'.
93-
tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(13,57): error TS1109: Expression expected.
94-
95-
96-
==== tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts (22 errors) ====
97-
declare class X<A> {
98-
}
99-
declare var prop11: X<<Tany>() => Tany>;
100-
~
101-
!!! error TS2314: Generic type 'X<A>' requires 1 type argument(s).
102-
~~
103-
!!! error TS1005: '=' expected.
104-
~~~~
105-
!!! error TS2304: Cannot find name 'Tany'.
106-
~
107-
!!! error TS1109: Expression expected.
108-
~~
109-
!!! error TS1005: ';' expected.
110-
~~~~
111-
!!! error TS2304: Cannot find name 'Tany'.
112-
~
113-
!!! error TS1109: Expression expected.
114-
declare var prop12: X<(<Tany>() => Tany)>;
115-
declare function f1(): X<(<Tany>() => Tany)>;
116-
declare function f2(): X<(<Tany>() => Tany)>;
117-
declare function f3(): X<<Tany>() => Tany>;
118-
~
119-
!!! error TS2314: Generic type 'X<A>' requires 1 type argument(s).
120-
~~
121-
!!! error TS1144: '{' or ';' expected.
122-
~~~~
123-
!!! error TS2304: Cannot find name 'Tany'.
124-
~
125-
!!! error TS1109: Expression expected.
126-
~~
127-
!!! error TS1005: ';' expected.
128-
~~~~
129-
!!! error TS2304: Cannot find name 'Tany'.
130-
~
131-
!!! error TS1109: Expression expected.
132-
declare function f4(): X<(<Tany>() => Tany)>;
133-
declare class Y<A, B> {
134-
}
135-
declare var prop2: Y<string[], <Tany>() => Tany>;
136-
declare var prop2: Y<string[], <Tany>() => Tany>;
137-
declare var prop3: Y<<Tany>() => Tany, <Tany>() => Tany>;
138-
~
139-
!!! error TS2314: Generic type 'Y<A, B>' requires 2 type argument(s).
140-
~~
141-
!!! error TS1005: '=' expected.
142-
~~~~
143-
!!! error TS2304: Cannot find name 'Tany'.
144-
~
145-
!!! error TS1109: Expression expected.
146-
~~
147-
!!! error TS1005: ';' expected.
148-
~~~~
149-
!!! error TS2304: Cannot find name 'Tany'.
150-
~~~~
151-
!!! error TS2304: Cannot find name 'Tany'.
152-
~
153-
!!! error TS1109: Expression expected.
154-
declare var prop4: Y<(<Tany>() => Tany), <Tany>() => Tany>;
155-

0 commit comments

Comments
 (0)