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

Prevent unintended ASI for nested conditionals #3732

Merged
merged 1 commit into from
Aug 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions src/ast/nodes/ConditionalExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ export default class ConditionalExpression extends NodeBase implements Deoptimiz
isCalleeOfRenderedParent: renderedParentType
? isCalleeOfRenderedParent
: (this.parent as CallExpression).callee === this,
preventASI: true,
renderedParentType: renderedParentType || this.parent.type
});
} else {
Expand Down
1 change: 1 addition & 0 deletions src/ast/nodes/LogicalExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ export default class LogicalExpression extends NodeBase implements Deoptimizable
isCalleeOfRenderedParent: renderedParentType
? isCalleeOfRenderedParent
: (this.parent as CallExpression).callee === this,
preventASI,
renderedParentType: renderedParentType || this.parent.type
});
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/YieldExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default class YieldExpression extends NodeBase {

render(code: MagicString, options: RenderOptions) {
if (this.argument) {
this.argument.render(code, options);
this.argument.render(code, options, { preventASI: true });
if (this.argument.start === this.start + 5 /* 'yield'.length */) {
code.prependLeft(this.start + 5, ' ');
}
Expand Down
16 changes: 13 additions & 3 deletions test/function/samples/prevent-tree-shaking-asi/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ function test1() {
return true ?
/* kept */

'expected' :
true ?
'expected' :
'unexpected' :
'unexpected';
}
assert.strictEqual(test1(), 'expected');
Expand All @@ -12,15 +14,17 @@ function test2() {
'unexpected' :
/* kept */

'expected';
false ?
'unexpected' :
'expected';
}
assert.strictEqual(test2(), 'expected');

function test3() {
return true &&
/* kept */

'expected';
'expected' || false;
}
assert.strictEqual(test3(), 'expected');

Expand All @@ -40,3 +44,9 @@ try {
} catch (err) {
assert.strictEqual(err.message, 'expected');
}

function* test5() {
yield false ||
'expected'
}
assert.strictEqual(test5().next().value, 'expected');