This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 242421 - False positive 'unused' hint for vars and func args in named function expression
Summary: False positive 'unused' hint for vars and func args in named function expression
Status: VERIFIED FIXED
Alias: None
Product: javascript
Classification: Unclassified
Component: Editor (show other bugs)
Version: 8.0
Hardware: PC Windows 7 x64
: P3 normal (vote)
Assignee: Petr Pisl
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-02-27 15:37 UTC by NukemBy
Modified: 2014-07-07 11:58 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
Screenshot of 'false positive' 'Unused' hints (4.19 KB, image/png)
2014-02-27 15:37 UTC, NukemBy
Details
Missing_occurences_for_rename (17.55 KB, image/png)
2014-02-27 16:05 UTC, NukemBy
Details

Note You need to log in before you can comment on or make changes to this bug.
Description NukemBy 2014-02-27 15:37:15 UTC
Created attachment 145637 [details]
Screenshot of 'false positive' 'Unused' hints

I'm getting 'false positive' 'Unused' hint for

- function arguments: path, ref, pfx

... all of them are forwarded into external function - therefore should be treated as used

- single local variable 

... is in several places

var myF = function MyLib_myF(path, ref, pfx, options) {
   var v = expand(path, ref, pfx, options);

   if( options && options.option1 && (v === path) )
       return path;

   v = page.root + '~/' + v;

   return v;
};

More details in attachment.
Comment 1 NukemBy 2014-02-27 16:05:05 UTC
Created attachment 145639 [details]
Missing_occurences_for_rename
Comment 2 NukemBy 2014-02-27 16:07:52 UTC
Was going to file another bug "JavaScript edit misses some occurrences during rename of func args", but now I see that root cause is the same - logical model of the JS file is built incorrectly and names are not considered to be related.

See same piece of code and another screenshot for details.
Comment 3 Vladimir Riha 2014-02-28 08:28:14 UTC
Reproducible, workaround is to remove the "MyLib_myF" from the 1st line

var myF = function (path, ref, pfx, options) {...


Your code works but just out of curiosity, why is the function named? It cannot be invoked by MyLib_myF() right? For instance following:

            var myF = function MyLib_myF(path, ref, pfx, options) {
                alert(1)
            };

            console.log(typeof MyLib_myF); // undefined
            MyLib_myF(); // throws ReferenceError



Can't you remove the the name as suggested above? It would fix all the problems here.



Product Version: NetBeans IDE 8.0 RC1 (Build 201402242200)
Java: 1.8.0; Java HotSpot(TM) Client VM 25.0-b69
Runtime: Java(TM) SE Runtime Environment 1.8.0-b129
System: Linux version 3.2.0-48-generic-pae running on i386; UTF-8; en_US (nb)
Comment 4 Vladimir Riha 2014-02-28 08:35:23 UTC
(In reply to Vladimir Riha from comment #3)
> Your code works but just out of curiosity, why is the function named? 

Never mind, I realized the difference right after submitting the comment :)
Comment 5 NukemBy 2014-02-28 09:04:54 UTC
Just to be in sync - the function name there is only intended for debugging purposes - Chrome (WebKit) displays it in stack trace. With no function name explicitly provided it tries to 'generate' function name using the name of variable the function is assigned to, but this is typically far from being accurate (especially when function is assigned to multiple variables).

Typically that name will be thrown away by JS minifiers and yes, cannot be used directly.

And, just for information, this case is described in details here 
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope -> "Function constructor vs. function declaration vs. function expression" -> case #4.
Comment 6 Petr Pisl 2014-03-28 09:12:01 UTC
The problem is in the creating function in model. This example code creates two functions in model (MyLib_myF and myF) which is wrong. Instead of this myF should be reference to MyLib_myF.
Comment 7 Petr Pisl 2014-03-28 14:17:39 UTC
Fixed in web-main. Thanks for the reporting and the test case.
Comment 8 Quality Engineering 2014-03-29 07:21:27 UTC
Integrated into 'main-silver', will be available in build *201403290001* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)

Changeset: http://hg.netbeans.org/main-silver/rev/d09160df9e79
User: Petr Pisl <ppisl@netbeans.org>
Log: #242421 - False positive 'unused' hint for vars and func args in named function expression
Comment 9 Vladimir Riha 2014-04-04 07:45:17 UTC
Verified in Dev

Product Version: NetBeans IDE Dev (Build 201404040001)
Java: 1.8.0; Java HotSpot(TM) Client VM 25.0-b70
Runtime: Java(TM) SE Runtime Environment 1.8.0-b132
System: Linux version 3.2.0-48-generic-pae running on i386; UTF-8; en_US (nb)
Comment 10 Petr Pisl 2014-04-15 07:44:01 UTC
Transplanted to the releases/release80 for patch 1.
Comment 11 Quality Engineering 2014-04-24 02:11:52 UTC
Integrated into 'releases/release80', will be available in build *201404240045* or newer. Wait for official and publicly available build.

Changeset: http://hg.netbeans.org/releases/rev/e73614e61133
User: Petr Pisl <ppisl@netbeans.org>
Log: #242421 - False positive 'unused' hint for vars and func args in named function expression
Comment 12 Vladimir Riha 2014-04-24 11:48:15 UTC
verified in patch1

Product Version: NetBeans IDE 8.0 (Build 201403101706)
Updates: NetBeans IDE is updated to version NetBeans 8.0 Patch 1
Java: 1.8.0_05; Java HotSpot(TM) Client VM 25.5-b02
Runtime: Java(TM) SE Runtime Environment 1.8.0_05-b13
System: Linux version 3.2.0-60-generic-pae running on i386; UTF-8; en_US (nb)
Comment 13 NukemBy 2014-07-07 09:28:10 UTC
It seems like not all cases of this bug are fixed, for example in this excerpt I'm getting false positive 'unused' hint for var 'r' and var 'i' 
(Product Version: NetBeans IDE Dev (Build 201407010002)). After removing the 'MyLib_myMethod' function name hint disappears just like in more simple case.

- - - - - - - - - - - - - - - - - - - - - - - - 

var myLib = new MyLib();

function MyLib() {
    /**
     * This is dummy JsDoc
     */
    this.myMethod = function MyLib_myMethod(array) {
        var r = "";
        for( var i = 0; i < array.length; i++ ) {
            r = r + array[i];
        }
        return r;
    };
}

myLib.myMethod([1, 2, 3, 4]);
Comment 14 Vladimir Riha 2014-07-07 11:05:15 UTC
The original issue is fixed, please file a new one instead of reopening already verified issue. Thank you
Comment 15 Petr Pisl 2014-07-07 11:58:08 UTC
I have created new issue #245445