Showing posts with label JSLint. Show all posts
Showing posts with label JSLint. Show all posts

Monday, February 15, 2010

jQuery Lint

jQuery Lintis a simple script you can download and use with jQuery. It works over the top of jQuery and diligently reports errors and any incorrect usage of jQuery. It will also, to some extent, offer guidance on best practices and performance concerns.

Unlike JSLint,jQuery Lint is a runtime reporter. To use it, you need to include it, after jQuery, in your document:

jQuery lint’s main objective is to notify you of incorrect usages of jQuery’s API. So, if you pass incorrect arguments to any method then jQuery Lint will let you know. It compares your arguments to the argument signatures in jQuery’s API. It reports via Firebug, although you can quite easily plug-in your own console mechanism.

It has four different error-reporting levels (accessible via jQuery.LINT.level), zero reports nothing, three will report everything, including small things like using css().css().css() instead of css({...}). It’s quite configurable too. You can add your own checks. E.g.

jQuery.LINT.special[1].jQuery = jQuery.LINT.special[1].jQuery || [];

// Add check on error-reporting level one.
// Check jQuery method.
jQuery.LINT.special[1].jQuery.push(function(selector, context) {

if (selector === '*') {
return "Don't use the universal selector!";
}

});

jQuery Lint tries to help you in determining where the problem occurred in your code. It’s not much help to you if it just says, “Err, you called css() incorrectly!”. If it occurred as a result of an event then Lint will say so, and if you’re using a browser that provides a stack-trace as part of its Error object (like Firefox) then Lint will also provide you with the file-name and line number. E.g.

jquery Lint - Reporting line number and file name where problem occured.

http://james.padolsey.com/javascript/jquery-lint/

Sunday, November 29, 2009

Check your scripts with JSLint on Rails

Here's how you use it:

  1. Make sure you have Java installed (5.0 or later) – it's required to run Rhino.
  2. Install the plugin:
    ./script/plugin install git://github.com/psionides/jslint_on_rails.git
  3. Run the rake task:
    rake jslint

Voila :) That's all you need to check your JS code. You will get a result like this (if everything goes well):

Running JSLint:

checking public/javascripts/Event.js... OK
checking public/javascripts/Map.js... OK
checking public/javascripts/Marker.js... OK
checking public/javascripts/Reports.js... OK

No JS errors found.

If you've messed up something, you will get such results instead:

Running JSLint:

checking public/javascripts/Event.js... 2 errors:

Lint at line 24 character 15: Use '===' to compare with 'null'.
if (a == null && b == null) {

Lint at line 72 character 6: Extra comma.
},

checking public/javascripts/Marker.js... 1 error:

Lint at line 275 character 27: Missing radix parameter.
var x = parseInt(mapX);

Found 3 errors.
rake aborted!
JSLint test failed.

http://psionides.jogger.pl/2009/11/23/check-your-scripts-with-jslint-on-rails/