Using jslint with javascript getters and setters

29 Mar 2010

If you the most excellent jslint on javascript source containing getters and setters you find jslint comes to a grinding halt. jslint doesn't like 'em, and the author isn't keen on adding that support. (I can appreciate that. He has a clear use-case for jslint, and too many projects are goal-less.)

That said, if you need this feature you can hack the parser, or do something else. For the short term, I choose the later. deaccessor.py is small python program (which could be ported to any language -- it's MIT licensed so go nuts), that converts getters and setters to a more traditional format that jslint and other tools can parse. It's not perfect, but it also checks that getters don't have an argument, and that setters have only one argument.

So... this:

var x = {
    get foo() {
        return this._foo;
    },
    set foo(x) {
        this._foo = x;
    }
};

... becomes this which jslint will happily parse.

var x = {
    get_foo: function () {
        return this._foo;
    },
    set_foo: function (x) {
        this._foo = x;
    }
};

You can season to taste. See the source for details on how to use.

Happy linting!