Compile-time JS syntax checking



All timestamps are based on your local time of:

Posted by: stak
Tags: mozilla
Posted on: 2012-03-21 23:42:28

Something that's bitten me more than once during Fennec development is making some changes to browser.js, going through the super-long build cycle, running my code, only to find out there was a syntax error of some sort in my change, which causes Fennec to not start up correctly. When this happens, it is a huge waste of time, and quite frustrating to boot.

Although I've filed bug 715242 to track this issue and fix it properly as part of the build process, I've also thrown together a quick way to do this in my local builds.

First, build the js engine as a standalone binary for your machine. If you have a host build of mozilla-central you might already have a working copy in your <mozilla-central-objdir>/js/src directory. If not, do the following (adapted from the SpiderMonkey build instructions):

mkdir -p ~/tmp/js-build
pushd ~/tmp/js-build
<mozilla-central-srcdir>/js/src/configure
make
popd


This will build a "js" binary that you can run with the -c flag to syntax-check JS files. Put this on your $PATH somewhere. Unfortunately, some of the .js files you'll want to syntax check (such as browser.js) are also preprocessed. Figuring out where in the build to find the post-processed versions of these files was too much work, so I went with the simple and easy approach:

grep -v "^#" $1 > ~/tmp/check-this.js
js -c ~/tmp/check-this.js


If you throw the above into a script and run it with a .js file as an argument, it will exit with zero on success and nonzero on failure, so you can conditionally run the rest of your build. I do this in my build scripts, which you can see in my github repo at https://github.com/staktrace/moz-scripts/ - look at the jscheck and build-android.sh files in particular.

[ Add a new comment ]

 
 
(c) Kartikaya Gupta, 2004-2025. User comments owned by their respective posters. All rights reserved.
You are accessing this website via IPv4. Consider upgrading to IPv6!