Skip to content

Commit

Permalink
Properly visit member expressions (#7228)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Nov 2, 2021
1 parent e0440dc commit ff07be5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
@@ -0,0 +1,6 @@
export const foo = {
a: "b",
bar: "bar"
};

export const bar = "a";
@@ -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
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
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

0 comments on commit ff07be5

Please sign in to comment.