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

Replace imported values in computed optional member expressions #8187

Merged
merged 3 commits into from Jun 7, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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