Unverified Commit a5674d0d authored by Sam Poder's avatar Sam Poder Committed by GitHub

[turbopack] Bail CJS tree-shaking on `var x = module.exports = {}` (#97130)

parent bfc922eb
...@@ -818,6 +818,21 @@ pub fn as_parent_path_with_in<'a>( ...@@ -818,6 +818,21 @@ pub fn as_parent_path_with_in<'a>(
path.into_boxed_slice() path.into_boxed_slice()
} }
/// Whether the node at `ast_path` is a whole statement, so its value goes nowhere:
/// true for `module.exports = {};` and `(module.exports = {});`, false for
/// `var x = module.exports = {}` or `f(module.exports = {})`.
fn is_expression_statement(ast_path: &AstNodePath<AstParentNodeRef<'_>>) -> bool {
for node_ref in ast_path.iter().rev() {
match node_ref {
// The `Expr` wrapper of the node itself, and of a parenthesized one.
AstParentNodeRef::Expr(..) | AstParentNodeRef::ParenExpr(_, ParenExprField::Expr) => {}
AstParentNodeRef::ExprStmt(_, ExprStmtField::Expr) => return true,
_ => return false,
}
}
false
}
/// Returns the [`MemberExpr`] where the current node is the object: /// Returns the [`MemberExpr`] where the current node is the object:
/// `<node>.<prop>` or `<node>[<expr>]`. /// `<node>.<prop>` or `<node>[<expr>]`.
fn member_access_parent<'r>( fn member_access_parent<'r>(
...@@ -1452,6 +1467,11 @@ impl<'a> Analyzer<'a, '_> { ...@@ -1452,6 +1467,11 @@ impl<'a> Analyzer<'a, '_> {
self.taint_cjs_exports(); self.taint_cjs_exports();
return; return;
} }
// Catches `var Self = Object.defineProperty(exports, …)`: the call returns exports.
if !is_expression_statement(ast_path) {
self.taint_cjs_exports();
return;
}
let dead = self.cjs_exports_object_replaced() let dead = self.cjs_exports_object_replaced()
&& !n.args.first().is_some_and(|target| { && !n.args.first().is_some_and(|target| {
is_module_exports_chain(&target.expr, self.eval_context.unresolved_mark) is_module_exports_chain(&target.expr, self.eval_context.unresolved_mark)
...@@ -1487,13 +1507,13 @@ impl<'a> Analyzer<'a, '_> { ...@@ -1487,13 +1507,13 @@ impl<'a> Analyzer<'a, '_> {
else { else {
return; return;
}; };
// Only a statement-position assignment definitely runs. // Catches `var Self = module.exports = { … }`: the alias reads exports back.
if matches!( if !is_expression_statement(ast_path) {
ast_path.len().checked_sub(2).and_then(|i| ast_path.get(i)), self.taint_cjs_exports();
Some(AstParentNodeRef::ExprStmt(_, ExprStmtField::Expr)) return;
) {
self.replace_cjs_exports_object();
} }
// A statement-position assignment definitely runs.
self.replace_cjs_exports_object();
let mut names = Vec::new(); let mut names = Vec::new();
for prop in &obj.props { for prop in &obj.props {
// A spread makes the export set unknowable. // A spread makes the export set unknowable.
......
var Self = Object.defineProperty(exports, 'helper', {
value: function () {
return 'abc'
},
})
exports.abc = function () {
return Self.helper()
}
var Self
Self = module.exports = {
helper: function () {
return 'abc'
},
abc: function () {
return Self.helper()
},
}
var Self = (module.exports = {
helper: function () {
return 'abc'
},
abc: function () {
return Self.helper()
},
})
...@@ -54,3 +54,15 @@ it('should be able to use AMD to define exports', () => { ...@@ -54,3 +54,15 @@ it('should be able to use AMD to define exports', () => {
expect(require('./using-amd').abc).toBe('abc') expect(require('./using-amd').abc).toBe('abc')
expect(require('./using-amd').def).toBe('def') expect(require('./using-amd').def).toBe('def')
}) })
it('should bailout when a variable aliases a replaced exports object', () => {
expect(require('./alias-exports-object').abc()).toBe('abc')
})
it('should bailout when an assignment chain aliases a replaced exports object', () => {
expect(require('./alias-exports-object-chain').abc()).toBe('abc')
})
it('should bailout when a variable aliases the result of defineProperty', () => {
expect(require('./alias-exports-define').abc()).toBe('abc')
})
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