From 685900c2a344ca8f41d23588737a877e6f042830 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Mon, 18 Apr 2016 16:08:52 -0700 Subject: [PATCH] Fix declaration emit when first generic function type in type argument position specified using space --- src/compiler/declarationEmitter.ts | 14 +++ ...mitFirstTypeArgumentGenericFunctionType.js | 95 +------------------ 2 files changed, 17 insertions(+), 92 deletions(-) diff --git a/src/compiler/declarationEmitter.ts b/src/compiler/declarationEmitter.ts index 38df2e45d48..58ce2286627 100644 --- a/src/compiler/declarationEmitter.ts +++ b/src/compiler/declarationEmitter.ts @@ -1377,6 +1377,7 @@ namespace ts { function emitSignatureDeclaration(node: SignatureDeclaration) { const prevEnclosingDeclaration = enclosingDeclaration; enclosingDeclaration = node; + let closeParenthesizedFunctionType = false; if (node.kind === SyntaxKind.IndexSignature) { // Index signature can have readonly modifier @@ -1388,6 +1389,16 @@ namespace ts { if (node.kind === SyntaxKind.ConstructSignature || node.kind === SyntaxKind.ConstructorType) { write("new "); } + else if (node.kind === SyntaxKind.FunctionType) { + const currentOutput = writer.getText(); + // Do not generate incorrect type when function type with type parameters is type argument + // This could happen if user used space between two '<' making it error free + // e.g var x: A< (a: Tany)=>Tany>; + if (node.typeParameters && currentOutput.charAt(currentOutput.length - 1) === "<") { + closeParenthesizedFunctionType = true; + write("("); + } + } emitTypeParameters(node.typeParameters); write("("); } @@ -1421,6 +1432,9 @@ namespace ts { write(";"); writeLine(); } + else if (closeParenthesizedFunctionType) { + write(")"); + } function getReturnTypeVisibilityError(symbolAccessibilityResult: SymbolAccessibilityResult): SymbolAccessibilityDiagnostic { let diagnosticMessage: DiagnosticMessage; diff --git a/tests/baselines/reference/declarationEmitFirstTypeArgumentGenericFunctionType.js b/tests/baselines/reference/declarationEmitFirstTypeArgumentGenericFunctionType.js index 2748b57933b..b2bca641a5a 100644 --- a/tests/baselines/reference/declarationEmitFirstTypeArgumentGenericFunctionType.js +++ b/tests/baselines/reference/declarationEmitFirstTypeArgumentGenericFunctionType.js @@ -52,104 +52,15 @@ var prop4; // parenthesized first type argument //// [declarationEmitFirstTypeArgumentGenericFunctionType.d.ts] declare class X { } -declare var prop11: X<() => Tany>; +declare var prop11: X<(() => Tany)>; declare var prop12: X<(() => Tany)>; declare function f1(): X<(() => Tany)>; declare function f2(): X<(() => Tany)>; -declare function f3(): X<() => Tany>; +declare function f3(): X<(() => Tany)>; declare function f4(): X<(() => Tany)>; declare class Y { } declare var prop2: Y() => Tany>; declare var prop2: Y() => Tany>; -declare var prop3: Y<() => Tany, () => Tany>; +declare var prop3: Y<(() => Tany), () => Tany>; declare var prop4: Y<(() => Tany), () => Tany>; - - -//// [DtsFileErrors] - - -tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(3,21): error TS2314: Generic type 'X' requires 1 type argument(s). -tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(3,22): error TS1005: '=' expected. -tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(3,24): error TS2304: Cannot find name 'Tany'. -tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(3,30): error TS1109: Expression expected. -tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(3,32): error TS1005: ';' expected. -tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(3,35): error TS2304: Cannot find name 'Tany'. -tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(3,40): error TS1109: Expression expected. -tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(7,24): error TS2314: Generic type 'X' requires 1 type argument(s). -tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(7,25): error TS1144: '{' or ';' expected. -tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(7,27): error TS2304: Cannot find name 'Tany'. -tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(7,33): error TS1109: Expression expected. -tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(7,35): error TS1005: ';' expected. -tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(7,38): error TS2304: Cannot find name 'Tany'. -tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(7,43): error TS1109: Expression expected. -tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(13,20): error TS2314: Generic type 'Y' requires 2 type argument(s). -tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(13,21): error TS1005: '=' expected. -tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(13,23): error TS2304: Cannot find name 'Tany'. -tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(13,29): error TS1109: Expression expected. -tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(13,31): error TS1005: ';' expected. -tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(13,34): error TS2304: Cannot find name 'Tany'. -tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(13,52): error TS2304: Cannot find name 'Tany'. -tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts(13,57): error TS1109: Expression expected. - - -==== tests/cases/compiler/declarationEmitFirstTypeArgumentGenericFunctionType.d.ts (22 errors) ==== - declare class X { - } - declare var prop11: X<() => Tany>; - ~ -!!! error TS2314: Generic type 'X' requires 1 type argument(s). - ~~ -!!! error TS1005: '=' expected. - ~~~~ -!!! error TS2304: Cannot find name 'Tany'. - ~ -!!! error TS1109: Expression expected. - ~~ -!!! error TS1005: ';' expected. - ~~~~ -!!! error TS2304: Cannot find name 'Tany'. - ~ -!!! error TS1109: Expression expected. - declare var prop12: X<(() => Tany)>; - declare function f1(): X<(() => Tany)>; - declare function f2(): X<(() => Tany)>; - declare function f3(): X<() => Tany>; - ~ -!!! error TS2314: Generic type 'X' requires 1 type argument(s). - ~~ -!!! error TS1144: '{' or ';' expected. - ~~~~ -!!! error TS2304: Cannot find name 'Tany'. - ~ -!!! error TS1109: Expression expected. - ~~ -!!! error TS1005: ';' expected. - ~~~~ -!!! error TS2304: Cannot find name 'Tany'. - ~ -!!! error TS1109: Expression expected. - declare function f4(): X<(() => Tany)>; - declare class Y { - } - declare var prop2: Y() => Tany>; - declare var prop2: Y() => Tany>; - declare var prop3: Y<() => Tany, () => Tany>; - ~ -!!! error TS2314: Generic type 'Y' requires 2 type argument(s). - ~~ -!!! error TS1005: '=' expected. - ~~~~ -!!! error TS2304: Cannot find name 'Tany'. - ~ -!!! error TS1109: Expression expected. - ~~ -!!! error TS1005: ';' expected. - ~~~~ -!!! error TS2304: Cannot find name 'Tany'. - ~~~~ -!!! error TS2304: Cannot find name 'Tany'. - ~ -!!! error TS1109: Expression expected. - declare var prop4: Y<(() => Tany), () => Tany>; - \ No newline at end of file