18

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?

Sebastian Wramba
  • 10,087
  • 8
  • 41
  • 58
Chris Tolworthy
  • 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
  • 2
    Do 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
  • 1
    Thanks. 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 Answers4

6

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.

CESDewar
  • 61
  • 1
  • 1
2

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

Dave Newton
  • 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
0

Try the Esprima parser. It comes with a syntax validator that will give you the line number of each error.

npm install --global esprima
esvalidate path/to/file.js

outputs

path/to/file.js:915: Unexpected token )
mareoraft
  • 3,474
  • 4
  • 26
  • 62
-1

Minimize the nesting of functions. It reduces the quality of the code (maintainability-wise).

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69