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

Invalid JS: Compiler emits invalid JS when throw followed by if or for #3709

Closed
epidemian opened this issue Nov 5, 2014 · 3 comments
Closed
Labels

Comments

@epidemian
Copy link
Contributor

This seemingly valid CS code:

throw if pedantic 
  new FatalError
else
  new ResumableError

Compiles to invalid JS:

throw if (pedantic) {
  new FatalError;
} else {
  new ResumableError;
};

I say seemingly valid because the same thing works for a function call:

raise = (e) -> throw e
raise if pedantic 
  new FatalError
else
  new ResumableError
var raise;

raise = function(e) {
  throw e;
};

raise(pedantic ? new FatalError : new ResumableError);
@bwin
Copy link
Contributor

bwin commented Nov 10, 2014

It also works if you add parentheses:

throw (if pedantic 
  new FatalError
else
  new ResumableError)

compiles to:

throw (pedantic ? new FatalError : new ResumableError);

@GeoffreyBooth
Copy link
Collaborator

GeoffreyBooth commented May 6, 2017

Probably related, from #3357: The following compiles to invalid JS:

throw for v in list then v

Basically we shouldn’t allow if or for (or all expressions?) after throw.

@GeoffreyBooth GeoffreyBooth changed the title Compiler emits invalid JS code when nesting IF inside THROW Invalid JS: Compiler emits invalid JS code when nesting IF inside THROW May 6, 2017
@GeoffreyBooth GeoffreyBooth changed the title Invalid JS: Compiler emits invalid JS code when nesting IF inside THROW Invalid JS: Compiler emits invalid JS code when for throw followed by if or for May 6, 2017
@GeoffreyBooth GeoffreyBooth changed the title Invalid JS: Compiler emits invalid JS code when for throw followed by if or for Invalid JS: Compiler emits invalid JS when throw followed by if or for May 6, 2017
GeoffreyBooth added a commit to GeoffreyBooth/coffeescript that referenced this issue Aug 25, 2017
GeoffreyBooth added a commit that referenced this issue Aug 25, 2017
)

* Fix #3709: throwing an if, for, switch or while should throw the returned value of the statement/loop

* Fix #3789: don’t throw a throw (unless it’s in a closure)

* LEVEL_LIST works better than a list of node types
@GeoffreyBooth
Copy link
Collaborator

Fixed by #4664.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants