Skip to content

Incorrect if JavaScript depending on context #1098

@LeXofLeviafan

Description

@LeXofLeviafan

When compiling the code x = (if foo! then bar(that) or baz(that)) (that is, relying on if-let to locally bind result of the first subexpression), the resulting JavaScript looks like this:

x = (that = foo()) ? bar(that) || baz(that) : void 8;

Which works pretty straightforwardly, as one would expect. If, however, you pass the same exact expression to, say, while loop, the output will have different structure:

while (if foo! then bar(that) or baz(that))
  console.log 42
while ((that = foo()) && bar(that) || baz(that)) {
  console.log(42);
}

Which deviates from expected behaviour, because now baz will be executed even when that is falsey (due to JavaScript operator precedence).
It is possible to reproduce structure from the first example in second case, but only by providing an explicit else branch:

while (if foo! then bar(that) or baz(that) else void)
  console.log 42
while ((that = foo()) ? bar(that) || baz(that) : void 8) {
  console.log(42);
}

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions