Unverified Commit 10cdcda5 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

fix(turbopack): evaluate pure constructor callees (#100588)

## Summary

Preserve effects from evaluating a constructor callee even when
construction is PURE-annotated. For example, `/*#__PURE__*/ new
(getConstructor())()` must account for calling `getConstructor()`.

Visit the callee before arguments, matching pure call handling. Add
analyzer and execution regressions while preserving upstream PURE
callback handling from #100577.

Independent of the callback stack (#100586 / #100587); rebase whichever
lands later if their `Expr::New` edits overlap.

## Verification

- Full `turbopack-ecmascript` library: 609 tests passed.
- Constructor-callee execution regression passed.
- Rustfmt and diff checks passed. Full monorepo suite not run locally.

<!-- NEXT_JS_LLM -->

<!-- fleet 87a54898-52c9-4506-ae11-7f75f7e661f6 -->
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
parent e828f1e5
......@@ -1049,7 +1049,8 @@ impl<'a> Visit for SideEffectVisitor<'a> {
// Check for /*#__PURE__*/ annotation or known pure constructor
let is_pure_annotated = self.is_pure_annotated(new.span);
if is_pure_annotated || self.is_known_pure_constructor(&new.callee) {
// Pure constructor, but still need to check arguments
// The annotation only covers invocation, not callee or argument evaluation.
new.callee.visit_with(self);
self.with_will_invoke_fn_exprs(!is_pure_annotated, |this| {
new.args.visit_children_with(this);
});
......@@ -1460,6 +1461,47 @@ mod tests {
};
}
mod constructor_callee_tests {
use super::*;
side_effects!(test_call_callee, "/*#__PURE__*/ new (getConstructor())();");
side_effects!(
test_nested_call_callee,
"/*#__PURE__*/ new (getContainer().Constructor)();"
);
side_effects!(
test_computed_callee,
"/*#__PURE__*/ new (constructors[getKey()])();"
);
side_effects!(
test_conditional_callee,
"/*#__PURE__*/ new (flag ? getConstructor() : Constructor)();"
);
side_effects!(
test_callee_and_arguments,
"/*#__PURE__*/ new (getConstructor())(effect());"
);
side_effects!(test_arguments, "/*#__PURE__*/ new Constructor(effect());");
no_side_effects!(test_identifier_callee, "/*#__PURE__*/ new Constructor();");
no_side_effects!(
test_member_callee,
"/*#__PURE__*/ new constructors.Constructor();"
);
no_side_effects!(
test_computed_pure_callee,
"/*#__PURE__*/ new constructors[key]();"
);
no_side_effects!(
test_uninvoked_constructor_body,
"/*#__PURE__*/ new (function () { effect(); })();"
);
no_side_effects!(test_known_constructor, "new Map();");
no_side_effects!(
test_pure_callee_call,
"/*#__PURE__*/ new (/*#__PURE__*/ getConstructor())();"
);
}
mod basic_tests {
use super::*;
......
function getConstructor() {
globalThis.constructorCallee = 'evaluated'
return class {}
}
/*#__PURE__*/ new (getConstructor())()
import './callee.js'
it('retains effects from evaluating a PURE constructor callee', () => {
expect(globalThis.constructorCallee).toBe('evaluated')
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment