Skip to content

Commit

Permalink
Replace imported values in computed optional member expressions (#8187)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Jun 7, 2022
1 parent d629007 commit a5b43a3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
@@ -1,5 +1,7 @@
import {B} from "./b.js";

export function foo(x) {
return [x.foo, x?.foo];
return [x.foo, x?.foo, x[B], x?.[B]];
}

output = foo;
@@ -0,0 +1 @@
export const B = "bar";
4 changes: 2 additions & 2 deletions packages/core/integration-tests/test/scope-hoisting.js
Expand Up @@ -186,7 +186,7 @@ describe('scope hoisting', function () {
assert.deepEqual(output, ['1', '2']);
});

it("doesn't rename member expression properties", async function () {
it('correctly renames member expression properties', async function () {
let b = await bundle(
path.join(
__dirname,
Expand All @@ -195,7 +195,7 @@ describe('scope hoisting', function () {
);

let output = await run(b);
assert.deepEqual(output({foo: 12}), [12, 12]);
assert.deepEqual(output({foo: 12, bar: 34}), [12, 12, 34, 34]);
});

it('supports renaming imports', async function () {
Expand Down
18 changes: 12 additions & 6 deletions packages/transformers/js/core/src/hoist.rs
Expand Up @@ -522,12 +522,18 @@ impl<'a> Fold for Hoist<'a> {
question_dot_token: opt.question_dot_token,
base: match opt.base {
OptChainBase::Call(call) => OptChainBase::Call(call.fold_with(self)),
OptChainBase::Member(member) => OptChainBase::Member(MemberExpr {
span: member.span,
obj: member.obj.fold_with(self),
// Don't visit member.prop so we avoid the ident visitor.
prop: member.prop,
}),
OptChainBase::Member(member) => {
if match_property_name(&member).is_some() {
OptChainBase::Member(MemberExpr {
span: member.span,
obj: member.obj.fold_with(self),
// Don't visit member.prop so we avoid the ident visitor.
prop: member.prop,
})
} else {
OptChainBase::Member(member.fold_children_with(self))
}
}
},
});
}
Expand Down

0 comments on commit a5b43a3

Please sign in to comment.