tamzinblake / js3-mode

A chimeric fork of js2-mode and js-mode
GNU General Public License v3.0
181 stars 13 forks source link

Indentation in nested arrays/objects with comma-first style #85

Open jfmatt-zz opened 10 years ago

jfmatt-zz commented 10 years ago

Hello. I've run across an issue with indenting a particular piece of code that features an array of object literals when using comma-first style.

The result I'm getting is the following:

var x = [
  {
    a: 123
  , b: 456
  }
, {
  a: 789
, b: 012
}
, {
  a: 'abc'
, b: 'xyz'
}
]

Note the contents and closing braces of the 2nd and 3rd objects, which are all one indentation level too far to the left. The same thing happens whether using tab-mode or spaces, and with all four combinations of the inner and nested values being objects or arrays.

By contrast, with comma-last notation, everything works perfectly/as expected:

var x = [
  {
    a: 123
  , b: 456
  },
  {
    a: 789
  , b: 012
  },
  {
    a: 'abc'
  , b: 'xyz'
  }
]

I'm using all default indentation options. Turning lazy-commas on drags the offending objects (all but the first) even farther to the left, so that the leading commas and closing braces are on the left baseline.

tamzinblake commented 10 years ago

Interesting. Normally I use this sort of style:

var x = [ { a: 123
          , b: 456
          }
        , { a: 789
          , b: 012
          }
        , { a: 'abc'
          , b: 'xyz'
          }
        ]

There's clearly a semantic issue on line 7 of your first example - it should be indenting from the brace, not from the start of the line. I'll have to look into that.

tamzinblake commented 10 years ago

Ah yes, since a is not preceded by an operator, it's falling back to standard continued expression indentation, which just looks at the previous line's indentation, and thus indents from the comma.

Going to have to fester on this one.