Friday, December 23, 2011

Appcelerator, JavaScript, and Lint

I've been using Appcelerator for a few months now. That means programming in JavaScript, and it's a little scary: out of the box, JavaScript is too forgiving. Things like accidental globals don't belong in production software. But it's tough to keep them out of a couple thousand lines of code without some help from tools.

For me, the right tool is JavaScript Lint. It's easy to integrate into an Appcelerator project. Suppose you're working on fooapp, with a top-level directory of the same name:

  • Download and install JavaScript Lint for your OS;
  • Copy jsl.default.conf from the installation directory to fooapp, and rename it to jsl.conf;
  • Edit jsl.conf. Set always_use_option_explicit to +
  • At the bottom of jsl.conf, insert:

https://gist.github.com/1515692

The +define lines tell JavaScript Lint about the globals in your project, so that it doesn't report them as problems.

Now, if you run jsl -conf jsl.conf from the command line in fooapp, it checks your JavaScript and sends the output to stdout. You may also be able to hook the command up to your text editor so that every save automatically runs the checks.

And it's easy to skip safe problem code. I had some third-party code that was triggering lots of warnings, but I trusted the soundness of the code and didn't want to change it. So, to eliminate the warnings, I bracketed the code within a couple of special comments:

https://gist.github.com/1515697

No comments:

Post a Comment