-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Trying to parse JS file with return statement: 'return' outside of function #288
Comments
Please fix this. Returning at the global scope is allowed. |
@stephenmathieson: Not according to ES5 it's not. Some interpreters like node wrap your program in a function, changing its behaviour. See nodejs/node-v0.x-archive#6254 |
<!doctype html>
<script>return;</script>
<script>alert("it works");</script> |
Yes, I said that. It's still not valid JavaScript. |
It could be nice with an option to allow global scope returns. |
I'm trying to uglify a commonJS module in which it is perfectly valid to return from, but I cannot get past this error. |
I'll bite. Who/what told you it was valid? |
CommonJS modules are wrapped in functions. The following example is valid and works fine:
We use return from within modules in many many places. The issue is that uglify assumes that the contents of a JS file are going to be in global scope, but dynamically loaded modules are not in the global scope. |
This is the CommonJS spec: http://wiki.commonjs.org/wiki/Modules/1.1 Where does it say "CommonJS modules are wrapped in functions"? It says "These modules are offered privacy of their top scope", which some implementations (node) choose to implement as a function body. This also gives them a convenient way to introduce the CommonJS module context. But it unintentionally (and incorrectly) adds support for top-level return. Don't take advantage of this feature. |
That has nothing to do with uglify. But fair enough mine was a bad example, so forget commonJS and consider a situation where one is building a massive JS-based application with hundreds of Javascript files in pieces that get concatenated together and wrapped in functions (or not) depending on a complex build script. Why should I not be able to uglify my individual files before putting them together, so that I know what has been uglified and what goes where? Scope needn't be relevant to uglify. "Don't take advantage of this feature." So will uglify also tell me that I am not allowed to use eval or non-strict equals? |
No way. :-) In any case, the idea is that the parser tries to be spec-compliant, and by the JS spec the |
Oh I certainly wasn't suggesting for a moment that ignoring return outside function should be standard behavior, clearly that would be inappropriate for most situations (even more now that I have been enlightened about the CJS spec). This is clearly a specialist case but it seems at least a couple of people want it for one reason or another. Awesomes. |
There you go. Pass |
thanks!! |
I'm trying to parse a JS file that looks a bit like this:
However, I get an error:
'return' outside of function [test.js:1,29]
What I want to do is parse the above JS file and then run my custom TreeTransformer. Can I tell the parser to ignore this error, as I'm already fixing it by wrapping it in a lambda function afterwards (and then doing some more transformations).
The text was updated successfully, but these errors were encountered: