Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,39 @@ describe('constant expressions', () => {
compare([constantFoldingPlugin], code, expected);
});

test('does not drop side effects from the leading operands of a sequence', () => {
const code = `
if ((i++, false)) {
a();
}
var b = (delete o.x, true) ? 1 : 2;
var c = (new Foo(), null) || 3;
var d = (--j, 1) + 2;
var h = (tag\`x\`, 0) || 4;
async function e() {
if ((await f, false)) {}
}
function* g() {
if ((yield, false)) {}
}
export {e, g};
`;

compare([constantFoldingPlugin], code, code);
});

test('folds a sequence whose leading operands have no side effects', () => {
const code = `
var a = (0, 'production' !== 'production') ? 1 : 2;
`;

const expected = `
var a = 2;
`;

compare([constantFoldingPlugin], code, expected);
});

test('does not fold arithmetic on destructured object bindings', () => {
const code = `
const O = {e: 80};
Expand Down
23 changes: 13 additions & 10 deletions packages/metro-transform-plugins/src/constant-folding-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@ import type {NodePath, VisitNode, Visitor} from '@babel/traverse';
import typeof Traverse from '@babel/traverse';
import type {
ArrowFunctionExpression as BabelNodeArrowFunctionExpression,
AssignmentExpression as BabelNodeAssignmentExpression,
BinaryExpression as BabelNodeBinaryExpression,
CallExpression as BabelNodeCallExpression,
ConditionalExpression as BabelNodeConditionalExpression,
FunctionDeclaration as BabelNodeFunctionDeclaration,
FunctionExpression as BabelNodeFunctionExpression,
IfStatement as BabelNodeIfStatement,
LogicalExpression as BabelNodeLogicalExpression,
OptionalCallExpression as BabelNodeOptionalCallExpression,
Program as BabelNodeProgram,
UnaryExpression as BabelNodeUnaryExpression,
} from '@babel/types';
Expand All @@ -47,13 +44,7 @@ export default function constantFoldingPlugin(context: {
value: unknown,
} {
const state = {safe: true};
const unsafe = (
path:
| NodePath<BabelNodeAssignmentExpression>
| NodePath<BabelNodeCallExpression>
| NodePath<BabelNodeOptionalCallExpression>,
state: {safe: boolean},
) => {
const unsafe = (path: NodePath<>, state: {safe: boolean}) => {
state.safe = false;
};

Expand All @@ -80,6 +71,18 @@ export default function constantFoldingPlugin(context: {
* resulting in the expression call being skipped.
*/
OptionalCallExpression: unsafe,
// Babel evaluates a sequence to its last operand, so side effects in

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit · Property access in a leading sequence operand is still dropped

The check is a deny-list, so some leading operands that can still have effects are folded away. The closest to real minifier output is a member access: if ((o.x, false)) { a(); } still becomes nothing. That drops a getter call, or the TypeError thrown when o is nullish. (`${o}`, false) (which calls toString) and ("x" in p, false) (which runs a Proxy trap) are dropped too. This isn't new in this PR, and these cases are rarer than n++. If you want the sequence case closed off fully, check that the leading operands are pure, for example path.get('expressions').slice(0, -1).every(p => p.scope.isPure(p.node)) in a SequenceExpression entry. That is an allow-list, so it handles cases nobody thought to list. Otherwise, the comment could say which cases the list deliberately leaves out.

Reproduced: A probe test (packages/metro-transform-plugins/src/__tests__/review-probe-test.js, run in both checkouts) printed the plugin's output. Head and base both reduce if ((o.x, false)) { a(); }, if ((`${o}`, false)) { a(); } and if (("x" in p, false)) { a(); } to empty output. Head keeps (i++, false), !(i++, 1), typeof (i++, 1) and (i++, 1) ?? 2, all of which base folded. · id 0b01608a

// the leading operands (e.g. `(i++, false)`) would otherwise be lost.
AwaitExpression: unsafe,
NewExpression: unsafe,
TaggedTemplateExpression: unsafe,
UpdateExpression: unsafe,
YieldExpression: unsafe,
UnaryExpression(path, state) {
if (path.node.operator === 'delete') {
state.safe = false;
}
},
},
state,
);
Expand Down
Loading