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

Summary: 'return' statement used at top level marked as error
Product: javascript Reporter: dusty <dusty>
Component: EditorAssignee: Petr Pisl <ppisl>
Status: NEW ---    
Severity: normal    
Priority: P3    
Version: 8.2   
Hardware: PC   
OS: Linux   
Issue Type: DEFECT Exception Reporter:

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");
return;
===

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");
return;
===

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.