Skip to content

Commit

Permalink
fix: namespace import MemberProp name failed (#1826)
Browse files Browse the repository at this point in the history
Co-authored-by: brightwu <[email protected]>
  • Loading branch information
shulandmimi and wre232114 authored Oct 12, 2024
1 parent 7d84234 commit e1f5c69
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/three-cats-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farmfe/core": patch
---

fix namespace import MemberProp name failed
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ var index_cjs = __commonJs((module, exports)=>{
bar: function() {
return bar;
},
index_default: function() {
default: function() {
return _default;
},
foo: function() {
Expand All @@ -92,6 +92,6 @@ var index_cjs = __commonJs((module, exports)=>{
const foo = 'foo';
const bar = 'bar';
});
var index_default = _interop_require_default(index_cjs()).index_default, bar = index_cjs()["bar"], foo = index_cjs()["foo"];
var index_default = _interop_require_default(index_cjs()).default, bar = index_cjs()["bar"], foo = index_cjs()["foo"];
export { bar, foo };
export default index_default;
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
export * as ns from './foo';
export * as ns from './foo';

import * as ns from './foo';

const foo = 123;

console.log(ns.default, ns.foo, foo);
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
})(nodeInterop);
}var foo_default = 'foo';
const foo = 'foo';
const foo$1 = 'foo';
const bar = 'bar';
var foo_ns = {
bar: bar,
foo: foo,
foo: foo$1,
"default": foo_default,
__esModule: true
};

const foo = 123;
console.log(foo_ns.default, foo_ns.foo, foo);
export { foo_ns as ns };
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use std::collections::{HashMap, HashSet};
use farmfe_core::{
swc_common::DUMMY_SP,
swc_ecma_ast::{
AssignExpr, AssignOp, AssignTarget, BindingIdent, Expr, Id, KeyValuePatProp, ObjectPatProp,
Pat, Prop, PropName, SimpleAssignTarget,
AssignExpr, AssignOp, AssignTarget, BindingIdent, Expr, Id, KeyValuePatProp, KeyValueProp, MemberProp, ObjectPat, ObjectPatProp, Pat, Prop, PropName, PropOrSpread, SimpleAssignTarget
},
};
use farmfe_toolkit::{
Expand Down Expand Up @@ -98,7 +97,7 @@ impl<'a> VisitMut for RenameIdent<'a> {
match n {
Prop::Shorthand(m) => {
if let Some(new_name) = self.rename(m) {
*n = farmfe_core::swc_ecma_ast::Prop::KeyValue(farmfe_core::swc_ecma_ast::KeyValueProp {
*n = Prop::KeyValue(farmfe_core::swc_ecma_ast::KeyValueProp {
key: farmfe_core::swc_ecma_ast::PropName::Ident(Ident {
span: DUMMY_SP,
sym: m.sym.as_str().into(),
Expand All @@ -108,16 +107,75 @@ impl<'a> VisitMut for RenameIdent<'a> {
new_name.as_str().into(),
)),
});
return;
}
}

_ => {}
}

n.visit_mut_children_with(self);
n.visit_mut_children_with(self);
}

fn visit_mut_prop_or_spread(&mut self, n: &mut PropOrSpread) {
match n {
PropOrSpread::Prop(box p) => match p {
Prop::Shorthand(ident) => {
if let Some(new_name) = self.rename(ident) {
*p = Prop::KeyValue(KeyValueProp {
key: farmfe_core::swc_ecma_ast::PropName::Ident(Ident {
span: DUMMY_SP,
sym: ident.sym.as_str().into(),
optional: false,
}),
value: Box::new(farmfe_core::swc_ecma_ast::Expr::Ident(
new_name.as_str().into(),
)),
});
} else {
p.visit_mut_with(self);
}
}
Prop::KeyValue(key_value_prop) => {
key_value_prop.visit_mut_with(self);
}
_ => {
p.visit_mut_with(self);
}
},
PropOrSpread::Spread(s) => {
s.visit_mut_with(self);
}
}
}

fn visit_mut_key_value_prop(&mut self, n: &mut KeyValueProp) {
//
// skip it
// ```js
// {
// key: value,
// }
// ```
//
if let farmfe_core::swc_ecma_ast::PropName::Ident(_) = n.key {
} else {
n.key.visit_mut_with(self);
}

n.value.visit_mut_with(self);
}

_ => n.visit_mut_children_with(self),
fn visit_mut_key_value_pat_prop(&mut self, n: &mut KeyValuePatProp) {
if let PropName::Ident(_) = n.key {
} else {
n.key.visit_mut_with(self);
}

n.value.visit_mut_with(self);
}

fn visit_mut_object_pat(&mut self, n: &mut farmfe_core::swc_ecma_ast::ObjectPat) {
fn visit_mut_object_pat(&mut self, n: &mut ObjectPat) {
for prop in &mut n.props {
match prop {
ObjectPatProp::Assign(n) => {
Expand Down Expand Up @@ -150,8 +208,20 @@ impl<'a> VisitMut for RenameIdent<'a> {
n.visit_mut_with(self);
}
}
ObjectPatProp::KeyValue(n) => {
n.visit_mut_with(self);
}
_ => prop.visit_mut_children_with(self),
};
}
}

fn visit_mut_member_prop(&mut self, n: &mut MemberProp) {
// ns.default, skip
if let MemberProp::Ident(_) = n {
return;
}

n.visit_mut_children_with(self);
}
}

0 comments on commit e1f5c69

Please sign in to comment.