diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/this-reference-wrapped/a.js b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/this-reference-wrapped/a.js index fa0aaf7fadf..9b8ffa42187 100644 --- a/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/this-reference-wrapped/a.js +++ b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/this-reference-wrapped/a.js @@ -1,3 +1,3 @@ const b = require('./b'); -output = b.foo + b.bar; +output = [b.foo, b.bar, b.foobar]; diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/this-reference-wrapped/b.js b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/this-reference-wrapped/b.js index 9740b8e6158..00bb4cf6e39 100644 --- a/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/this-reference-wrapped/b.js +++ b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/this-reference-wrapped/b.js @@ -1,2 +1,24 @@ -this.foo = 2; -eval('this.bar = 4'); +this.foo = 5; + +class X { + constructor() { + this.bar = 2; + } + x() { + this.bar = 2; + } +} + +function f() { + this.bar = 2; +} + +if (Date.now() > 0) { + this.foo += 1; +} + +new f(); +new X().x(); + + +eval('this.foobar = 4'); diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/this-reference/a.js b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/this-reference/a.js index afb22d072f4..3153877673e 100644 --- a/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/this-reference/a.js +++ b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/this-reference/a.js @@ -1,3 +1,3 @@ const b = require('./b'); -output = b.foo; +output = [b.foo, b.bar]; diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/this-reference/b.js b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/this-reference/b.js index 559a5bfbffb..f3d374bf715 100644 --- a/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/this-reference/b.js +++ b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/this-reference/b.js @@ -1 +1,21 @@ -this.foo = 2; +this.foo = 5; + +class X { + constructor() { + this.bar = 2; + } + x() { + this.bar = 2; + } +} + +function f() { + this.bar = 2; +} + +if (Date.now() > 0) { + this.foo += 1; +} + +new f(); +new X().x(); diff --git a/packages/core/integration-tests/test/scope-hoisting.js b/packages/core/integration-tests/test/scope-hoisting.js index 985d5b672c7..89ee9af10d8 100644 --- a/packages/core/integration-tests/test/scope-hoisting.js +++ b/packages/core/integration-tests/test/scope-hoisting.js @@ -3833,8 +3833,8 @@ describe('scope hoisting', function () { ), ); - let output = await run(b); - assert.strictEqual(output, 2); + let output = await run(b, {output: null}, {strict: true}); + assert.deepEqual(output, [6, undefined]); }); it('supports assigning to this as exports object in wrapped module', async function () { @@ -3845,8 +3845,8 @@ describe('scope hoisting', function () { ), ); - let output = await run(b); - assert.strictEqual(output, 6); + let output = await run(b, {output: null}, {strict: true}); + assert.deepEqual(output, [6, undefined, 4]); }); it('supports using exports self reference', async function () { diff --git a/packages/transformers/js/core/src/hoist.rs b/packages/transformers/js/core/src/hoist.rs index 054683dd41d..a64d485e62d 100644 --- a/packages/transformers/js/core/src/hoist.rs +++ b/packages/transformers/js/core/src/hoist.rs @@ -884,6 +884,7 @@ impl<'a> Fold for Hoist<'a> { match_member_expr(member, vec!["module", "exports"], &self.collect.decls) } Expr::Ident(ident) => &*ident.sym == "exports" && !self.collect.decls.contains(&id!(ident)), + Expr::This(_) if !self.in_function_scope => true, _ => false, };