Skip to content

Bug: Functions as non-final arguments #5487

@a1q0

Description

@a1q0

CoffeeScript 2.7.0

Expected Behavior:

The compiler should recognize and properly process function literals when they are used as arguments in a function call, especially when these function literals are not the last argument.

Actual Behavior:

The compiler throws a syntax error at the comma following the function.

How to reproduce :

foo(() => 1, 2) 

# bug.coffee:2:13: error: unexpected ,
# foo(() => 1, 2)	
#              ^	

direct equivalent works fine in javascript :

foo(() => 1, 2)

Syntax ambiguity ?

Since object literal braces a: 'a', b: 'b' can be omitted if shorthands properties are used a, b then we can't know if the intent was to pass an object literal with shorthand props a, b or several arguments a, b.

An object literal with braces and shorthand props: foo(() => { 1, 2 }) transpiles to foo(() => { return {1: 1, 2: 2}; }); as expected.

So, wouldn't it be neater if the currently broken foo(() => 1, 2) didn't throw a syntax error, but picked an opiniated default ?

I'd rather write foo(() => {1, 2}) than foo((() => 1), 2) :/

btw foo(() => {1, 2}, 3) doesn't work either. 'unexpected ,'

best workaround for now

this works fine but can't take arguments

foo((-> 1), 2)
foo((=> 1), 2)

alternatively add a line return after the ,

foo(
    () => 1,
    2
)
# transpile to javascript as expected
foo(() => {
  return 1;
}, 2);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions