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

Empty statements unexpectedly removed. #931

Closed
mbostock opened this issue Sep 12, 2016 · 2 comments
Closed

Empty statements unexpectedly removed. #931

mbostock opened this issue Sep 12, 2016 · 2 comments

Comments

@mbostock
Copy link
Contributor

I expected the following code to be unchanged by Rollup:

var result = 0;
(function(test) {
  if (test === 0);
  else ++result;
})(0);
console.log(result);

However, the resulting output drops the semicolon from the if block, resulting in invalid JavaScript:

var result = 0;
(function(test) {
  if (test === 0)
  else ++result;
})(0);
console.log(result);
  else ++result;
  ^^^^
SyntaxError: Unexpected token else

You can workaround this issue by using an empty block instead of an empty statement:

var result = 0;
(function(test) {
  if (test === 0) {}
  else ++result;
})(0);
console.log(result);

Naturally, the problem also applies to else-if and else:

var result = 0;
(function(test) {
  if (test === 0) {}
  else if (test === 1);
  else;
})(0);
console.log(result);

Which results in:

var result = 0;
(function(test) {
  if (test === 0) {}
  else if (test === 1)
  else
})(0);
console.log(result);
  else
  ^^^^
SyntaxError: Unexpected token else

This example is slightly more complicated than it should be (using the IIFE) because #930 was causing the simpler test case to be removed entirely.

mbostock added a commit to d3/d3-path that referenced this issue Sep 12, 2016
mbostock added a commit to d3/d3-time that referenced this issue Sep 12, 2016
@kzc
Copy link
Contributor

kzc commented Sep 13, 2016

@Rich-Harris I don't know if rollup shares any code with buble other than acorn and magic-string but this Issue is somewhat related to the block-less if/else/loop body problem in buble:

https://gitlab.com/Rich-Harris/buble/merge_requests/80

https://gitlab.com/Rich-Harris/buble/issues/110

eventualbuddha added a commit that referenced this issue Sep 13, 2016
When used as part of another statement, such as an `IfStatement`, they can be semantically meaningful.

Fixes #931
@eventualbuddha
Copy link
Contributor

Fixed in v0.35.10.

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

3 participants