Checking JavaScript code for typos in Notepad++ - javascript

I use a combination of Notepad++, JSHint and Cordova CLI to create my hybrid Android web apps - yes, I am aware that there are more sophisticated ways of doing this but tht is not what this question is about.
One of the issues I run into every now and again is a typographic - as opposed to syntactic which is easily caught by JSHint - error which only manifests itself once I am compiled the APK and tried to run it. For instance
docment instead of document (missing u)
getElmentByID instead of getElementById (missing e, wrong capitalization on ID)
The one thing I really love about Java is that the compiler stops you dead in your tracks and demands that you fix such errors first. That is not the case with JavaScript where the best one can do is test for correct syntax.
However, I am wondering - perhaps there is a way (maybe a NPP plugin that I am unaware of) to check for such errors?

Use typescript or flowtype to catch those, and many more errors. Alternatively, find a decent syntax theme that's set up to highlight DOM methods.

Related

What is the most effective JavaScript syntax checking solution?

I am developing an AJAX application and in the process of doing this I am creating many small JS files that will eventually be concatenated and minified in the production release. However in development I will be working with the individual uniminified files.
The issue I have is that I am using Kate in Ubuntu as my editor and when I make small errors like an extra comma in a JSON structure, or an extra parenthesis, colon, semi-colon, or other simple syntax error I have no way of knowing until I see it in Firebug or IE Developer Toolbar (run in a VM). Unfortunately Firebug isn't very good at identifying which line has the syntax error and only IE complains about extra commas. If I could see these mistakes in real-time like a good modern IDE it would be awesome. However, I'll settle for any syntax checking solution that doesn't require me to copy and paste each file from my local machine into a text area.
Also, JSLint goes a bit overboard with it's checking. Half of the things it complains about I do intentionally.
What are my options?
jshint ( http://www.jshint.com/ )is one of the descendants of jslint after several people had also such problems with jslint (being too opinionated).
You could do similar things I guess with jshint as well as it is done by jslint here: http://sergioserra.posterous.com/using-jslint-with-kate
Personally, I used vim with jslint (with a much more permissible configuration than the default), which was checking syntax live.
Another alternative is Closure Linter from Google: https://developers.google.com/closure/utilities/

How to enforce semicolons in JavaScript?

How best does the developer who's decided he likes semicolons at the end of his JavaScript statements enforce that expectation technically for himself?
I'm using Visual Studio (ASP.NET webapps), but I'd love to hear about solutions for any environment.
You could somehow integrate Javascript Lint into the IDE.
I sometimes run my JS through a handler that strips all new lines, comments, tabs, and extra whitespace automatically. If my JS statements don't end with a semi-colon I'll get an error (in most cases)...doing it this way is a PAIN to debug though (e.g., error on line 1 character 2654).
The other way I use is Firebug + YSlow. You can check it's built in JSLint every once in a while.
Maybe there is a file in Visual Studio for checking javascript syntax you may be able to modify like there is for (X)HTML and XML.

JScript syntax verifier?

I'm new to JScript coming from a C++ world.
I'm quite surprised that expressions are evaluated at run-time. What I mean is if I added a function and didn't provide its definition, the program would "crash" (in the debugger) when I run it.
It's also funny how I can just type gibberish anywhere and only at run-time the debugger would complain.
But the "live" page wouldn't!
Is there any JScript add-on tool that checks its syntax while we type? I'm currently using Aptana Studio.
Or is the debug button equivalent to compiling in the web world? But then what if you had million lines of code and some little function was not defined or misspelled? That's a scary thought to me. =S
JSLint is considered by many to be the best syntax checker. I don't know that there is an add-in for your particular situation, but you should be able to add it to your build process pretty easily.
Update: Apparently there are some established techniques for integrating it with Aptana.
Welcome to the world of scripting!

Preventing auto-creation of global variables in Javascript

I just spent some time debugging a problem that boiled down to forgetting to use the var keyword in front of a new variable identifier, so Javascript was automatically creating that variable in the global scope. Is there any way to prevent this, or change the default behavior, without using a validator like JSLint?
Running a validator in between writing and executing Javascript code seems like a poor excuse for compiling, which is the step I'd usually rely on to catch this sort of thing.
I'm guessing the answer is a "no," so I'm looking into a JSLint Eclipse plugin as I post this.
ES5 strict mode prevents automatic creation of global variables, but it's probably going to be a year before there are any shipping browsers that recognise strict mode, so JSLint is probably your best bet until then :-/
Check this handy bookmarklet created by Remy Sharp, you can use it to check whether any variables have slipped out to the global namespace.
Not that I know of -- JS developers have historically been adverse to adding "seat belts" to the core language, so they've been implemented in external tools like JSLint instead. You might consider adding JSLint to your build/test process via it's Rhino version, and make the test suite fail if JSLint reports errors.
The answer is indeed no - but you could use the Eclipse plugin as you said, which can as far as I know run when saving etc., so it won't require additional steps.
IntelliJ IDEA is also able to spot undeclared variables. IntelliJ does it real-time as you type and can also analyze your codebase jslint-style. Probably some other alternatives too.
I made a bookmarklet some time ago to inspect and detect global variables.
(source: thinkweb2.com)
It has some additional features (like filtering out Prototype.js or Google Analytics properties) too.
Install Firebug, and enable it on your site. It will complain each time you create a global without var, as well as some other situations which you might or might not want to catch.
You can also do this using the about:config option javascript.options.strict, but since that's global it'll affect all the other sites you browse too.

Firefox add-on tools for checking JavaScript syntax?

Not sure if there is any Firefox add-on tool for checking JavaScript syntax, var declaration, or even pre-compiling available?
I find out is very difficult to debug JavaScript in a web html page. I have to add some script there. When the scripts get very big or long, it stops working. Basically, there must be some bugs. It is very frustrating even the load event stop working. I do need some good tools to find out the bugs.
I have tried FireBug. It is good but not enough. Maybe I don't know all its features. Anyway, I need any good suggestions.
This page may help you a bit as it also contains a screencast.
Debugging Javascript in Firefox with Firebug
It's not strictly an Add-On (although you can obviously run it in a Firefox web page), but I've found JSLint to be helpful in exposing expression anomalies that might or might not be strictly invalid syntax (although it will catch all those, too.)
My suggestion is to check you javascript with JSLint. JSLint will show you common problems in javascript code and helps you to create strict and compatible code.
YSlow includes JSLint as Tool.
As an alternative to FireBug check this debugger: Venkman JavaScript Debugger, has been out there for a while, and it's not bad at all...
I have always been a firebug fan and it sure is one of the best debuggers out there. Any JS issue Press F12, Enable the console for catching the errors and then you can find the exact line causing the error. Add a breakpoint and you can see the exact values of variables too. Very intuitive UI..just get used to using it and it will save a lot of your time.
I HIGHLY recommend Rainbow. It's still in Beta and only works in FF3, but it offers syntax highlighting. That will get you half-way there when it comes to syntax.
I constantly use firebug with a js file that has some 15000 lines. I've had no problems with it (sometimes I have to wait a few seconds for it to scroll to the breakpoint but even then it works fine).
Unfortunately JSLint does'nt work for us. We use some third party code that while beeing "valid" has unnecessary ";" according to jsLint. The code is in the beginning of our js file and jslint stops analyzing at 3% because it thinks there are too many errors (all of them unnecessary ";".)
You're missing the point on 2 counts:
First, being presented with a gazillion unnecessary error messages will make it impossible to find the problem in the first place.
Secondly, the errors are unnecessary. I used to use JSLint religiously, but now it's become too much of a code Nazi and I can't use it.
I'm aware that many of the things it checks for can be disabled, but I just don't have enough time to research what I'd have to do.
The issue is the usability of the default configuration. I, like the original poster, am just looking for a way to find true JavaScript syntax errors very quickly, including on HTML pages. I'm sure that Firebug can do it, but as in the case with JSLint, I don't have the spare time to find out how.
I do, however, use Firebug for debugging, though the version I'm using (1.10.3) has a bad habit of displaying errors, but reporting an incorrect file and line number.
Hi Gene: you could change one variable option.maxerr inside your jslint.js file into the number you want.
The default value is 50, you could change it to 500 or 1000 to suite your taste.
I use both the following for code validation.
JSHint # http://jshint.com
JSLint FF Plugin. Get at https://addons.mozilla.org/en-us/firefox/addon/jslinter/
Alternatively, jshint and jslint plugins are available for text editors like Notepad ++, sublime etc.

Categories

Resources