Self Invoking Functions, Resharper 6.1 and JS Lint - Possible to be Valid For Both?

367 views Asked by At

Say I have a bit of JavaScript that looks like this:

MyCompany.MyProduct = {};

(function () {

    "use strict";

    MyCompany.MyProduct.doSomethingAmazing = function () {
    };
}());

which I believe is an OK approach to take...

It passes Mr Crockford's Javascript lint so I figured I was onto a good thing but then I noticed that Resharper (6.1 running in Visual Studio 2010) told me

Unexpected Expression

on the last line (i.e. this bit: }());).

Now, I know there is a controversial bracket placement there and many prefer the form })(); but that does not appease Resharper and it annoys JS Lint so I let that drop.

Then I went for:

MyCompany.MyProduct = {};

!function () {

    "use strict";

    MyCompany.MyProduct.doSomethingAmazing = function () {
    };
}();

which I think is equivalent (according to this anyway) and Resharper seemed happy but now JS Lint is freaking out, telling me:

Wrap an immediate function invocation in parentheses to assist the reader in understanding that the expression is the result of a function, and not the function itself.

So, before I just select one style (probably the one that annoys both the squabbling parties because if they can't play together...) I was wondering, since Javascript has so many neat syntactical alleyways if you know where to shine the torch:

Is there any syntax that I can use that will keep both parties happy?

1

There are 1 answers

0
jkdev On

You cannot satisfy two opposites at once. But there are workarounds...

JSLint requires you to move the invocation into the parens that contain the function. As an alternative to JSLint you can try JSHint or ESLint, with similar but more flexible rules. ESLint in particular allows you to set the option wrap-iife to either "inside" or "outside." Here's an example.

Or, you could try to change the rules used by Resharper. I don't know what options Resharper gives you for that, since I haven't used it, but it might be worth looking up.