Unused local variable warning in self-executing anonymous function

eclipse, javascript

Solution

This appears to be a known bug in Eclipse:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=351470

`FOO_BAR_ANON` is captured when you define function Foo within the anonymous function and reference `FOO_BAR_ANON` within Foo. See Closures documentation.

Here's the example used in the bug report (at the end of the page):

(function() {
    var moveCaretTimer = -1;

    function setMask() {
        (function() {
            function focusEvent() {
                var moveCaret = function() {
                // empty
                };
                clearTimeout(moveCaretTimer);
                moveCaretTimer = setTimeout(moveCaret, 0);
            }
        })();
    }

    setMask.storageKey = storageKey;
})();

`moveCaretTimer` is marked as never read, its occurrences not highlighted.

Problem

Eclipse generates 'The local variable is never read' when a variable is declared inside the self-executing anonymous function, and does not when it is declared in the global scope. Self-executing example: ``` var MODULE = {}; (function (module) { // THIS LINE GENERATES WARNING var FOO_BAR_ANON = {}; function Foo ( ) { if ( this instanceof Foo ) { // THIS IS WHERE VARIABLE IS USED this.fooBar = FOO_BAR_ANON; } else { return new Foo( ); } } module['Foo'] = Foo; })( MODULE ); ``` Global scope example, no warning generated: ``` var MODULE = {}; var FOO_BAR_GLOBAL = {}; function FooGlobal ( ) { if ( this instanceof FooGlobal ) { this.fooBar = FOO_BAR_GLOBAL; } else { return new FooGlobal( ); } } MODULE['FooGlobal'] = FooGlobal; ``` Could you, please, explain, why the warning is generated in the first place, and how to silence it?

Original source