10

I want to convert

console.log({
  a: 'a'
}, {
  b: 'b'
});

into CoffeeScript. The only way I found is

console.log
  a: 'a',
    b: 'b'

It seems bizarre that a: 'a' and b: 'b' are not indented the same when they are essentially symetric in this situation.

1
  • Just because you can leave off braces/parens in coffeescript, it doesn't mean you should. This is probably one of the "shouldn't" cases. Commented Feb 12, 2012 at 23:20

4 Answers 4

31

Put the comma one in a separated line, one indentation level less than the hash/object, so it's treated as part of the function invocation.

console.log
   a: 'a'
, # indentation level matters!
   b: 'b'

this will not work because the indentation level is the same as hash, so it's treated as part of the hash.

console.log
   a: 'a'
   ,
   b: 'b'
Sign up to request clarification or add additional context in comments.

Comments

13

Or you could use braces, which do work in CS:

console.log {a:'a'}, {b:'b'}

1 Comment

Right, braces and parentheses are (usually) optional but there's no reason for contortions to avoid them.
1

Well, if you think about the parsing rules,

a: 'a',
b: 'b'

actually means

{ a: 'a', b: 'b' }

Since this isn't the behaviour you want, you need to tell the parser that the line with b: is another object. Indenting will do that for you. Now this wasn't really a question, but I hope it helps you understand why to do it the way you described. It is the right way.

Comments

1
$ coffee -bce 'console.log(a: "a"; b: "b")'
// Generated by CoffeeScript 1.2.1-pre

console.log({
  a: "a"
}, {
  b: "b"
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.