diff --git a/packages/metro-transform-plugins/src/__tests__/constant-folding-plugin-test.js b/packages/metro-transform-plugins/src/__tests__/constant-folding-plugin-test.js index 184ea1cc22..3eb02c813b 100644 --- a/packages/metro-transform-plugins/src/__tests__/constant-folding-plugin-test.js +++ b/packages/metro-transform-plugins/src/__tests__/constant-folding-plugin-test.js @@ -128,6 +128,64 @@ describe('constant expressions', () => { compare([constantFoldingPlugin], code, ''); }); + test('keeps the declaration of a var in a removed branch that is used elsewhere', () => { + const code = ` + if ('production' === 'development') { + var a = 1; + for (var i = 0; i < 3; i++) {} + } + if (true) { + f(); + } else { + var b = 1, c = 2; + } + if (x) { + g(); + } else if (false) { + var d; + } + export function h() { + if (false) { + var k = 1; + } + return k; + } + use(a, i, b, d); + c = 3; + `; + + const expected = ` + var a, i, b, c, d; + { + f(); + } + if (x) { + g(); + } + export function h() { + var k; + return k; + } + use(a, i, b, d); + c = 3; + `; + + compare([constantFoldingPlugin], code, expected); + }); + + test('does not keep a var from a removed branch that is only used in a nested function there', () => { + const code = ` + if (false) { + (function () { + var c; + }); + } + use(c); + `; + + compare([constantFoldingPlugin], code, 'use(c);'); + }); + test('does not fold non-literal void expressions', () => { const code = ` void obj.prop; diff --git a/packages/metro-transform-plugins/src/constant-folding-plugin.js b/packages/metro-transform-plugins/src/constant-folding-plugin.js index d91beef7b6..178a0f0de8 100644 --- a/packages/metro-transform-plugins/src/constant-folding-plugin.js +++ b/packages/metro-transform-plugins/src/constant-folding-plugin.js @@ -153,6 +153,53 @@ export default function constantFoldingPlugin(context: { if (result.confident) { state.stripped = true; + // `var`s are hoisted out of the branch that is dropped. Keep a bare + // declaration for those still used outside it, so they read as + // `undefined` instead of throwing a ReferenceError. + const dropped: NodePath<> = result.value + ? // $FlowFixMe[incompatible-type] + path.get('alternate') + : // $FlowFixMe[incompatible-type] + path.get('consequent'); + const hoisted = new Set(); + const outside = (p: NodePath<>) => !p.isDescendant(dropped); + const collect = (declaration: NodePath<>) => { + const {node} = declaration; + if (node?.type !== 'VariableDeclaration' || node.kind !== 'var') { + return; + } + for (const name of Object.keys( + declaration.getBindingIdentifiers(), + )) { + const binding = declaration.scope.getBinding(name); + // Declared elsewhere too (as a param or function, say). + if (binding?.kind !== 'var') { + continue; + } + const writes = [binding.path, ...binding.constantViolations]; + if ( + !writes.some(p => p.isVariableDeclarator() && outside(p)) && + [...binding.referencePaths, ...writes].some(outside) + ) { + hoisted.add(name); + } + } + }; + if (dropped.node != null) { + collect(dropped); + dropped.traverse({ + Function(p: NodePath<>) { + p.skip(); + }, + VariableDeclaration: collect, + }); + } + const scope = + path.scope.getFunctionParent() ?? path.scope.getProgramParent(); + for (const name of hoisted) { + scope.push({id: t.identifier(name)}); + } + if (result.value || node.alternate) { const valueIsTruthy = Boolean(result.value); // $FlowFixMe[incompatible-type]