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

Don't visit the property of non-computed member expressions in the esm2cjs folder #7102

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