Stage 4 Draft / July 28, 2016

Trailing Commas in Function Param Lists

Introduction#

In some codebases/style guides, there are scenarios that arise where function calls and definitions are split across multiple lines in the style of:


    1: function clownPuppiesEverywhere(
    2:   param1,
    3:   param2
    4: ) { /* ... */ }
    5:  
    6: clownPuppiesEverywhere(
    7:   'foo',
    8:   'bar'
    9: );
	

In these cases, when some other code contributer comes along and adds another parameter to one of these parameter lists, they must make two line updates:


     1: function clownPuppiesEverywhere(
     2:   param1,
     3:   param2, // updated to add a comma
     4:   param3  // updated to add new parameter
     5: ) { /* ... */ }
     6: 
     7: clownPuppiesEverywhere(
     8:   'foo',
     9:   'bar', // updated to add a comma
    10:   'baz'  // updated to add new parameter
    11: );
	

In the process of doing this change on code managed by a version control system (git, subversion, mercurial, etc), the blame/annotation code history information for lines 3 and 9 get updated to point at the person who added the comma (rather than the person who originally added the parameter).

To help mitigate this problem, some other languages (Python, D, Hack, ...probably others...) have added grammar support to allow a trailing comma in these parameter lists. This allows code contributors to always end a parameter addition with a trailing comma in one of these per-line parameter lists and never have to worry about the code attribution problem again:


     1: function clownPuppiesEverywhere(
     2:   param1,
     3:   param2, // Next parameter that's added only has to add a new line, not modify this line
     5: ) { /* ... */ }
     6: 
     7: clownPuppiesEverywhere(
     8:   'foo',
     9:   'bar', // Next parameter that's added only has to add a new line, not modify this line
    11: );
	

Note that this proposal is exclusively about grammar and makes no changes to semantics, therefore the presence of a trailing comma has no effect on things like <<function>>.length.

This repo contains the original proposal slides, a version of esprima hacked to allow trailing commas in parameter lists, and a very simple CLI utility to show that it's possible (and easy) to transpile trailing commas to ES5-compatible non-trailing commas in a build step.

1Modified Productions#

1.1Arguments#

Arguments[Yield]:() (ArgumentList[?Yield]) (ArgumentList[?Yield],)

1.2CoverParenthesizedExpressionAndArrowParameterList#

CoverParenthesizedExpressionAndArrowParameterList[Yield]:(Expression[In, ?Yield]) (Expression[In, ?Yield],) () (...BindingIdentifier[?Yield]) (...BindingPattern[?Yield]) (Expression[In, ?Yield],...BindingIdentifier[?Yield]) (Expression[In, ?Yield],...BindingPattern[?Yield])

1.3FormalParameters#

FormalParameters[Yield]:[empty] FunctionRestParameter[?Yield] FormalParameterList[?Yield] FormalParameterList[?Yield], FormalParameterList[?Yield],FunctionRestParameter[?Yield]

1.3.1Static Semantics: BoundNames#

FormalParameters[Yield]:FormalParameterList[?Yield],FunctionRestParameter[?Yield]

  1. Let names be BoundNames of FormalParameterList
  2. Append to names the BoundNames of FunctionRestParameter
  3. Return names

1.3.2Static Semantics: ContainsExpression#

FormalParameters[Yield]:FormalParameterList[?Yield],FunctionRestParameter[?Yield]

  1. Return ContainsExpression of FormalParameterList

1.3.3Static Semantics: ExpectedArgumentCount#

FormalParameters[Yield]:FormalParameterList[?Yield],FunctionRestParameter[?Yield]

  1. Return ExpectedArgumentCount of FormalParameterList

1.3.4Static Semantics: IsSimpleParameterList#

FormalParameters[Yield]:FormalParameterList[?Yield],FunctionRestParameter[?Yield]

  1. Return false

1.3.5Runtime Semantics: IteratorBindingInitialization#

FormalParameters[Yield]:FormalParameterList[?Yield],FunctionRestParameter[?Yield]

  1. Let restIndex be the result of performing IteratorBindingInitialization for FormalParameterList using iteratorRecord and environment as the arguments.
  2. ReturnIfAbrupt(restIndex).
  3. Return the result of performing IteratorBindingInitialization for FunctionRestParameter using iteratorRecord and environment as the arguments.

1.4FormalParameterList#

FormalParameterList[Yield]:TODO:Removemeafterbugfixfordel/first-emu-rhsissue FunctionRestParameter[?Yield] FormalsList[?Yield] FormalsList[?Yield],FunctionRestParameter[?Yield] FormalParameter[?Yield] FormalParameterList[?Yield],FormalParameter[?Yield]

1.4.1Static Semantics: BoundNames#

