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 262414 - 'return' statement used at top level marked as error
Summary: 'return' statement used at top level marked as error
Status: NEW
Alias: None
Product: javascript
Classification: Unclassified
Component: Editor (show other bugs)
Version: 8.2
Hardware: PC Linux
: P3 normal (vote)
Assignee: Petr Pisl
Depends on:
Reported: 2016-06-13 16:04 UTC by dusty
Modified: 2016-06-15 16:22 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Note You need to log in before you can comment on or make changes to this bug.
Description dusty 2016-06-13 16:04:30 UTC
While the JS syntax should not allow to use the "return" statement from top level, the syntax is fine if using node, and in fact there are some libraries doing exactly that.

This in turn trigger the showing of a syntax error in netbeans and hence in all the project.

As a proof, a simple source file like this one runs fine in node and does not even give a warning:
console.log("Hello world");

Please turn this error in a warning, or give the option to disable it so we can edit node projects correctly without errors.
Comment 1 Petr Pisl 2016-06-14 14:16:13 UTC
A few weeks ago I have implemented that if the first line of a file starts with

#! some_path

and the first line ends with node, then the file is treated as nodejs source file.

The implementation is:

if the first line 
    starts with "#!" 
    and ends with "node" 
    and the length of first line is bigger then 12, 
then replace the first line with "(function(){" and at the end of source append "})();"

So currently you can say to NB to treat the file as nodejs file if you place at the firs line something like

#! source of node

There is condition that the length of the first line has to be at least 12 chars, to replace it with the function declaration. It's because we need to keep offsets as they are. 

So your example you can extend:

#! source of node
console.log("Hello world");

and it should work. 

This was a simple way how to treat this issue. If the current implementation doesn't work for you, we need to solve it in different way. But disabling the error is not good way, because the parser will not parse such source. The parsing crash in global scope and there is now way how to recover from this scope. 

I will try to play little bit with sanitizing the code in such error.
Comment 2 dusty 2016-06-14 15:20:15 UTC
I can do that on my sources but I can't change the sources of various libraries I use in my project.

And there are many libraries that use return in JS sources. For example I can point you to "glob", "JSONStream", "browserify", "acorn", "convert-source-map" and many others.

Since many of those libraries are basic building blocks for all other kind of projects, the problem is that our project is marked has having errors, even if it has none :-(
Comment 3 Petr Pisl 2016-06-15 16:22:56 UTC
Now the line with return statement in global space is for parser invisible, but still is visible the error that is reported during the first parsing (without sanitization). So now it's just about the marking. I will try to find a solution for this as well.