JSLint in Vim through Lynx
10 months ago / 7 Comments
I recently saw a nice blog post by Mike Cantelon on integrating JSLint with Vim, and since I spend most of my days hacking JavaScript in Vim, it was a no-brainer for me to want that integrated-JSLint awesomeness, and right now, dammit.
Of course I already had SpiderMonkey (sudo apt-get install spidermonkey-bin for you Ubuntu/Debian types), and quickly downloaded the needed fulljslint.js file. (I stuck it in /usr/lib.)
However, once I started going through the steps to set it up, a couple of improvements occurred to me.
First of all, when I got to the point of calling JSLint from SpiderMonkey (a la Ian Bicking's post, I saved myself a step of indirection by making the script an actual executable, rather than passing it to the js executable. I just added the usual shebang) line, and the path to the js executable at the top.
Here's my whole runjslint script, that I stuck in /usr/bin:
#!/usr/bin/js load('/usr/lib/fulljslint.js'); var body = arguments[0]; var result = JSLINT(body, { browser: true, forin: true }); if (result) { print('How are you gentlemen. :)'); } else { print('Somebody set up us the bomb! :/'); } print(JSLINT.report());
Next, being a JavaScript guy, the idea of using a Python script to format the JSLint HTML output into plaintext seemed somehow less-than-satisfying.
I actually started to add a quick inline formatting step directly in the js command-line script, but I quickly realized that the JSLint output had a pretty complicated structure, and simple splitting/removing-tags just wasn't going to cut it for me anyhow. I wanted something a little nicer.
Using Lynx (sudo apt-get install lynx) seemed to me an obvious approach -- and as it turns out, at least under Linux it's easy to use Lynx as a filter to convert HTML output to plaintext in a shell command. (I'm not sure how you'd do it under OSX or one of the other BSDish OS's, but I'd assume there's a way to make it work.)
Here's what I added to my .exrc, including the piping through lynx, to create the filter:
cabbr jslint !runjslint "`cat %`" \ \| lynx --force-html /dev/fd/5 -dump 5<&0 \| less
It was really cool to see how easily this could be done, and with these small improvements, it's even nicer.
I find myself JSLinting now pretty regularly throughout the day -- especially once I got JSLint to shut up about one-shot constructor functions for namespace objects. And I guess that's a pretty specific tweak that might be worth another post.
Comments
I have many friends that will agree with you that the idea using a Python script to format the JSLint HTML output into plaintext seemed somehow less-than-satisfying (sorry for the rewriting), however other standards are needed to be tested too
As a rule once you started to do something in a good way you will continue to do it or simply try to keep on. Your situation is of that kind, for sure.
I appreciate this sharing, because particularly I like your style and manner of informational portrayal. Kinda intelligible and persuasive. I owe you in this question,)
This strategy is definitively a good way to pursue. I totally approve your system’s choice, Linux and Lynx are a really good combo especially in framework of JS. Best regards
Thank you for sharing your use of the technique! I’m not an expert in Javascript, but such a website as yours help to progress in informatics. They say that small opportunities are often the beginning of great enterprises.Thank you for giving us such opportunities!
It is always amazing and amusing to read on Javascript’s possibilities. Been a fan of this blog for about a year now and admire your straight forward style. Much of what you have written in the past seems to be proving out. Well, that was my 2 cents LOL… thx for reading
Matthew, now I even can’t imagine that one day you may leave Javascript! I wish more of the Javascript writers were as vocal and knowledgeable as you are. Just keep it up! I look forward to the blog every week, perhaps you should write twice a week for us news-starved JS fans.