For AI agents: the complete documentation index is available at /tc39-atlas/llms.txt, the full documentation bundle is available at /tc39-atlas/llms-full.txt, and this page is available as Markdown at /tc39-atlas/proposals/stage/4/proposal-trailing-function-commas.md.
  • 简体中文
  • Trailing commas in function parameter lists and calls S4

    中文标题:函数参数列表和调用中允许尾随逗号的提案

    提案概览
    提案速览

    该提案解决了在多行函数定义或调用中添加参数时需要修改前一行(添加逗号)并添加新行,从而影响版本控制 blame 信息的问题。解决方案是允许在函数参数列表和调用中使用尾随逗号,与其他语言的功能一致。

    Note

    以下 README 来自上游仓库,其中的阶段或状态标注可能滞后;当前信息以提案概览为准。

    允许在函数参数列表中使用尾随逗号的提案

    在某些代码库/风格指南中,会出现函数调用和定义被跨多行拆分的场景,如下所示:

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

    在这些情况下,当其他代码贡献者后来在这些参数列表中添加另一个参数时,他们必须进行两次行更新:

     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: );

    在版本控制系统(git、subversion、mercurial 等)管理的代码上执行此更改的过程中,第 3 行和第 9 行的 blame/annotate 代码历史信息会被更新为指向添加逗号的人(而不是最初添加参数的人)。

    为了缓解这个问题,一些其他语言(Python、D、Hack,可能还有其他语言)已经添加了语法支持,允许在这些参数列表中使用尾随逗号。这使得代码贡献者总是以尾随逗号结束参数的添加,并且再也不必担心代码归属问题:

     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: );

    请注意,本提案纯粹是关于语法,并且不对语义做任何更改,因此尾随逗号的存在对诸如 <<function>>.length 之类的内容没有任何影响。

    此仓库包含提案幻灯片,一个被修改以允许在参数列表中使用尾随逗号的 esprima 版本,以及一个非常简单的 CLI 实用程序,用于证明在构建步骤中将尾随逗号转译为 ES5 兼容的非尾随逗号是可能的(而且容易)。

    对于 CLI,您可以给它一个单独的文件名参数从磁盘读取,或者将源代码文本通过管道传输给它。

    规范文本

    参见 http://jeffmo.github.io/es-trailing-function-commas/