FormalParameterList[Yield]:FormalsList[?Yield],FunctionRestParameter[?Yield]

  1. Let names be BoundNames of FormalsList.
  2. Append to names th eBoundNames of FunctionRestParameter.
  3. Return names.

FormalParameterList[Yield]:FormalParameterList[?Yield],FormalParameter[?Yield]

  1. Let names be BoundNames of FormalParameterList.
  2. Append to names the BoundNames of FormalParameter.
  3. Return names.

1.4.2Static Semantics: ContainsExpression#

FormalParameterList[Yield]:FunctionRestParameter[?Yield]

  1. Return true.

FormalParameterList[Yield]:FormalsList[?Yield],FunctionRestParameter[?Yield]

  1. Return ContainsExpression of FormalsList.

FormalParameterList[Yield]:FormalParameterList[?Yield],FormalParameter[?Yield]

  1. If ContainsExpression of FormalParameterList is true, return true.
  2. Return ContainsExpression of FormalParameter.

1.4.3Static Semantics: ExpectedArgumentCount#

FormalParameterList[Yield]:FunctionRestParameter[?Yield]

  1. Return 0.

FormalParameterList[Yield]:FormalsList[?Yield],FunctionRestParameter[?Yield]

  1. Return the ExpectedArgumentCount of FormalsList. Note The ExpectedArgumentCount of a FormalParameterList is the number of FormalParameters to the left of either the rest parameter or the first FormalParameter with an Initializer. A FormalParameter without an initializer is allowed after the first parameter with an initializer but such parameters are considered to be optional with undefined as their default value.

FormalParameterList[Yield]:FormalParameterList[?Yield],FormalParameter[?Yield]

  1. Let count be ExpectedArgumentCount of FormalParameterList.
  2. If HasInitializer of FormalParameterList is true or HasInitializer of FormalParameter is true, return count.
  3. Return count+1.

1.4.4Static Semantics: HasInitializer#

FormalParameterList[Yield]:FormalParameterList[?Yield],FormalParameter[?Yield]

  1. If HasInitializer of FormalParameterList is true, return true.
  2. Return HasInitializer of FormalParameter.

1.4.5Static Semantics: IsSimpleParameterList#

FormalParameterList[Yield]:FunctionRestParameter[?Yield]

  1. Return false.

FormalParameterList[Yield]:FormalsList[?Yield],FunctionRestParameter[?Yield]

  1. Return false.

FormalParameterList[Yield]:FormalParameterList[?Yield],FormalParameter[?Yield]

  1. If IsSimpleParameterList of FormalParameterList is false, return false.
  2. Return IsSimpleParameterList of FormalParameter.

1.4.6Runtime Semantics: IteratorBindingInitialization#

FormalParameterList[Yield]:FormalsList[?Yield],FunctionRestParameter[?Yield]

  1. Let restIndex be the result of performing IteratorBindingInitialization for FormalsList using iteratorRecord, and environment as the arguments.
  2. ReturnIfAbrupt(restIndex).
  3. Return the result of performing IteratorBindingInitialization for FunctionRestParameter using iteratorRecord and environment as the arguments.

FormalParameterList[Yield]:FormalParameterList[?Yield],FormalParameter[?Yield]

  1. Let restIndex be the result of performing IteratorBindingInitialization for FormalParameterList using iteratorRecord, and environment as the arguments.
  2. ReturnIfAbrupt(restIndex).
  3. Return the result of performing IteratorBindingInitialization for FormalParameter using iteratorRecord and environment as the arguments.

1.5FormalsList#

The FormalsList production is removed.

FormalsList[Yield]:TODO:Removemeafterbugfixfordel/first-emu-rhsissue FormalParameter[?Yield] FormalsList[?Yield],FormalParameter[?Yield]

1.5.1Static Semantics: BoundNames#

FormalsList[Yield]:FormalsList[?Yield],FormalParameter[?Yield]

  1. Let names be BoundNames of FormalsList.
  2. Append to names the BoundNames of FormalParameter.
  3. Return names.

1.5.2Static Semantics: ContainsExpression#

FormalsList[Yield]:FormalsList[?Yield],FormalParameter[?Yield]

  1. If ContainsExpression of FormalsList is true, return true.
  2. Return ContainsExpression of FormalParameter.

1.5.3Static Semantics: ExpectedArgumentCount#

FormalsList[Yield]:FormalParameter[?Yield]

  1. If HasInitializer of FormalParameter is true, return 0.
  2. Return 1.

FormalsList[Yield]:FormalsList[?Yield],FormalParameter[?Yield]

  1. Let count be the ExpectedArgumentCount of FormalsList.
  2. If HasInitializer of FormalsList is true or HasInitializer of FormalParameter is true, return count.
  3. Return count+1.

ACopyright & Software License#

Copyright Notice

© 2016 Jeff Morrison

Software License

All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT http://www.ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the authors nor Ecma International may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.