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

Properly visit member expressions #7228

Merged
merged 2 commits into from
Nov 2, 2021
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
@@ -0,0 +1,6 @@
export const foo = {
a: "b",
bar: "bar"
};

export const bar = "a";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { foo, bar } from "./foo.js";

export default [bar, foo[bar], foo.bar];
8 changes: 8 additions & 0 deletions packages/core/integration-tests/test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -4982,6 +4982,14 @@ describe('javascript', function () {
assert.deepEqual(res.default, 123);
});

it('should replace imported values in member expressions', async function () {
let b = await bundle(
path.join(__dirname, 'integration/js-import-member/index.js'),
);
let res = await run(b);
assert.deepEqual(res.default, ['a', 'b', 'bar']);
});

it('should not freeze live default imports', async function () {
let b = await bundle(
path.join(__dirname, 'integration/js-import-default-live/index.js'),
Expand Down
5 changes: 2 additions & 3 deletions packages/transformers/js/core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,10 @@ macro_rules! fold_member_expr_skip_prop {
&mut self,
mut node: swc_ecmascript::ast::MemberExpr,
) -> swc_ecmascript::ast::MemberExpr {
node.obj = node.obj.fold_children_with(self);
node.obj = node.obj.fold_with(self);

// To ensure that fold_expr doesn't replace `require` in non-computed member expressions
if node.computed {
node.prop = node.prop.fold_children_with(self);
node.prop = node.prop.fold_with(self);
}

node
Expand Down