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

Avoid invalid code when "else" branch is simplified #3421

Merged
merged 3 commits into from Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 /* f */) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm always up for a good puzzle... What's the significance of f? Or is it e?

$ node -p 'String.fromCharCode(101)'
e
$ node -p 'String.fromCharCode(102)'
f

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The beauty of comments. I definitely did NOT copy this code from for..of loop 😉

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);