how to make visual studio javascript formatting work? - Stack Overflow

admin2025-04-17  0

For some reason Visual Studio javascript formatting does not understand the syntax:

(function(){

})();

I'm getting "expected expression" in the second character of this line: })();

So I was wondering if anyone had this problem before (want to format their code but visual studio stubbornly thinks the valid code has an error and refuses to do so) and how do we solve it?

For some reason Visual Studio javascript formatting does not understand the syntax:

(function(){

})();

I'm getting "expected expression" in the second character of this line: })();

So I was wondering if anyone had this problem before (want to format their code but visual studio stubbornly thinks the valid code has an error and refuses to do so) and how do we solve it?

Share Improve this question edited Nov 26, 2011 at 19:24 Adam Wagner 16.1k7 gold badges53 silver badges67 bronze badges asked Jul 4, 2011 at 13:25 NamesisNamesis 411 silver badge3 bronze badges 6
  • what kind of error is it giving you though? Technically, you're supposed to put the open/close parens right after the closing curly brace before the last closing curly brace. that could possibly be an error to the visual studio parser. – hellatan Commented Jul 4, 2011 at 13:30
  • i don't quite get you mate. btw i've updated my question to answer your question. – Namesis Commented Jul 4, 2011 at 13:51
  • 1 I cannot reproduce this using VS 2010 Ultimate SP1. How are you getting this? Also, if you want to pass JSLint test, use this: (function(){}()); – Mrchief Commented Jul 4, 2011 at 19:38
  • @namesis - Mrchief's example to pass jslint is what i meant – hellatan Commented Jul 5, 2011 at 15:01
  • Like @Dan said, looks like (function(){ needs to be the first piece of code in the file to get the formatter to work. Even the JSLint conformant expression doesn't help if you have a leading semicolon before (function(){. – Oliver Commented May 15, 2013 at 10:24
 |  Show 1 more ment

4 Answers 4

Reset to default 3

It seems that in order to make the Visual Studio JS piler happy, you need to have the "(function(){" be the first line of code in your .js file. You can precede it with ments/whitespace, if you'd like, but no code before it.

Sorry, I can't explain why it is that way but it should fix your problem.

Try the following:

(function () {

} ());

i.e., I've moved the function argument brackets inside the overall brackets.

Use following shortcut:

ctrl k+d

I had to terminate all Node.exe tasks. After reopening my Javascript files, the ctrl+k,ctrl+d started working again. (Visual Studio 2017)

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744885672a272520.html

最新回复(0)