Eric Eric - 3 months ago 10
CoffeeScript Question

How to fix error as implicit braces are forbidden by coffee-lint checking tool?

I have simple CoffeeScript code that is working well when integrated with jQeury.
But

coffee-lint
code checking tool shows the following error


coffeelint file.coffee



Implicit braces are forbidden.


My code is

$ ->
$("#selector").dialog
modal: true


What might be causing this error?

Answer

This would be the minimal change you need. I recommend adding the () for the function call, but this rule doesn't care about those.

$ ->
  $("#selector").dialog {
    modal: true
  }

modal: true is implied to be an object. To demonstrate why this rule is good to have enabled, lets say you had some very similar code that accepts a parameter.

makeDialog = (foo) ->
  $("#selector").dialog
    modal: true,
    foo: foo,

This code looks fine, it even compiles correctly. But at some point you notice that foo: foo can be simplified.

makeDialog = (foo) ->
  $("#selector").dialog
    modal: true,
    foo,

Now your code is broken. CoffeeScript correctly guessed that modal: true is a property on an implied object that is the first parameter of dialog, but it doesn't know if foo is a 2nd property on that object or a 2nd parameter to the function. It ends up compiling out to this:

return $("#selector").dialog({ modal: true }, foo);