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 252017 - Optional 'unused' hint for 'var fn = function FuncName() { ... }'
Summary: Optional 'unused' hint for 'var fn = function FuncName() { ... }'
Status: NEW
Alias: None
Product: javascript
Classification: Unclassified
Component: Editor (show other bugs)
Version: 8.1
Hardware: PC Windows XP
: P3 normal (vote)
Assignee: Petr Pisl
URL:
Keywords:
Depends on: 238685 252019 252028
Blocks:
  Show dependency tree
 
Reported: 2015-04-23 12:10 UTC by NukemBy
Modified: 2015-04-24 08:43 UTC (History)
0 users

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments
Unused-hint-for-functions.png (63.57 KB, image/png)
2015-04-23 12:10 UTC, NukemBy
Details

Note You need to log in before you can comment on or make changes to this bug.
Description NukemBy 2015-04-23 12:10:59 UTC
Created attachment 153323 [details]
Unused-hint-for-functions.png

(See attached screenshot for details)

For better debug experience in Chrome I always give explicit names to ALL functions in my JS library. When function is immediately assigned to a variable or property, its name  ...

  According to 
  https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions
   -> Function constructor vs. function declaration vs. function expression

  "The function name can be used only within the function's body. 
   Attempting to use it outside the function's body results in an 
   error (or undefined if the function name was previously declared
   via a var statement)"

... CANNOT be used outside of function and therefor 'unused' hint is actually unavoidable according to JS grammar. Typical in such case function is supposed to be anonymous, but it leads to weird call stack in debugger (see screenshot).

So I think there should be some special handling for that case. I see two choice:
1. Just do not generate 'unused' hint for that particular case (least amount of changes, noone really cares about 'unused' in that case)
2. Create an individual option/setting for that case so that users like me could disable only that case while keeping 'unused' hint for other cases.

Special cases are:
1. var fn = function FnName();
2. var obj = {fn: function FnName()};
3. do_something(function MyCallback() {});

Below is a longer test script where I expect to see no 'unused' hints.
BTW: currently there is inconsistent behavior - 'unused' hint is not generated for code in 'global' scope, but is generated for 'local function' scope.

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

/* global SomeLib */

var _extend = SomeLib.extend;

var MyLib = new function MyLib() {
    this.Cls = Cls;
    
    var _fn = function _Cls_fn(arg) {
        console.info(arg);
        console.info(function My_Callback_Function() {
        });
        debugger;
    };
    
    function Cls() {
    }

    _extend(Cls.prototype, {
        fn1: function Cls_fn1(arg) {
            _fn(arg);
        },
        fn2: function (arg) {
            this.fn1(arg);
        },
        fn3: function (arg) {
            this.fn2(arg);
        },
        fn4: function Cls_fn4(arg) {
            this.fn3(arg);
        }
    });
};

    var _fn = function _fn(arg) {
        console.info(arg);
        debugger;
    };
    
    function Cls() {
    }

    _extend(Cls.prototype, {
        fn1: function Cls_fn1(arg) {
            _fn(arg);
        },
        fn2: function (arg) {
            this.fn1(arg);
        },
        fn3: function (arg) {
            this.fn2(arg);
        },
        fn4: function Cls_fn4(arg) {
            this.fn3(arg);
        }
    });

    (new Cls()).fn4("arg");
    (new MyLib.Cls()).fn4("arg");
Comment 1 Vladimir Riha 2015-04-23 12:24:38 UTC
Reproducible

Product Version: NetBeans IDE Dev (Build 201504170001)
Java: 1.8.0_40; Java HotSpot(TM) Client VM 25.40-b25
Runtime: Java(TM) SE Runtime Environment 1.8.0_40-b25
System: Linux version 3.13.0-35-generic running on i386; UTF-8; en_US (nb)
Comment 2 Petr Pisl 2015-04-23 15:32:08 UTC
I have played with this. The first problem here is that the parser doesn't mark these anonymous functions as anonymous. I know how to fix it if the anonymous function is used in var definition. I need to find out a solution for the object literal case. 

But this is just start. Then I have to change the way how such functions are handled so far. Like anonymous functions are not displayed in navigator, are not colored in semantic analyzer etc.

I will create separate issue for different cases that has to be handled.