Skip to content
jamespadolsey edited this page Sep 13, 2010 · 16 revisions

jQuery Lint exposes itself under the jQuery namespace as LINT:

jQuery.LINT;

Error/Misuse reporting

You can change the error-reporting level via jQuery.LINT.level:

jQuery.LINT.level = 1; // Only report "serious" things...

See Error reports for more info.

Disable specific reports

You can disable some of the bundled reports (to disable all just set jQuery.LINT.level to 0).

// Don't report when no elements are found:
jQuery.LINT.specific.noElementsFound = false;
// Don't report when I use a selector more than once:
jQuery.LINT.specific.repeatSelector = false;
// Don't report if I try to access jQuery.browser:
jQuery.LINT.specific.browserSniffing = false;
// Don't report if I use invalid filters in my selectors (e.g. :hover):
jQuery.LINT.specific.invalidFilters = false;

jQuery API data

You have direct access to the API data:

jQuery.LINT.api;

This is an object, each property refers to a jQuery function/method.

jQuery.LINT.api.jQuery; // jQuery function
jQuery.LINT.api.css; // css method
jQuery.LINT.api['jQuery.extend']; // extend helper function

“Special” Checks

If you want to add some custom checks to Lint you’ll probably want to do this via special instead of manipulating the api data directly.

As explained already on the Error Reports page, there are four error reporting levels (strictly, three, because zero means everything is off). You can add a custom check under any of these levels:

// Add css method check (err reporting lvl: 3)
jQuery.LINT.special[3].css = [function() {
    if (!condition_to_see_if_it_passes) {
        return "You did something wrong!\nMore info:\nBlah blah blah";
    }
}];

Make sure that you’re not overwriting any pre-existing special checks though. Currently there aren’t any that come with jQuery Lint but still, it’s something to be aware of. Each method, under each error-reporting level, can have its checks stored in an array. Another example:

// Defining multiple checks.
var ajaxMethodChecks = jQuery.LINT.special['jQuery.ajax'] = (jQuery.LINT.special['jQuery.ajax'] || []);
ajaxMethodChecks.push(function(){ /* Do check */ });
ajaxMethodChecks.push(function(){ /* Do check */ });
ajaxMethodChecks.push(function(){ /* Do check */ });

You can also access and manipulate the console mecahnism used by jQuery Lint, via jQuery.LINT.console. E.g.

var warningContainer = $('#warning');
jQuery.LINT.console.warn = function(msg) {
    warningContainer.append(msg + '<br/>');
};

Typically, you won’t need to do this. The checks done by Lint are browser agnostic, and the reporting mechanism works best in Firefox (Firebug). You should only need to define your own console if you’re developing under a different browser. jQuery Lint, by default, will silently fail in browsers that don’t support its default console mechanism. From the source:

_console = {
    warn: glob.console && console.warn ?
        function(){return console.warn.apply(console, arguments);} : function(){},
    group: glob.console && console.group ?
        function(){return console.group.apply(console, arguments);} : function(){},
    groupEnd: glob.console && console.groupEnd ?
        function(){return console.groupEnd.apply(console, arguments);} : function(){},
    groupCollapsed: glob.console && console.groupCollapsed ?
        function(){return console.groupCollapsed.apply(console, arguments);} : function(){},
    log: glob.console && console.log ?
        function(){return console.log.apply(console, arguments);} : function(){}
},

Adding plugins to Lint

If you need to, you can specify argument signatures for your plugins too, or any method you’ve got under the jQuery namespace. E.g.

jQuery.fn.foo = function(num, str, flag) {
    // do stuff with 'num' and 'str', and the optional 'flag'
};
jQuery.LINT.register('foo', [
    {
        arg: [
            {
                type: 'String',
                name: 'str'
            },
            {
                type: 'Number',
                name: 'num'
            },
            {
                type: 'Boolean',
                name: 'flag',
                optional: true
            }
        ]
    }
]);
jQuery.bar = function(fn) {
    // do something with 'fn'
};
jQuery.LINT.register('jQuery.bar', [
    {
        arg: [
            {
                type: 'Function',
                name: 'fn'
            }
        ]
    }
]);

You will now receive warnings when you use jQuery().foo() or jQuery.bar() incorrectly.