Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assigning to this in CommonJS #8737

Merged
merged 2 commits into from
Jan 3, 2023
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
@@ -1,3 +1,3 @@
const b = require('./b');

output = b.foo + b.bar;
output = [b.foo, b.bar, b.foobar];
Original file line number Diff line number Diff line change
@@ -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');
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const b = require('./b');

output = b.foo;
output = [b.foo, b.bar];
Original file line number Diff line number Diff line change
@@ -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();
8 changes: 4 additions & 4 deletions packages/core/integration-tests/test/scope-hoisting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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 () {
Expand Down
1 change: 1 addition & 0 deletions packages/transformers/js/core/src/hoist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down