-
What should I do to replace an AST node with another node of a different type? For example: // input:
<For each={list}/>
// output:
{list.map()} I find that SWC is difficult to modify when the semantics and types are completely different. Is there any good idea here? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 16 replies
-
Currently, swc is designed to modify AST nodes from rust side. if you are using swc form rust, you can simply implement Fold. |
Beta Was this translation helpful? Give feedback.
-
fn fold_jsx_namespaced_name(&mut self, node: ast::JSXNamespacedName) -> ast::Ident { // here renturn a different type
if *node.name.sym == *"key" {
return ast::Ident {
span: DUMMY_SP,
optional: false,
sym: format!("{}", "key").into(),
};
} else {
return node;
}
} In fact, I want to return a completely different ast node. I need to completely distort the semantics and modify the structure in the right place. I really can't find a good way. I've cried many times. Do you have a way? |
Beta Was this translation helpful? Give feedback.
-
I tried your method and found it still couldn't work. // input:
<For each={list}/>
// output:
{list.map()} // this is a Stmt, not Expr. not this: // input:
<For each={list}/>
// output:
list.map() Do you have any ideas in this situation? |
Beta Was this translation helpful? Give feedback.
-
@kdy1 Thank you, I found a better way to distort semantics, use match c {
ast::JSXElementChild::JSXElement(v) => {
ast::JSXElementChild::JSXExprContainer... I have to say, it's really hard [cring] |
Beta Was this translation helpful? Give feedback.
-
@kdy1 I am comming back 😭 // input
<Component>
<view slot=header>header</view>
</Component>
// output
<Component $slot$header={<view>header</view>}>
</Component> When I need to change the slot tag into render props, I need to modify the reference of the parent. Do you have a good idea? |
Beta Was this translation helpful? Give feedback.
Currently, swc is designed to modify AST nodes from rust side.
if you are using swc form rust, you can simply implement Fold.