Skip to content

Commit

Permalink
Avoid invalid code when "else" branch is simplified (#3421)
Browse files Browse the repository at this point in the history
* Make sure there is always whitespace between "else" and the subsequent statement to avoid invalid code when the alternate branch is simplified.

* Fix comment
  • Loading branch information
lukastaegert committed Mar 6, 2020
1 parent 1e6284f commit 85c54ee
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/ast/nodes/IfStatement.ts
Expand Up @@ -82,6 +82,9 @@ export default class IfStatement extends StatementBase implements DeoptimizableE
}
if (this.alternate !== null) {
if (this.alternate.included) {
if (code.original.charCodeAt(this.alternate.start - 1) === 101 /* e */) {
code.prependLeft(this.alternate.start, ' ');
}
this.alternate.render(code, options);
} else {
code.remove(this.consequent.end, this.alternate.end);
Expand Down
11 changes: 11 additions & 0 deletions test/function/samples/if-statement-insert-whitespace/_config.js
@@ -0,0 +1,11 @@
module.exports = {
description: 'inserts necessary white-space when simplifying if-statements (#3419)',
options: {
external: 'external'
},
context: {
require(required) {
return false;
}
}
};
8 changes: 8 additions & 0 deletions test/function/samples/if-statement-insert-whitespace/main.js
@@ -0,0 +1,8 @@
let works = false;
const makeItWork = () => works = true;

import value from 'external';

if (value) {} else"production"!=="local"?makeItWork():void 0;

assert.ok(works);

0 comments on commit 85c54ee

Please sign in to comment.