Skip to content

Commit

Permalink
fix slot variable in arrow functions
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Jan 25, 2024
1 parent 5fd11ce commit a5e7d2d
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
43 changes: 43 additions & 0 deletions visitor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,49 @@ where
}
}

fn visit_mut_arrow_expr(&mut self, arrow_expr: &mut ArrowExpr) {
arrow_expr.visit_mut_children_with(self);

if !self.injecting_consts.is_empty() || !self.injecting_vars.is_empty() {
if let BlockStmtOrExpr::Expr(ret) = &*arrow_expr.body {
let mut stmts = vec![Stmt::Return(ReturnStmt {
span: DUMMY_SP,
arg: Some(ret.clone()),
})];

if !self.injecting_consts.is_empty() {
stmts.insert(
0,
Stmt::Decl(Decl::Var(Box::new(VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Const,
declare: false,
decls: mem::take(&mut self.injecting_consts),
}))),
);
}

if !self.injecting_vars.is_empty() {
stmts.insert(
0,
Stmt::Decl(Decl::Var(Box::new(VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Let,
declare: false,
decls: mem::take(&mut self.injecting_vars),
}))),
);
self.slot_counter = 1;
}

arrow_expr.body = Box::new(BlockStmtOrExpr::BlockStmt(BlockStmt {
span: DUMMY_SP,
stmts,
}));
}
}
}

fn visit_mut_expr(&mut self, expr: &mut Expr) {
expr.visit_mut_children_with(self);

Expand Down
17 changes: 17 additions & 0 deletions visitor/tests/fixture/slot-in-arrow-function-bug/input.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const Component = (row) => (
<NSpace>
<NButton
type="primary"
secondary
onClick={handler1}
>
{t('text1')}
</NButton>
<NButton onClick={handler2}>
{t('text2')}
</NButton>
<NButton type="error" onClick={handler3}>
{t('text3')}
</NButton>
</NSpace>
)
46 changes: 46 additions & 0 deletions visitor/tests/fixture/slot-in-arrow-function-bug/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { createVNode as _createVNode, isVNode as _isVNode, resolveComponent as _resolveComponent } from "vue";
function _isSlot(s) {
return typeof s === "function" || ({}).toString.call(s) === "[object Object]" && !_isVNode(s);
}
const Component = (row)=>{
let _slot, _slot2, _slot3;
return _createVNode(_resolveComponent("NSpace"), null, {
default: ()=>[
_createVNode(_resolveComponent("NButton"), {
"type": "primary",
"secondary": true,
"onClick": handler1
}, _isSlot(_slot = t('text1')) ? _slot : {
default: ()=>[
_slot
],
_: 1
}, 8, [
"secondary",
"onClick"
]),
_createVNode(_resolveComponent("NButton"), {
"onClick": handler2
}, _isSlot(_slot2 = t('text2')) ? _slot2 : {
default: ()=>[
_slot2
],
_: 1
}, 8, [
"onClick"
]),
_createVNode(_resolveComponent("NButton"), {
"type": "error",
"onClick": handler3
}, _isSlot(_slot3 = t('text3')) ? _slot3 : {
default: ()=>[
_slot3
],
_: 1
}, 8, [
"onClick"
])
],
_: 1
});
};

0 comments on commit a5e7d2d

Please sign in to comment.