admin管理员组

文章数量:1201582

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?

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?

Share Improve this question edited Sep 29, 2011 at 13:06 Sebastian Wramba 10.1k8 gold badges42 silver badges59 bronze badges asked Sep 29, 2011 at 13:04 Chris TolworthyChris Tolworthy 2,0964 gold badges20 silver badges24 bronze badges 5
  • "Unexpected end of input" is almost certainly a missing bracket, not an extra one. – cdeszaq Commented Sep 29, 2011 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 Commented Sep 29, 2011 at 13:08
  • You could try to parse your file with lint: javascriptlint.com/online_lint.php If you have problems with the size of the file try to split it into smaller ones... – mamoo Commented Sep 29, 2011 at 13:09
  • 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 Commented Sep 29, 2011 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 Commented Aug 12, 2016 at 10:52
Add a comment  | 

4 Answers 4

Reset to default 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.

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

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 )

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

本文标签: javascriptMismatched parentheses a quick way to find themStack Overflow