JSLint is Javascript validator. It saves your ass from common Javascript pitfalls. This post tells you how to use JSLint with Eclipse, or more precisisely Aptana Studio. For example, JSLint will caught extra commas in your JS files which would choke on Internet Explorer, but run on other browsers.
Aptana Studio Javascript has ability to scan your Javascript while typing and highlight the errors.
By default Aptana Studio uses a validator called Mozilla validator which is very lax. Your Javascript code will run on Firefox, but that alone doesn’t make many web developers happy 🙁
First you need to enable JSList validator in Settings -> Aptana Studio -> Validation -> Javascript.
Now you can see how bad your Javascript code is (bottom window):
Errors and warnings are highlighted in the Javascript editor left marginal when you type. There is also Problems view which shows all the errors and warnings in your workspace. The Problems view can be found Show -> View -> Problems.
1. Hinting globals for JSLint
You can also use JSLint global hints per source file to hint what globals the file has available, so JSLint won’t complain.
For example, to hint that global krusovice namespace is available, add the following line to the beginning of the file:
/*global krusovice*/
Note that not having wrapping spaces is important here.
2. Common warnings to ignore
JSLint is very accurate with every detail – like a Finnish-Swedish lady when it comes to cleaning your apartment. You probably don’t want to see red marker all over you code, so you might want to tune down warnings a bit.
Add these to Settings -> Aptana Studio -> Validation -> Javascript -> Ignore.
Don’t complain about missing jQuery or Console:
.*'(\$|jQuery)' is not defined.*
And
.*'console' is not defined.*
3. Using sane tab setting and fixing mixed spaces and tabs
Turn on: Settings -> Aptana Studio -> Javascript -> Tab policy -> Use spaces to make your source code to be friendly with all the other text editors in the world who use tab size 8. (Who Eclipse has managed to f*k up this?)
You probably want also to fix those “Mixed tabs and spaces” errors. Use AnyEdit plug-in for Eclipse to convert tabs to spaces in the selected file (see Installation section).
First you need to set sane tab site for AnyEdit, which defaults to 2 spaces (looks like you can nbe even more f*ed up as stated above). You need to fix this in Settings -> General -> Editor -> AnyEdit tools -> Convert. Set Eclipse default, 4, there.
Then select all text in the editor, right click -> Convert tabs to spaces.
What is missing
I could not find a way to ignore files. This is very irritating if you have something like jQuery UI in your source tree and you cannot supress warnings for the problematic files. Ignore regular expressions patterns don’t seem to match path or file part of the error.
Subscribe to RSS feed Follow me on Twitter Follow me on Facebook Follow me Google+
Pingback: Elegant D » Using Javascript JSLint validator in Eclipse and Aptana Studio …