Skip to content

Commit

Permalink
Don't visit the property of non-computed member expressions in the es…
Browse files Browse the repository at this point in the history
…m2cjs folder (#7102)
  • Loading branch information
mischnic committed Oct 18, 2021
1 parent 4a62581 commit efe7227
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 10 deletions.
@@ -0,0 +1 @@
export const Foo = null;
@@ -0,0 +1,7 @@
import { Foo } from "./foo";

const S = {
Foo: () => "S",
};

module.exports = <S.Foo />;
7 changes: 7 additions & 0 deletions packages/core/integration-tests/test/transpilation.js
Expand Up @@ -87,6 +87,13 @@ describe('transpilation', function() {
assert(file.includes('fileName: "integration/jsx/index.jsx"'));
});

it('should support compiling JSX correctly with member expression type', async function() {
await bundle(path.join(__dirname, '/integration/jsx-member/index.jsx'));

let file = await outputFS.readFile(path.join(distDir, 'index.js'), 'utf8');
assert(file.includes('React.createElement(S.Foo'));
});

it('should support compiling JSX in JS files with React dependency', async function() {
await bundle(path.join(__dirname, '/integration/jsx-react/index.js'));

Expand Down
12 changes: 2 additions & 10 deletions packages/transformers/js/core/src/dependency_collector.rs
Expand Up @@ -10,6 +10,7 @@ use swc_ecmascript::ast;
use swc_ecmascript::utils::ident::IdentLike;
use swc_ecmascript::visit::{Fold, FoldWith};

use crate::fold_member_expr_skip_prop;
use crate::utils::*;
use crate::Config;

Expand Down Expand Up @@ -780,16 +781,7 @@ impl<'a> Fold for DependencyCollector<'a> {
node.fold_children_with(self)
}

fn fold_member_expr(&mut self, mut node: ast::MemberExpr) -> ast::MemberExpr {
node.obj = node.obj.fold_children_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
}
fold_member_expr_skip_prop! {}

fn fold_expr(&mut self, node: ast::Expr) -> ast::Expr {
use ast::*;
Expand Down
4 changes: 4 additions & 0 deletions packages/transformers/js/core/src/modules.rs
Expand Up @@ -6,6 +6,8 @@ use swc_ecma_preset_env::{Feature, Versions};
use swc_ecmascript::ast::*;
use swc_ecmascript::visit::{Fold, FoldWith};

use crate::fold_member_expr_skip_prop;

type IdentId = (JsWord, SyntaxContext);
macro_rules! id {
($ident: expr) => {
Expand Down Expand Up @@ -613,4 +615,6 @@ impl Fold for ESMFold {
_ => node.fold_children_with(self),
}
}

fold_member_expr_skip_prop! {}
}
19 changes: 19 additions & 0 deletions packages/transformers/js/core/src/utils.rs
Expand Up @@ -309,3 +309,22 @@ impl BailoutReason {
}
}
}

#[macro_export]
macro_rules! fold_member_expr_skip_prop {
() => {
fn fold_member_expr(
&mut self,
mut node: swc_ecmascript::ast::MemberExpr,
) -> swc_ecmascript::ast::MemberExpr {
node.obj = node.obj.fold_children_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
}
};
}

0 comments on commit efe7227

Please sign in to comment.