From 47e027b50b68e56611400089d4b016c84d32e1e0 Mon Sep 17 00:00:00 2001 From: dcode Date: Fri, 6 Jan 2023 19:21:39 +0100 Subject: [PATCH 1/3] Support trailing comma in function types --- src/parser.ts | 4 ++++ tests/parser/function-type.ts | 1 + tests/parser/function-type.ts.fixture.ts | 1 + 3 files changed, 6 insertions(+) diff --git a/src/parser.ts b/src/parser.ts index 166c3d4132..8eaffed790 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -795,6 +795,10 @@ export class Parser extends DiagnosticEmitter { firstParamKind = kind; } } + } else if (parameters && parameters.length && tn.peek() == Token.CloseParen) { // allow trailing comma + isSignature = true; + tn.discard(state); + break; } else { if (isSignature) { this.error( diff --git a/tests/parser/function-type.ts b/tests/parser/function-type.ts index 4c9a385fe5..f547cdeefa 100644 --- a/tests/parser/function-type.ts +++ b/tests/parser/function-type.ts @@ -3,3 +3,4 @@ var b: (a: i32, b: i32) => void; var c: (a: i32, b: i32) => (a: i32, b: i32) => void; var d: (a: i32, a: i32) => void; // NOTE: duplicates in type signatures doesn't in TypeScript var e: (a) => void; // TS1110 +var f: (a: i32, b: i32,) => (a: i32, b: i32,) => void; diff --git a/tests/parser/function-type.ts.fixture.ts b/tests/parser/function-type.ts.fixture.ts index 061e00711d..e4e6aa7888 100644 --- a/tests/parser/function-type.ts.fixture.ts +++ b/tests/parser/function-type.ts.fixture.ts @@ -3,4 +3,5 @@ var b: (a: i32, b: i32) => void; var c: (a: i32, b: i32) => (a: i32, b: i32) => void; var d: (a: i32, a: i32) => void; var e: (a) => void; +var f: (a: i32, b: i32) => (a: i32, b: i32) => void; // ERROR 1110: "Type expected." in function-type.ts(5,10+0) From b36fc3a459307f95287fa8af812b830f22fcf70d Mon Sep 17 00:00:00 2001 From: dcode Date: Fri, 6 Jan 2023 23:35:45 +0100 Subject: [PATCH 2/3] simplify --- src/parser.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/parser.ts b/src/parser.ts index 8eaffed790..e027349f6c 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -795,9 +795,8 @@ export class Parser extends DiagnosticEmitter { firstParamKind = kind; } } - } else if (parameters && parameters.length && tn.peek() == Token.CloseParen) { // allow trailing comma - isSignature = true; - tn.discard(state); + } else if (parameters && tn.peek() == Token.CloseParen) { // allow trailing comma + assert(isSignature && parameters.length); break; } else { if (isSignature) { From bc1bd25385120c91fc58014f7687a788483a8f0a Mon Sep 17 00:00:00 2001 From: dcode Date: Fri, 6 Jan 2023 23:37:16 +0100 Subject: [PATCH 3/3] simplify --- src/parser.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/parser.ts b/src/parser.ts index e027349f6c..cade050161 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -795,11 +795,9 @@ export class Parser extends DiagnosticEmitter { firstParamKind = kind; } } - } else if (parameters && tn.peek() == Token.CloseParen) { // allow trailing comma - assert(isSignature && parameters.length); - break; } else { if (isSignature) { + if (tn.peek() == Token.CloseParen) break; // allow trailing comma this.error( DiagnosticCode.Identifier_expected, tn.range()