views:

380

answers:

2

Duplicate: http://stackoverflow.com/questions/605461/vs2008-removes-my-indentation-in-js-file (Turn off auto formatting in the formatting tab)

Hi I have VS2008 and ReSharper 4.0 and I'm having a problem. When I enter a new line of code in any .js file, it goes thru and reformats all of the JavaScript braces. I've found where in ReSharper where I can set brace behavior for C# (.cs) files. I've searched in VS2008 and couldn't find any settings for JavaScript code.

    function doSomething(withMe) 
    {
     for (idx in myArray) 
     {
      if (myArray[idx] != -1) 
      {
       return "yep";
      }
     }
     return "nope";
    }

Become this (which I like better, but my team likes the above instead)

    function doSomething(withMe) {
     for (idx in myArray) {
      if (myArray[idx] != -1) {
       return "yep";
      }
     }
     return "nope";  
    }

It's getting painful to go back and re-add the CRs everywhere. Where can I find the setting to make this madness stop.

+6  A: 
cjibo
Thanks for the answer. This has been driving me crazy for a long time.
Michael Kniskern
Apparently, JScript setting formats JavaScript code too. No wonder I couldn't find it. :) Thanks for the pointer.
I strongly discourage you to do this. See the other answers in for this question.
Dave Van den Eynde
+4  A: 

For javascript you should get into the habit of putting your braces at the end of the line, like visual studio suggests. The reason for this is that the interpreter can, occasionally, add in a ; when interpreting the file which can lead to odd behaviour. For example:

return
{
    'foo': 3
};

this returns undefined, not the hash literal as you'd expect, because the interpreter turns it into:

return;
{
    'foo': 3
};

You may think I'm making this up. I assure you I am not.

I suggest you educate your team and get into the habit of formatting your javascript as VS suggests. It'll save you from yourself.


Also, another thing. Instead of doing:

myArray[idx] != -1

I suggest you do:

myArray[idx] !== -1

This will stop it from doing type coercion. Infact... never use == or != always use === and !==.

jonnii
Lol. The myArray[idx] != -1 was just some dummy code I threw in there. We don't have that in our code, just came out of my mind. But interesting point. I have no idea what coercion is, but it sounds important.
Type coercion is the way javascript will do type comparisons. For example ' \t\r\n ' == 0 evaluates to true.
jonnii