Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxError: 'return' outside of function #86

Closed
scriby opened this issue Feb 14, 2014 · 8 comments
Closed

SyntaxError: 'return' outside of function #86

scriby opened this issue Feb 14, 2014 · 8 comments

Comments

@scriby
Copy link

scriby commented Feb 14, 2014

The acorn parser is throwing an error on a node source file with a return statement at the top level. I'm not certain whether this is technically valid or not, but it works in V8 and I know the uglify parser allows it.

@michaelficarra
Copy link

JavaScript does not allow return outside function bodies. Acorn and Esprima both handle this correctly. You should open an issue with UglifyJS about this. See nodejs/node-v0.x-archive#6254 for why it is allowed in node. tl;dr: It's a side effect of a hack they used to introduce special node "global"s, and they've opted for performance over correctness.

@mishoo
Copy link
Contributor

mishoo commented Feb 14, 2014

@michaelficarra He is mistaken, UglifyJS does not allow it either.

marijnh added a commit that referenced this issue Feb 14, 2014
@marijnh
Copy link
Member

marijnh commented Feb 14, 2014

Still, for Tern, which needs to parse node code (without the wrapping function kludge being visible), I guess I should add an option to allow this. Implemented in attached patch. I'll push something to Tern to enable it there.

@marijnh marijnh closed this as completed Feb 14, 2014
marijnh added a commit to ternjs/tern that referenced this issue Feb 14, 2014
@scriby
Copy link
Author

scriby commented Feb 14, 2014

Thanks for the patch. I was incorrect about Uglify, looks like I had changed that in my fork. I tested Esprima and it doesn't allow it. The TypeScript compiler does allow it.

(I'm not just being pedantic, I do actually have a good use case for returning from the top level of the script :))

@michaelficarra
Copy link

@scriby: I don't think you do. You can always use a top-level IIFE and return from within there.

@scriby
Copy link
Author

scriby commented Feb 14, 2014

I consider it worthwhile to not have to wrap the entire module in a function, which increases indentation level unnecessarily.

@NeamahAlskeini
Copy link

Hi all
When I run aling_dataset_mtcnn.py, I get this error!!
File "align_dataset_mtcnn.py", line 162
return parse.parse_args(argv)
SyntaxError: 'return' outside function

Any one could help to fix this issue?

Regards
Neamah

@fabiosantoscode
Copy link

To future googlers, add the allowReturnOutsideFunction option.

coderaiser added a commit to coderaiser/putout that referenced this issue Jan 24, 2020
mericanAncer127 added a commit to mericanAncer127/ternjs that referenced this issue Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants