From 4281bf5752b58ac466723e2339e49aad0f3ac298 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Thu, 2 Jun 2016 07:35:58 -0700 Subject: [PATCH] Allow trailing commas in function parameter and argument lists --- src/compiler/checker.ts | 20 +++++++------------ .../reference/ArrowFunction2.errors.txt | 3 --- ...serErrorRecovery_ParameterList3.errors.txt | 8 -------- .../parserErrorRecovery_ParameterList3.js | 7 ------- .../parserParameterList12.errors.txt | 8 -------- .../reference/parserParameterList12.symbols | 5 +++++ .../reference/parserParameterList12.types | 5 +++++ ...gCommasInFunctionParametersAndArguments.js | 9 +++++++++ ...asInFunctionParametersAndArguments.symbols | 8 ++++++++ ...mmasInFunctionParametersAndArguments.types | 10 ++++++++++ ...gCommasInFunctionParametersAndArguments.ts | 3 +++ .../parserErrorRecovery_ParameterList3.ts | 2 -- .../fourslash/trailingCommaSignatureHelp.ts | 15 ++++++++++++++ 13 files changed, 62 insertions(+), 41 deletions(-) delete mode 100644 tests/baselines/reference/parserErrorRecovery_ParameterList3.errors.txt delete mode 100644 tests/baselines/reference/parserErrorRecovery_ParameterList3.js delete mode 100644 tests/baselines/reference/parserParameterList12.errors.txt create mode 100644 tests/baselines/reference/parserParameterList12.symbols create mode 100644 tests/baselines/reference/parserParameterList12.types create mode 100644 tests/baselines/reference/trailingCommasInFunctionParametersAndArguments.js create mode 100644 tests/baselines/reference/trailingCommasInFunctionParametersAndArguments.symbols create mode 100644 tests/baselines/reference/trailingCommasInFunctionParametersAndArguments.types create mode 100644 tests/cases/conformance/es7/trailingCommasInFunctionParametersAndArguments.ts delete mode 100644 tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ParameterLists/parserErrorRecovery_ParameterList3.ts create mode 100644 tests/cases/fourslash/trailingCommaSignatureHelp.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 06ef2463402..402d6455387 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10322,7 +10322,7 @@ namespace ts { } function hasCorrectArity(node: CallLikeExpression, args: Expression[], signature: Signature) { - let adjustedArgCount: number; // Apparent number of arguments we will have in this call + let argCount: number; // Apparent number of arguments we will have in this call let typeArguments: NodeArray; // Type arguments (undefined if none) let callIsIncomplete: boolean; // In incomplete call we want to be lenient when we have too few arguments let isDecorator: boolean; @@ -10333,7 +10333,7 @@ namespace ts { // Even if the call is incomplete, we'll have a missing expression as our last argument, // so we can say the count is just the arg list length - adjustedArgCount = args.length; + argCount = args.length; typeArguments = undefined; if (tagExpression.template.kind === SyntaxKind.TemplateExpression) { @@ -10356,7 +10356,7 @@ namespace ts { else if (node.kind === SyntaxKind.Decorator) { isDecorator = true; typeArguments = undefined; - adjustedArgCount = getEffectiveArgumentCount(node, /*args*/ undefined, signature); + argCount = getEffectiveArgumentCount(node, /*args*/ undefined, signature); } else { const callExpression = node; @@ -10367,8 +10367,7 @@ namespace ts { return signature.minArgumentCount === 0; } - // For IDE scenarios we may have an incomplete call, so a trailing comma is tantamount to adding another argument. - adjustedArgCount = callExpression.arguments.hasTrailingComma ? args.length + 1 : args.length; + argCount = args.length; // If we are missing the close paren, the call is incomplete. callIsIncomplete = (callExpression).arguments.end === callExpression.end; @@ -10392,12 +10391,12 @@ namespace ts { } // Too many arguments implies incorrect arity. - if (!signature.hasRestParameter && adjustedArgCount > signature.parameters.length) { + if (!signature.hasRestParameter && argCount > signature.parameters.length) { return false; } // If the call is incomplete, we should skip the lower bound check. - const hasEnoughArguments = adjustedArgCount >= signature.minArgumentCount; + const hasEnoughArguments = argCount >= signature.minArgumentCount; return callIsIncomplete || hasEnoughArguments; } @@ -18027,10 +18026,6 @@ namespace ts { } function checkGrammarParameterList(parameters: NodeArray) { - if (checkGrammarForDisallowedTrailingComma(parameters)) { - return true; - } - let seenOptionalParameter = false; const parameterCount = parameters.length; @@ -18149,8 +18144,7 @@ namespace ts { } function checkGrammarArguments(node: CallExpression, args: NodeArray): boolean { - return checkGrammarForDisallowedTrailingComma(args) || - checkGrammarForOmittedArgument(node, args); + return checkGrammarForOmittedArgument(node, args); } function checkGrammarHeritageClause(node: HeritageClause): boolean { diff --git a/tests/baselines/reference/ArrowFunction2.errors.txt b/tests/baselines/reference/ArrowFunction2.errors.txt index 3b9ba21f15a..d18bc483acc 100644 --- a/tests/baselines/reference/ArrowFunction2.errors.txt +++ b/tests/baselines/reference/ArrowFunction2.errors.txt @@ -1,12 +1,9 @@ tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ArrowFunctions/ArrowFunction2.ts(1,13): error TS2304: Cannot find name 'b'. -tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ArrowFunctions/ArrowFunction2.ts(1,14): error TS1009: Trailing comma not allowed. ==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ArrowFunctions/ArrowFunction2.ts (2 errors) ==== var v = (a: b,) => { ~ !!! error TS2304: Cannot find name 'b'. - ~ -!!! error TS1009: Trailing comma not allowed. }; \ No newline at end of file diff --git a/tests/baselines/reference/parserErrorRecovery_ParameterList3.errors.txt b/tests/baselines/reference/parserErrorRecovery_ParameterList3.errors.txt deleted file mode 100644 index df73dc51ce1..00000000000 --- a/tests/baselines/reference/parserErrorRecovery_ParameterList3.errors.txt +++ /dev/null @@ -1,8 +0,0 @@ -tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ParameterLists/parserErrorRecovery_ParameterList3.ts(1,13): error TS1009: Trailing comma not allowed. - - -==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ParameterLists/parserErrorRecovery_ParameterList3.ts (1 errors) ==== - function f(a,) { - ~ -!!! error TS1009: Trailing comma not allowed. - } \ No newline at end of file diff --git a/tests/baselines/reference/parserErrorRecovery_ParameterList3.js b/tests/baselines/reference/parserErrorRecovery_ParameterList3.js deleted file mode 100644 index 4c20e676f40..00000000000 --- a/tests/baselines/reference/parserErrorRecovery_ParameterList3.js +++ /dev/null @@ -1,7 +0,0 @@ -//// [parserErrorRecovery_ParameterList3.ts] -function f(a,) { -} - -//// [parserErrorRecovery_ParameterList3.js] -function f(a) { -} diff --git a/tests/baselines/reference/parserParameterList12.errors.txt b/tests/baselines/reference/parserParameterList12.errors.txt deleted file mode 100644 index 686b29f63b1..00000000000 --- a/tests/baselines/reference/parserParameterList12.errors.txt +++ /dev/null @@ -1,8 +0,0 @@ -tests/cases/conformance/parser/ecmascript5/ParameterLists/parserParameterList12.ts(1,13): error TS1009: Trailing comma not allowed. - - -==== tests/cases/conformance/parser/ecmascript5/ParameterLists/parserParameterList12.ts (1 errors) ==== - function F(a,) { - ~ -!!! error TS1009: Trailing comma not allowed. - } \ No newline at end of file diff --git a/tests/baselines/reference/parserParameterList12.symbols b/tests/baselines/reference/parserParameterList12.symbols new file mode 100644 index 00000000000..efaab7d1b13 --- /dev/null +++ b/tests/baselines/reference/parserParameterList12.symbols @@ -0,0 +1,5 @@ +=== tests/cases/conformance/parser/ecmascript5/ParameterLists/parserParameterList12.ts === +function F(a,) { +>F : Symbol(F, Decl(parserParameterList12.ts, 0, 0)) +>a : Symbol(a, Decl(parserParameterList12.ts, 0, 11)) +} diff --git a/tests/baselines/reference/parserParameterList12.types b/tests/baselines/reference/parserParameterList12.types new file mode 100644 index 00000000000..7ff43cd6a15 --- /dev/null +++ b/tests/baselines/reference/parserParameterList12.types @@ -0,0 +1,5 @@ +=== tests/cases/conformance/parser/ecmascript5/ParameterLists/parserParameterList12.ts === +function F(a,) { +>F : (a: any) => void +>a : any +} diff --git a/tests/baselines/reference/trailingCommasInFunctionParametersAndArguments.js b/tests/baselines/reference/trailingCommasInFunctionParametersAndArguments.js new file mode 100644 index 00000000000..5fb4b7d638f --- /dev/null +++ b/tests/baselines/reference/trailingCommasInFunctionParametersAndArguments.js @@ -0,0 +1,9 @@ +//// [trailingCommasInFunctionParametersAndArguments.ts] +function f1(x,) {} + +f1(1,); + + +//// [trailingCommasInFunctionParametersAndArguments.js] +function f1(x) { } +f1(1); diff --git a/tests/baselines/reference/trailingCommasInFunctionParametersAndArguments.symbols b/tests/baselines/reference/trailingCommasInFunctionParametersAndArguments.symbols new file mode 100644 index 00000000000..5eb9d75807b --- /dev/null +++ b/tests/baselines/reference/trailingCommasInFunctionParametersAndArguments.symbols @@ -0,0 +1,8 @@ +=== tests/cases/conformance/es7/trailingCommasInFunctionParametersAndArguments.ts === +function f1(x,) {} +>f1 : Symbol(f1, Decl(trailingCommasInFunctionParametersAndArguments.ts, 0, 0)) +>x : Symbol(x, Decl(trailingCommasInFunctionParametersAndArguments.ts, 0, 12)) + +f1(1,); +>f1 : Symbol(f1, Decl(trailingCommasInFunctionParametersAndArguments.ts, 0, 0)) + diff --git a/tests/baselines/reference/trailingCommasInFunctionParametersAndArguments.types b/tests/baselines/reference/trailingCommasInFunctionParametersAndArguments.types new file mode 100644 index 00000000000..4af2326076b --- /dev/null +++ b/tests/baselines/reference/trailingCommasInFunctionParametersAndArguments.types @@ -0,0 +1,10 @@ +=== tests/cases/conformance/es7/trailingCommasInFunctionParametersAndArguments.ts === +function f1(x,) {} +>f1 : (x: any) => void +>x : any + +f1(1,); +>f1(1,) : void +>f1 : (x: any) => void +>1 : number + diff --git a/tests/cases/conformance/es7/trailingCommasInFunctionParametersAndArguments.ts b/tests/cases/conformance/es7/trailingCommasInFunctionParametersAndArguments.ts new file mode 100644 index 00000000000..0492ed989e3 --- /dev/null +++ b/tests/cases/conformance/es7/trailingCommasInFunctionParametersAndArguments.ts @@ -0,0 +1,3 @@ +function f1(x,) {} + +f1(1,); diff --git a/tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ParameterLists/parserErrorRecovery_ParameterList3.ts b/tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ParameterLists/parserErrorRecovery_ParameterList3.ts deleted file mode 100644 index c51a52ef83e..00000000000 --- a/tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ParameterLists/parserErrorRecovery_ParameterList3.ts +++ /dev/null @@ -1,2 +0,0 @@ -function f(a,) { -} \ No newline at end of file diff --git a/tests/cases/fourslash/trailingCommaSignatureHelp.ts b/tests/cases/fourslash/trailingCommaSignatureHelp.ts new file mode 100644 index 00000000000..4881571cd2b --- /dev/null +++ b/tests/cases/fourslash/trailingCommaSignatureHelp.ts @@ -0,0 +1,15 @@ +/// + +////function str(n: number): string; +/////** +//// * Stringifies a number with radix +//// * @param radix The radix +//// */ +////function str(n: number, radix: number): string; +////function str(n: number, radix?: number): string { return ""; } + +edit.insert("str(1,"); +verify.currentParameterHelpArgumentNameIs("radix"); +verify.currentParameterHelpArgumentDocCommentIs("The radix"); +verify.currentSignatureHelpIs("str(n: number, radix: number): string"); +verify.currentSignatureHelpDocCommentIs("Stringifies a number with radix");