Skip to content

Commit

Permalink
disable automatic use declaration injection
Browse files Browse the repository at this point in the history
  • Loading branch information
Benedikt Strehle committed Sep 29, 2024
1 parent abf573f commit 38c79f0
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 19 deletions.
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,4 +464,20 @@ test!(
<span>inline&nbsp;text</span>
</div>
"#
);


test!(
Syntax::Es(EsSyntax {
jsx: true,
..Default::default()
},),
|_| TransformVisitor,
t36,
r#"
let x = 10;
run(() => {
console.log(x + 1);
})
"#
);
12 changes: 7 additions & 5 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ use swc_core::{
common::{util::take::Take, SyntaxContext, DUMMY_SP},
ecma::{
ast::{
ArrowExpr, BlockStmt, BlockStmtOrExpr, CallExpr, Callee, Expr, ExprOrSpread, ExprStmt,
FnDecl, Ident, JSXEmptyExpr, JSXExpr, JSXExprContainer, Lit, Null, ReturnStmt, Stmt,
Str, VarDecl,
IdentName, JSXAttr, JSXAttrName, JSXAttrValue, JSXElement, JSXElementChild, JSXSpreadChild, MemberExpr, MemberProp
ArrowExpr, BlockStmt, BlockStmtOrExpr, CallExpr, Callee, Expr, ExprOrSpread, ExprStmt, FnDecl, Ident, IdentName, JSXAttr, JSXAttrName, JSXAttrValue, JSXElement, JSXElementChild, JSXEmptyExpr, JSXExpr, JSXExprContainer, JSXSpreadChild, Lit, MemberExpr, MemberProp, Null, ReturnStmt, SpanExt, Stmt, Str, VarDecl
},
visit::{Fold, Visit, FoldWith, VisitWith},
visit::{Fold, FoldWith, Visit, VisitWith},
},
};

Expand All @@ -26,6 +23,7 @@ impl VariableCollector {

impl Visit for VariableCollector {
fn visit_ident(&mut self, ident: &Ident) {

// add variable to list if not already present
if !self.variables.contains(&ident.sym.to_string()) {
self.variables.push(ident.sym.to_string());
Expand Down Expand Up @@ -233,6 +231,8 @@ impl TransformVisitor {
}

fn transform_transferable_closure(arrow: &ArrowExpr, ctxt: SyntaxContext) -> ArrowExpr {
// TODO: enable?
return arrow.clone();
// find all variables used in the arrow function body
let mut collector = VariableCollector::new();
arrow.body.visit_with(&mut collector);
Expand Down Expand Up @@ -304,6 +304,8 @@ impl TransformVisitor {
}

fn transform_transferable_call_expr(call: &CallExpr) -> CallExpr {
// TODO: enable?
return call.clone();
let arg = TransformVisitor::get_first_arg(call);

match arg.unwrap_parens() {
Expand Down
1 change: 0 additions & 1 deletion tests/__swc_snapshots__/src/lib.rs/t3.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
run(()=>{
use(console, x, y);
console.log(x + y);
return x + 1;
});
4 changes: 4 additions & 0 deletions tests/__swc_snapshots__/src/lib.rs/t36.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let x = 10;
run(()=>{
console.log(x + 1);
});
5 changes: 1 addition & 4 deletions tests/__swc_snapshots__/src/lib.rs/t4.js
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
run(()=>{
use(x);
return x + 1;
});
run(()=>x + 1);
1 change: 0 additions & 1 deletion tests/__swc_snapshots__/src/lib.rs/t5.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
run(()=>{
use(x, y);
use(x);
return x + y;
});
5 changes: 1 addition & 4 deletions tests/__swc_snapshots__/src/lib.rs/t6.js
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
<button onclick:frontend={()=>{
use(console, x);
return console.log(x);
}}/>;
<button onclick:frontend={()=>console.log(x)}/>;
5 changes: 1 addition & 4 deletions tests/__swc_snapshots__/src/lib.rs/t9.js
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
<button value:frontend={always(()=>{
use(x);
return x + 1;
})}/>;
<button value:frontend={always(()=>x + 1)}/>;

0 comments on commit 38c79f0

Please sign in to comment.