未验证 提交 438a5075 编写于 作者: L Lukas Taegert-Atkinson 提交者: GitHub

Wrap leading function expression iifes in conditionals (#4099)

上级 ce2592df
import MagicString from 'magic-string';
import { BLANK } from '../../utils/blank';
import { NodeRenderOptions, RenderOptions } from '../../utils/renderHelpers';
import { DeoptimizableEntity } from '../DeoptimizableEntity';
import { HasEffectsContext } from '../ExecutionContext';
import {
......@@ -82,4 +85,15 @@ export default class BinaryExpression extends NodeBase implements DeoptimizableE
hasEffectsWhenAccessedAtPath(path: ObjectPath): boolean {
return path.length > 1;
}
render(
code: MagicString,
options: RenderOptions,
{ renderedParentType, renderedSurroundingElement }: NodeRenderOptions = BLANK
): void {
this.left.render(code, options, {
renderedSurroundingElement: renderedParentType || renderedSurroundingElement
});
this.right.render(code, options);
}
}
......@@ -180,7 +180,7 @@ export default class ConditionalExpression extends NodeBase implements Deoptimiz
render(
code: MagicString,
options: RenderOptions,
{ renderedParentType, isCalleeOfRenderedParent, preventASI }: NodeRenderOptions = BLANK
{ isCalleeOfRenderedParent, renderedParentType, preventASI }: NodeRenderOptions = BLANK
): void {
const usedBranch = this.getUsedBranch();
if (!this.test.included) {
......@@ -207,7 +207,11 @@ export default class ConditionalExpression extends NodeBase implements Deoptimiz
renderedParentType: renderedParentType || this.parent.type
});
} else {
super.render(code, options);
this.test.render(code, options, {
renderedSurroundingElement: renderedParentType
});
this.consequent.render(code, options);
this.alternate.render(code, options);
}
}
......
......@@ -166,7 +166,12 @@ export default class LogicalExpression extends NodeBase implements Deoptimizable
render(
code: MagicString,
options: RenderOptions,
{ renderedParentType, isCalleeOfRenderedParent, preventASI }: NodeRenderOptions = BLANK
{
isCalleeOfRenderedParent,
preventASI,
renderedParentType,
renderedSurroundingElement
}: NodeRenderOptions = BLANK
): void {
if (!this.left.included || !this.right.included) {
const operatorPos = findFirstOccurrenceOutsideComment(
......@@ -192,7 +197,10 @@ export default class LogicalExpression extends NodeBase implements Deoptimizable
renderedParentType: renderedParentType || this.parent.type
});
} else {
this.left.render(code, options, { preventASI });
this.left.render(code, options, {
preventASI,
renderedSurroundingElement: renderedParentType || renderedSurroundingElement
});
this.right.render(code, options);
}
}
......
module.exports = {
description: 'correctly uses parentheses when a simplification would put a function IIFE first'
};
let a = 0;
const _1 = function (){}() ? a = 1 : a = 2;
assert.strictEqual(a, 2);
let b = 0;
const _2 = function (){}() || (b = 1);
assert.strictEqual(b, 1);
let c = 0;
const _3 = function (){}() + (c = 1);
assert.strictEqual(c, 1);
let d = 0;
const _4 = function (){}() || function (){}() || (d = 1);
assert.strictEqual(d, 1);
let e = 0;
const _5 = function (){}() + function (){}() + (e = 1);
assert.strictEqual(e, 1);
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册