Skip to content

Add a better error message for disallowed trailing commas after rest elements in function params #7460

@simonbuchan

Description

@simonbuchan

Update

Let's make a nice "A trailing comma is not permitted after the rest element" error for all commas after function params. Currently we throw a nice error for arrow functions in this case, but apparently not for non-arrow functions.

Original Issue below

This is a bug report.

Input Code

// error-input.js
function foo(
    first,
    ...rest
) {
}

// trailing-input.js
function foo(
    first,
    rest,
) {
}

// error-input.js
function foo(
    first,
    ...rest,
) {
}

Babel/Babylon Configuration (.babelrc, package.json, cli command)

// package.json
{
  "dependencies": {
    "@babel/cli": "7.0.0-beta.40",
    "@babel/core": "7.0.0-beta.40"
  }
}

Expected Behavior

> yarn -s babel rest-input.js
function foo(first, ...rest) {}

> yarn -s babel trailing-input.js
function foo(first, rest) {}

> yarn -s babel error-input.js
function foo(first, ...rest) {}

Current Behavior

> yarn -s babel rest-input.js
function foo(first, ...rest) {}

> yarn -s babel trailing-input.js
function foo(first, rest) {}

> yarn -s babel error-input.js
{ SyntaxError: error-input.js: Unexpected token, expected ")" (3:11)

  1 | function foo(
  2 |     first,
> 3 |     ...rest,
    |            ^
  4 | ) {
  5 | }
  6 |
    at Parser.raise (C:\code\personal\bugs\babel-ts-traling-comma\node_modules\babylon\lib\index.js:772:15)
    at Parser.unexpected (C:\code\personal\bugs\babel-ts-traling-comma\node_modules\babylon\lib\index.js:2047:16)
    at Parser.expect (C:\code\personal\bugs\babel-ts-traling-comma\node_modules\babylon\lib\index.js:2035:28)
    at Parser.parseBindingList (C:\code\personal\bugs\babel-ts-traling-comma\node_modules\babylon\lib\index.js:2324:14)
    at Parser.parseFunctionParams (C:\code\personal\bugs\babel-ts-traling-comma\node_modules\babylon\lib\index.js:4560:24)
    at Parser.parseFunction (C:\code\personal\bugs\babel-ts-traling-comma\node_modules\babylon\lib\index.js:4548:10)
    at Parser.parseFunctionStatement (C:\code\personal\bugs\babel-ts-traling-comma\node_modules\babylon\lib\index.js:4206:17)
    at Parser.parseStatementContent (C:\code\personal\bugs\babel-ts-traling-comma\node_modules\babylon\lib\index.js:3923:21)
    at Parser.parseStatement (C:\code\personal\bugs\babel-ts-traling-comma\node_modules\babylon\lib\index.js:3899:17)
    at Parser.parseBlockOrModuleBlockBody (C:\code\personal\bugs\babel-ts-traling-comma\node_modules\babylon\lib\index.js:4426:23)
  pos: 38,
  loc: Position { line: 3, column: 11 },
  code: 'BABEL_PARSE_ERROR' }
error An unexpected error occurred: "Command failed.
Exit code: 1
Command: C:\\windows\\system32\\cmd.exe
Arguments: /d /s /c C:\\code\\personal\\bugs\\babel-ts-traling-comma\
ode_modules\\.bin\\babel error-input.js
Directory: C:\\code\\personal\\bugs\\babel-ts-traling-comma
Output:
".
info If you think this is a bug, please open a bug report with the information provided in "C:\\code\\personal\\bugs\\babel-ts-traling-comma\\yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Your Environment

software version(s)
Babel 7.0.0-beta.40
node v9.6.1
npm yarn 1.5.1
Operating System Win 10

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions