I just rearranged a very large JavaScript file. I now get "Unexpected end of input." Somewhere in those hundred of functions, one has lost (or gained) a bracket. What's the quickest way to find it?

- 10,087
- 8
- 41
- 58

- 2,066
- 4
- 20
- 24
-
"Unexpected end of input" is almost certainly a missing bracket, not an extra one. – cdeszaq Sep 29 '11 at 13:06
-
2Do you have an editor that will collapse braces, or show their scope visually in the left margin, or has a "go to other end of scope" command, or has a separate "outline" window? – Ed Staub Sep 29 '11 at 13:08
-
1Thanks. I'll look for an editor that collapses braces. I was looking for a highlighter, and had not thought of collapsing. Thanks. – Chris Tolworthy Sep 29 '11 at 13:32
-
While I agree with using a folding editor, I found mine by using the WebStorm IDE which keeps every version of the file that you ever save and makes it easy to comapre them side by side with your current version. That's how I found my problem – Mawg says reinstate Monica Aug 12 '16 at 10:52
-
You could try to parse your file with lint: http://www.javascriptlint.com/online_lint.php If you have problems with the size of the file try to split it into smaller ones... – mamoo Sep 29 '11 at 13:09
4 Answers
A good trick when missing a brace in eclipse is to go to the final brace in the source module and double-click it. That will highlight all the way back to what it THINKS is the matching open brace. Where it highlights back to is invariably the START of where the problem is, so skip that open brace and go to the next one and start double-clicking open braces and you will usually find where the brace is missing pretty quickly. I learnt that the hard way with a source code file of 20,000+ lines of code and getting hundreds of errors without the slightest indication as where the real problem was as the errors started appearing thousands of lines earlier in the code.

- 61
- 1
- 1
Re-format the file using something that indents well. Look for something that's too far to the left.

- 158,873
- 26
- 254
- 302
-
Normally I would, but I use Marc Kerbiquet's "code browser." It's incredibly helpful for organizing code. But the one downside is that adding more indents would make it much less useful. But I will definitely do that in smaller files. – Chris Tolworthy Sep 29 '11 at 13:37
-
It's still just a file; put it in an editor that does indenting. (I mean, yes, jslint is fine too, just sayin'.) – Dave Newton Sep 29 '11 at 13:44
Minimize the nesting of functions. It reduces the quality of the code (maintainability-wise).

- 20,506
- 2
- 28
- 69
-
2I am quite a newb - I did not know you could nest functions in JavaScript (in my last language that wasn't possible.) You have given me evil ideas, thanks! – Chris Tolworthy Sep 29 '11 at 13:34
-
4Appropriately nested functions _improve_ the quality of the code, they don't reduce it. – nnnnnn Sep 29 '11 at 13:44
-
The key word is "Appropriately" too often it's lazy, quick and dirty, programming. – Steve Wellens Sep 29 '11 at 16:20