Skip to content

Commit

Permalink
Add option to allow return outside of functions.
Browse files Browse the repository at this point in the history
Close #288
  • Loading branch information
mishoo committed Oct 20, 2014
1 parent a64bdda commit f36a1ea
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
9 changes: 6 additions & 3 deletions bin/uglifyjs
Expand Up @@ -64,6 +64,7 @@ You need to pass an argument to this option to specify the name that your module
.describe("v", "Verbose")
.describe("V", "Print version number and exit.")
.describe("noerr", "Don't throw an error for unknown options in -c, -b or -m.")
.describe("bare-returns", "Allow return outside of functions. Useful when minifying CommonJS modules.")

.alias("p", "prefix")
.alias("o", "output")
Expand Down Expand Up @@ -100,6 +101,7 @@ You need to pass an argument to this option to specify the name that your module
.boolean("lint")
.boolean("V")
.boolean("noerr")
.boolean("bare-returns")

.wrap(80)

Expand Down Expand Up @@ -275,9 +277,10 @@ async.eachLimit(files, 1, function (file, cb) {
else {
try {
TOPLEVEL = UglifyJS.parse(code, {
filename : file,
toplevel : TOPLEVEL,
expression : ARGS.expr,
filename : file,
toplevel : TOPLEVEL,
expression : ARGS.expr,
bare_returns : ARGS.bare_returns,
});
} catch(ex) {
if (ex instanceof UglifyJS.JS_Parse_Error) {
Expand Down
3 changes: 2 additions & 1 deletion lib/parse.js
Expand Up @@ -609,6 +609,7 @@ function parse($TEXT, options) {
toplevel : null,
expression : false,
html5_comments : true,
bare_returns : false,
});

var S = {
Expand Down Expand Up @@ -788,7 +789,7 @@ function parse($TEXT, options) {
return if_();

case "return":
if (S.in_function == 0)
if (S.in_function == 0 && !options.bare_returns)
croak("'return' outside of function");
return new AST_Return({
value: ( is("punc", ";")
Expand Down

0 comments on commit f36a1ea

Please sign in to comment.