Allow trailing commas in function parameter and argument lists

This commit is contained in:
Andy Hanson 2016-06-02 07:35:58 -07:00
parent 166f399d17
commit 4281bf5752
13 changed files with 62 additions and 41 deletions

View file

@ -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<TypeNode>; // 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 = <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>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<ParameterDeclaration>) {
if (checkGrammarForDisallowedTrailingComma(parameters)) {
return true;
}
let seenOptionalParameter = false;
const parameterCount = parameters.length;
@ -18149,8 +18144,7 @@ namespace ts {
}
function checkGrammarArguments(node: CallExpression, args: NodeArray<Expression>): boolean {
return checkGrammarForDisallowedTrailingComma(args) ||
checkGrammarForOmittedArgument(node, args);
return checkGrammarForOmittedArgument(node, args);
}
function checkGrammarHeritageClause(node: HeritageClause): boolean {

View file

@ -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.
};

View file

@ -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.
}

View file

@ -1,7 +0,0 @@
//// [parserErrorRecovery_ParameterList3.ts]
function f(a,) {
}
//// [parserErrorRecovery_ParameterList3.js]
function f(a) {
}

View file

@ -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.
}

View file

@ -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))
}

View file

@ -0,0 +1,5 @@
=== tests/cases/conformance/parser/ecmascript5/ParameterLists/parserParameterList12.ts ===
function F(a,) {
>F : (a: any) => void
>a : any
}

View file

@ -0,0 +1,9 @@
//// [trailingCommasInFunctionParametersAndArguments.ts]
function f1(x,) {}
f1(1,);
//// [trailingCommasInFunctionParametersAndArguments.js]
function f1(x) { }
f1(1);

View file

@ -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))

View file

@ -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

View file

@ -0,0 +1,3 @@
function f1(x,) {}
f1(1,);

View file

@ -0,0 +1,15 @@
/// <reference path="fourslash.ts" />
////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");