From adc8d1ecbb43927ffe6fe7d12e0a0eb4aef433f8 Mon Sep 17 00:00:00 2001 From: Manu MA Date: Wed, 29 Dec 2021 11:38:13 +0100 Subject: [PATCH 1/5] feat: support implicit qhooks --- ...re__test__example_implicit_qhooks.snap.new | 124 +++++++++ src/optimizer/core/src/test.rs | 24 ++ src/optimizer/core/src/transform.rs | 236 ++++++++++++------ 3 files changed, 302 insertions(+), 82 deletions(-) create mode 100644 src/optimizer/core/src/snapshots/qwik_core__test__example_implicit_qhooks.snap.new diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_implicit_qhooks.snap.new b/src/optimizer/core/src/snapshots/qwik_core__test__example_implicit_qhooks.snap.new new file mode 100644 index 00000000000..31a3376d444 --- /dev/null +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_implicit_qhooks.snap.new @@ -0,0 +1,124 @@ +--- +source: src/optimizer/core/src/test.rs +expression: output + +--- +==INPUT== + + +const Header = qComponent({ + "onMount": qHook(() => { console.log("mount") }), + "otherThing": () => { console.log("otherThing") }, + onRender: () => { + return ( +
console.log(ctx)} + on:click={(ctx) => console.log(ctx)} + /> + ); + } +}); + +============================= test.tsx == + +/*#__PURE__*/ qComponent({ + "onMount": qHook(()=>import("./h_test_header_onmount") + , "Header_onMount"), + "otherThing": qHook(()=>import("./h_test_header_otherthing") + , "Header_otherThing"), + onRender: qHook(()=>import("./h_test_header_onrender") + , "Header_onRender") +}); + +============================= h_test_header_onmount.tsx (ENTRY POINT)== + +export const Header_onMount = /*#__PURE__*/ qHook(()=>{ + console.log("mount"); +}); + +============================= h_test_header_otherthing.tsx (ENTRY POINT)== + +export const Header_otherThing = /*#__PURE__*/ qHook(()=>{ + console.log("otherThing"); +}); + +============================= h_test_header_onrender_div_onclick.tsx (ENTRY POINT)== + +export const Header_onRender_div_onclick = /*#__PURE__*/ qHook((ctx)=>console.log(ctx) +); + +============================= h_test_header_onrender.tsx (ENTRY POINT)== + +export const Header_onRender = /*#__PURE__*/ qHook(()=>{ + return (
console.log(ctx) + } on:click={qHook(()=>import("./h_test_header_onrender_div_onclick") + , "Header_onRender_div_onclick")}/>); +}); + +== HOOKS == + +[ + { + "origin": "test.tsx", + "name": "Header_onMount", + "entry": null, + "canonicalFilename": "h_test_header_onmount", + "localDecl": [], + "localIdents": [ + "console", + "log", + "qHook" + ] + }, + { + "origin": "test.tsx", + "name": "Header_otherThing", + "entry": null, + "canonicalFilename": "h_test_header_otherthing", + "localDecl": [], + "localIdents": [ + "console", + "log", + "qHook" + ] + }, + { + "origin": "test.tsx", + "name": "Header_onRender_div_onclick", + "entry": null, + "canonicalFilename": "h_test_header_onrender_div_onclick", + "localDecl": [ + "ctx" + ], + "localIdents": [ + "console", + "ctx", + "log", + "qHook" + ] + }, + { + "origin": "test.tsx", + "name": "Header_onRender", + "entry": null, + "canonicalFilename": "h_test_header_onrender", + "localDecl": [ + "ctx" + ], + "localIdents": [ + "click", + "console", + "ctx", + "div", + "import", + "log", + "on", + "prop", + "qHook" + ] + } +] + +== DIAGNOSTICS == + +[] diff --git a/src/optimizer/core/src/test.rs b/src/optimizer/core/src/test.rs index 27654e73a1b..a6aed9366e4 100644 --- a/src/optimizer/core/src/test.rs +++ b/src/optimizer/core/src/test.rs @@ -294,6 +294,30 @@ export const Header = qComponent({ ); } +#[test] +fn example_implicit_qhooks() { + test_input( + "test.tsx", + r#" +const Header = qComponent({ + "onMount": qHook(() => { console.log("mount") }), + "otherThing": () => { console.log("otherThing") }, + onRender: () => { + return ( +
console.log(ctx)} + on:click={(ctx) => console.log(ctx)} + /> + ); + } +}); + "#, + EntryStrategy::Hook, + MinifyMode::Simplify, + false, + ); +} + #[test] fn issue_117() { test_input( diff --git a/src/optimizer/core/src/transform.rs b/src/optimizer/core/src/transform.rs index e97a4ddc999..f1dd9b2c933 100644 --- a/src/optimizer/core/src/transform.rs +++ b/src/optimizer/core/src/transform.rs @@ -44,9 +44,18 @@ impl TransformContext { pub type ThreadSafeTransformContext = Arc>; +#[derive(Debug)] +enum PositionToken { + QComponent, + ObjectProp, + JSXListener, + Any, +} + #[allow(clippy::module_name_repetitions)] pub struct HookTransform<'a> { stack_ctxt: Vec, + position_ctxt: Vec, root_sym: Option, context: ThreadSafeTransformContext, @@ -69,6 +78,7 @@ impl<'a> HookTransform<'a> { HookTransform { path_data, stack_ctxt: Vec::with_capacity(16), + position_ctxt: Vec::with_capacity(16), hooks, root_sym: None, comments, @@ -121,6 +131,99 @@ impl<'a> HookTransform<'a> { declare: node.declare, } } + + fn create_synthetic_qhook(&mut self, fn_expr: ast::ArrowExpr) -> ast::CallExpr { + ast::CallExpr { + callee: ast::ExprOrSuper::Expr(Box::new(ast::Expr::Ident(ast::Ident::new( + QHOOK.clone(), + DUMMY_SP, + )))), + span: fn_expr.span, + type_args: None, + args: vec![ast::ExprOrSpread { + spread: None, + expr: Box::new(ast::Expr::Arrow(fn_expr)), + }], + } + } + + fn handle_qhook(&mut self, node: ast::CallExpr, qhook_span: Span) -> ast::CallExpr { + let mut node = node; + let mut user_symbol = None; + if let Some(second_arg) = node.args.get(1) { + if let ast::Expr::Lit(ast::Lit::Str(ref str)) = *second_arg.expr { + if validate_sym(&str.value) { + let custom_sym = str.value.to_string(); + user_symbol = Some(custom_sym); + } else { + HANDLER.with(|handler| { + handler + .struct_span_err( + str.span, + "Second argument should be the name of a valid identifier", + ) + .emit(); + }); + } + } + } + + let symbol_name = match self.register_context_name(&user_symbol) { + Ok(symbol_name) => symbol_name, + Err(err) => { + HANDLER.with(|handler| { + handler + .struct_span_err(node.span, &format!("{}", err)) + .emit(); + }); + user_symbol.unwrap() + } + }; + + let mut canonical_filename = + ["h_", &self.path_data.file_prefix, "_", &symbol_name].concat(); + canonical_filename.make_ascii_lowercase(); + + // Remove last arguments + node.args.drain(1..); + + let folded = node.fold_children_with(self); + let hook_collect = HookCollect::new(&folded); + let entry = self.entry_policy.get_entry_for_sym( + &symbol_name, + self.path_data, + &self.stack_ctxt, + &hook_collect, + &folded, + ); + + let import_path = fix_path( + "a", + &self.path_data.path, + &format!( + "./{}", + entry + .as_ref() + .map(|e| e.as_ref()) + .unwrap_or(&canonical_filename) + ), + ) + // TODO: check with manu + .unwrap(); + + let (local_decl, local_idents) = hook_collect.get_words(); + self.hooks.push(Hook { + entry, + canonical_filename, + name: symbol_name.clone(), + expr: folded, + local_decl, + local_idents, + origin: self.path_data.path.to_string_lossy().into(), + }); + + create_inline_qhook(import_path, &symbol_name, qhook_span) + } } impl<'a> Fold for HookTransform<'a> { @@ -211,121 +314,90 @@ impl<'a> Fold for HookTransform<'a> { let mut stacked = false; if let ast::PropName::Ident(ref ident) = node.key { self.stack_ctxt.push(ident.sym.to_string()); + self.position_ctxt.push(PositionToken::ObjectProp); stacked = true; } if let ast::PropName::Str(ref s) = node.key { self.stack_ctxt.push(s.value.to_string()); + self.position_ctxt.push(PositionToken::ObjectProp); stacked = true; } let o = node.fold_children_with(self); if stacked { + self.position_ctxt.pop(); self.stack_ctxt.pop(); } o } fn fold_jsx_attr(&mut self, node: ast::JSXAttr) -> ast::JSXAttr { - let mut stacked = false; - if let ast::JSXAttrName::Ident(ref ident) = node.name { - self.stack_ctxt.push(ident.sym.to_string()); - stacked = true; + let mut is_listener = false; + match node.name { + ast::JSXAttrName::Ident(ref ident) => { + let ident_name = ident.sym.to_string(); + println!("{}", ident_name); + self.stack_ctxt.push(ident_name); + } + ast::JSXAttrName::JSXNamespacedName(ref namespaced) => { + let ns_name = namespaced.ns.sym.as_ref(); + let ident_name = [ns_name, namespaced.name.sym.as_ref()].concat(); + println!("namespaced: {}", &ident_name); + self.stack_ctxt.push(ident_name); + + is_listener = ns_name == "on"; + if is_listener { + self.position_ctxt.push(PositionToken::JSXListener); + } + } } let o = node.fold_children_with(self); - if stacked { - self.stack_ctxt.pop(); + self.stack_ctxt.pop(); + if is_listener { + self.position_ctxt.pop(); } o } + fn fold_expr(&mut self, node: ast::Expr) -> ast::Expr { + let node = match (self.position_ctxt.as_slice(), node) { + ( + [.., PositionToken::QComponent, PositionToken::Any, PositionToken::ObjectProp], + ast::Expr::Arrow(arrow), + ) + | ([.., PositionToken::JSXListener], ast::Expr::Arrow(arrow)) => { + ast::Expr::Call(self.create_synthetic_qhook(arrow)) + } + (_, node) => node, + }; + + self.position_ctxt.push(PositionToken::Any); + let o = node.fold_children_with(self); + self.position_ctxt.pop(); + o + } + fn fold_call_expr(&mut self, node: ast::CallExpr) -> ast::CallExpr { + let mut open_component = false; if let ast::ExprOrSuper::Expr(expr) = &node.callee { if let ast::Expr::Ident(id) = &**expr { let qhook_span = id.span; if QCOMPONENT.eq(&id.sym) { + self.position_ctxt.push(PositionToken::QComponent); + open_component = true; if let Some(comments) = self.comments { comments.add_pure_comment(node.span.lo); } } else if QHOOK.eq(&id.sym) { - let mut node = node; - let mut user_symbol = None; - if let Some(second_arg) = node.args.get(1) { - if let ast::Expr::Lit(ast::Lit::Str(ref str)) = *second_arg.expr { - if validate_sym(&str.value) { - let custom_sym = str.value.to_string(); - user_symbol = Some(custom_sym); - } else { - HANDLER.with(|handler| { - handler - .struct_span_err( - str.span, - "Second argument should be the name of a valid identifier", - ) - .emit(); - }); - } - } - } - - let symbol_name = match self.register_context_name(&user_symbol) { - Ok(symbol_name) => symbol_name, - Err(err) => { - HANDLER.with(|handler| { - handler - .struct_span_err(node.span, &format!("{}", err)) - .emit(); - }); - user_symbol.unwrap() - } - }; - - let mut canonical_filename = - ["h_", &self.path_data.file_prefix, "_", &symbol_name].concat(); - canonical_filename.make_ascii_lowercase(); - - // Remove last arguments - node.args.drain(1..); - - let folded = node.fold_children_with(self); - let hook_collect = HookCollect::new(&folded); - let entry = self.entry_policy.get_entry_for_sym( - &symbol_name, - self.path_data, - &self.stack_ctxt, - &hook_collect, - &folded, - ); - - let import_path = fix_path( - "a", - &self.path_data.path, - &format!( - "./{}", - entry - .as_ref() - .map(|e| e.as_ref()) - .unwrap_or(&canonical_filename) - ), - ) - // TODO: check with manu - .unwrap(); - - let (local_decl, local_idents) = hook_collect.get_words(); - self.hooks.push(Hook { - entry, - canonical_filename, - name: symbol_name.clone(), - expr: folded, - local_decl, - local_idents, - origin: self.path_data.path.to_string_lossy().into(), - }); - - return create_inline_qhook(import_path, &symbol_name, qhook_span); + return self.handle_qhook(node, qhook_span); } } } - node.fold_children_with(self) + let o = node.fold_children_with(self); + if open_component { + self.position_ctxt.pop(); + } + o } } From 8b277b005d19cc43dbd618ccb9a332032836a0e8 Mon Sep 17 00:00:00 2001 From: Manu MA Date: Wed, 29 Dec 2021 12:00:20 +0100 Subject: [PATCH 2/5] fix: improve identifier detection --- src/optimizer/core/src/collector.rs | 27 ++++++++++++++++++- .../snapshots/qwik_core__test__example_1.snap | 7 +---- .../qwik_core__test__example_10.snap | 6 +---- .../qwik_core__test__example_11.snap | 7 ++--- .../qwik_core__test__example_12.snap | 3 +-- .../qwik_core__test__example_13.snap | 3 +-- .../snapshots/qwik_core__test__example_2.snap | 7 +---- .../snapshots/qwik_core__test__example_3.snap | 7 +---- .../snapshots/qwik_core__test__example_4.snap | 7 +---- .../snapshots/qwik_core__test__example_5.snap | 5 ---- .../snapshots/qwik_core__test__example_6.snap | 3 +-- .../snapshots/qwik_core__test__example_7.snap | 7 +---- .../snapshots/qwik_core__test__example_8.snap | 6 ++--- ..._core__test__example_implicit_qhooks.snap} | 18 +++---------- src/optimizer/core/src/transform.rs | 2 -- 15 files changed, 42 insertions(+), 73 deletions(-) rename src/optimizer/core/src/snapshots/{qwik_core__test__example_implicit_qhooks.snap.new => qwik_core__test__example_implicit_qhooks.snap} (92%) diff --git a/src/optimizer/core/src/collector.rs b/src/optimizer/core/src/collector.rs index f4e5bd6566b..7a9021d41c0 100644 --- a/src/optimizer/core/src/collector.rs +++ b/src/optimizer/core/src/collector.rs @@ -434,9 +434,34 @@ impl Visit for HookCollect { self.expr_ctxt.pop(); } + fn visit_jsx_opening_element(&mut self, node: &ast::JSXOpeningElement) { + let mut stacked = false; + if let ast::JSXElementName::Ident(ref ident) = node.name { + let ident_name = ident.sym.as_ref().chars().next().unwrap(); + if let 'A'..='Z' = ident_name { + self.expr_ctxt.push(ExprOrSkip::Expr); + } else { + self.expr_ctxt.push(ExprOrSkip::Skip); + } + stacked = true; + } + + node.visit_children_with(self); + if stacked { + self.expr_ctxt.pop(); + } + } + fn visit_jsx_attr(&mut self, node: &ast::JSXAttr) { + self.expr_ctxt.push(ExprOrSkip::Skip); + node.visit_children_with(self); + self.expr_ctxt.pop(); + } + fn visit_ident(&mut self, node: &ast::Ident) { if let Some(ExprOrSkip::Expr) = self.expr_ctxt.last() { - self.local_idents.insert(id!(node)); + if node.span.ctxt() != SyntaxContext::empty() { + self.local_idents.insert(id!(node)); + } } } diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_1.snap b/src/optimizer/core/src/snapshots/qwik_core__test__example_1.snap index 44d83695ecc..58185a2b594 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__example_1.snap +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_1.snap @@ -1,5 +1,5 @@ --- -source: src/test.rs +source: src/optimizer/core/src/test.rs expression: output --- @@ -53,7 +53,6 @@ export const Header_onRender = /*#__PURE__*/ qHook(()=>{ "localDecl": [], "localIdents": [ "console", - "log", "qHook" ] }, @@ -68,7 +67,6 @@ export const Header_onRender = /*#__PURE__*/ qHook(()=>{ "localIdents": [ "console", "ctx", - "log", "qHook" ] }, @@ -79,9 +77,6 @@ export const Header_onRender = /*#__PURE__*/ qHook(()=>{ "canonicalFilename": "h_test_header_onrender", "localDecl": [], "localIdents": [ - "div", - "import", - "onClick", "qHook" ] } diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_10.snap b/src/optimizer/core/src/snapshots/qwik_core__test__example_10.snap index 99e7e3cdb2e..0b812bc8334 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__example_10.snap +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_10.snap @@ -76,7 +76,6 @@ export const Header = /*#__PURE__*/ qHook((decl1, { decl2 }, [decl3])=>{ "decl1", "decl2", "decl3", - "div", "ident1", "ident10", "ident11", @@ -89,10 +88,7 @@ export const Header = /*#__PURE__*/ qHook((decl1, { decl2 }, [decl3])=>{ "ident7", "ident8", "ident9", - "no", - "onClick", - "qHook", - "required" + "qHook" ] } ] diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_11.snap b/src/optimizer/core/src/snapshots/qwik_core__test__example_11.snap index 76fbc7737fc..408c581a792 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__example_11.snap +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_11.snap @@ -1,5 +1,5 @@ --- -source: src/test.rs +source: src/optimizer/core/src/test.rs expression: output --- @@ -100,10 +100,7 @@ export { App_onRender } from "./h_test_app_onrender"; "Header", "bbar", "dep2", - "import", - "onClick", - "qHook", - "stuff" + "qHook" ] }, { diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_12.snap b/src/optimizer/core/src/snapshots/qwik_core__test__example_12.snap index 0bbaadad7f7..9684e790059 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__example_12.snap +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_12.snap @@ -1,5 +1,5 @@ --- -source: src/test.rs +source: src/optimizer/core/src/test.rs expression: output --- @@ -38,7 +38,6 @@ export { sym2 } from "./h_test_sym2"; "localDecl": [], "localIdents": [ "console", - "log", "qHook" ] } diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_13.snap b/src/optimizer/core/src/snapshots/qwik_core__test__example_13.snap index 280cac1d713..585d5c68819 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__example_13.snap +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_13.snap @@ -1,5 +1,5 @@ --- -source: src/test.rs +source: src/optimizer/core/src/test.rs expression: output --- @@ -38,7 +38,6 @@ export { Header_onRender } from "./h_test_header_onrender"; "localDecl": [], "localIdents": [ "console", - "log", "qHook" ] } diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_2.snap b/src/optimizer/core/src/snapshots/qwik_core__test__example_2.snap index 2e536c79e17..75b1c4b21a1 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__example_2.snap +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_2.snap @@ -1,5 +1,5 @@ --- -source: src/test.rs +source: src/optimizer/core/src/test.rs expression: output --- @@ -53,7 +53,6 @@ export const Header_onRender = /*#__PURE__*/ qHook(()=>{ "localDecl": [], "localIdents": [ "console", - "log", "qHook" ] }, @@ -68,7 +67,6 @@ export const Header_onRender = /*#__PURE__*/ qHook(()=>{ "localIdents": [ "console", "ctx", - "log", "qHook" ] }, @@ -79,9 +77,6 @@ export const Header_onRender = /*#__PURE__*/ qHook(()=>{ "canonicalFilename": "h_test_header_onrender", "localDecl": [], "localIdents": [ - "div", - "import", - "onClick", "qHook" ] } diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_3.snap b/src/optimizer/core/src/snapshots/qwik_core__test__example_3.snap index c85888949a7..4ffc64a3cb7 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__example_3.snap +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_3.snap @@ -1,5 +1,5 @@ --- -source: src/test.rs +source: src/optimizer/core/src/test.rs expression: output --- @@ -59,7 +59,6 @@ export const App_Header_onRender = /*#__PURE__*/ qHook(()=>{ "localDecl": [], "localIdents": [ "console", - "log", "qHook" ] }, @@ -74,7 +73,6 @@ export const App_Header_onRender = /*#__PURE__*/ qHook(()=>{ "localIdents": [ "console", "ctx", - "log", "qHook" ] }, @@ -85,9 +83,6 @@ export const App_Header_onRender = /*#__PURE__*/ qHook(()=>{ "canonicalFilename": "h_test_app_header_onrender", "localDecl": [], "localIdents": [ - "div", - "import", - "onClick", "qHook" ] } diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_4.snap b/src/optimizer/core/src/snapshots/qwik_core__test__example_4.snap index 6c22ec67651..12eea2bd7f2 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__example_4.snap +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_4.snap @@ -1,5 +1,5 @@ --- -source: src/test.rs +source: src/optimizer/core/src/test.rs expression: output --- @@ -59,7 +59,6 @@ export const App_Header_onRender = /*#__PURE__*/ qHook(()=>{ "localDecl": [], "localIdents": [ "console", - "log", "qHook" ] }, @@ -74,7 +73,6 @@ export const App_Header_onRender = /*#__PURE__*/ qHook(()=>{ "localIdents": [ "console", "ctx", - "log", "qHook" ] }, @@ -85,9 +83,6 @@ export const App_Header_onRender = /*#__PURE__*/ qHook(()=>{ "canonicalFilename": "h_test_app_header_onrender", "localDecl": [], "localIdents": [ - "div", - "import", - "onClick", "qHook" ] } diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_5.snap b/src/optimizer/core/src/snapshots/qwik_core__test__example_5.snap index 5affd03b950..4ac3ddcfb43 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__example_5.snap +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_5.snap @@ -62,7 +62,6 @@ export const Header_onRender = /*#__PURE__*/ qHook(()=>{ "localIdents": [ "console", "ctx", - "log", "qHook" ] }, @@ -77,7 +76,6 @@ export const Header_onRender = /*#__PURE__*/ qHook(()=>{ "localIdents": [ "console", "ctx", - "log", "qHook" ] }, @@ -88,9 +86,6 @@ export const Header_onRender = /*#__PURE__*/ qHook(()=>{ "canonicalFilename": "h_test_header_onrender", "localDecl": [], "localIdents": [ - "div", - "import", - "onClick", "qHook" ] } diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_6.snap b/src/optimizer/core/src/snapshots/qwik_core__test__example_6.snap index 19f5ff8d1ab..9efdd140f57 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__example_6.snap +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_6.snap @@ -1,5 +1,5 @@ --- -source: src/test.rs +source: src/optimizer/core/src/test.rs expression: output --- @@ -32,7 +32,6 @@ export const sym1 = /*#__PURE__*/ qHook((ctx)=>console.log("1") "localIdents": [ "console", "ctx", - "log", "qHook" ] } diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_7.snap b/src/optimizer/core/src/snapshots/qwik_core__test__example_7.snap index ac7cd86068a..5d9ab1b9670 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__example_7.snap +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_7.snap @@ -1,5 +1,5 @@ --- -source: src/test.rs +source: src/optimizer/core/src/test.rs expression: output --- @@ -78,7 +78,6 @@ export const App_onRender = /*#__PURE__*/ qHook(()=>{ "localDecl": [], "localIdents": [ "console", - "log", "qHook" ] }, @@ -93,7 +92,6 @@ export const App_onRender = /*#__PURE__*/ qHook(()=>{ "localIdents": [ "console", "ctx", - "log", "qHook" ] }, @@ -104,9 +102,6 @@ export const App_onRender = /*#__PURE__*/ qHook(()=>{ "canonicalFilename": "h_test_header_onrender", "localDecl": [], "localIdents": [ - "div", - "import", - "onClick", "qHook" ] }, diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_8.snap b/src/optimizer/core/src/snapshots/qwik_core__test__example_8.snap index 3de92eed150..a989062f8c9 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__example_8.snap +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_8.snap @@ -1,5 +1,5 @@ --- -source: src/test.rs +source: src/optimizer/core/src/test.rs expression: output --- @@ -55,9 +55,7 @@ export const Header_onRender = /*#__PURE__*/ qHook((hola)=>{ "Header", "global", "hola", - "nothere", - "qHook", - "stuff" + "qHook" ] } ] diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_implicit_qhooks.snap.new b/src/optimizer/core/src/snapshots/qwik_core__test__example_implicit_qhooks.snap similarity index 92% rename from src/optimizer/core/src/snapshots/qwik_core__test__example_implicit_qhooks.snap.new rename to src/optimizer/core/src/snapshots/qwik_core__test__example_implicit_qhooks.snap index 31a3376d444..c8dcbf8e0ab 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__example_implicit_qhooks.snap.new +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_implicit_qhooks.snap @@ -66,7 +66,6 @@ export const Header_onRender = /*#__PURE__*/ qHook(()=>{ "localDecl": [], "localIdents": [ "console", - "log", "qHook" ] }, @@ -77,9 +76,7 @@ export const Header_onRender = /*#__PURE__*/ qHook(()=>{ "canonicalFilename": "h_test_header_otherthing", "localDecl": [], "localIdents": [ - "console", - "log", - "qHook" + "console" ] }, { @@ -92,9 +89,7 @@ export const Header_onRender = /*#__PURE__*/ qHook(()=>{ ], "localIdents": [ "console", - "ctx", - "log", - "qHook" + "ctx" ] }, { @@ -106,15 +101,8 @@ export const Header_onRender = /*#__PURE__*/ qHook(()=>{ "ctx" ], "localIdents": [ - "click", "console", - "ctx", - "div", - "import", - "log", - "on", - "prop", - "qHook" + "ctx" ] } ] diff --git a/src/optimizer/core/src/transform.rs b/src/optimizer/core/src/transform.rs index f1dd9b2c933..51a7460001c 100644 --- a/src/optimizer/core/src/transform.rs +++ b/src/optimizer/core/src/transform.rs @@ -335,13 +335,11 @@ impl<'a> Fold for HookTransform<'a> { match node.name { ast::JSXAttrName::Ident(ref ident) => { let ident_name = ident.sym.to_string(); - println!("{}", ident_name); self.stack_ctxt.push(ident_name); } ast::JSXAttrName::JSXNamespacedName(ref namespaced) => { let ns_name = namespaced.ns.sym.as_ref(); let ident_name = [ns_name, namespaced.name.sym.as_ref()].concat(); - println!("namespaced: {}", &ident_name); self.stack_ctxt.push(ident_name); is_listener = ns_name == "on"; From 9b4073b9b86b553e02471df70fa7cedf191351b2 Mon Sep 17 00:00:00 2001 From: Manu MA Date: Thu, 30 Dec 2021 10:47:52 +0100 Subject: [PATCH 3/5] Update src/optimizer/core/src/collector.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Armando Pérez Marqués --- src/optimizer/core/src/collector.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/optimizer/core/src/collector.rs b/src/optimizer/core/src/collector.rs index 7a9021d41c0..94a3f2681a5 100644 --- a/src/optimizer/core/src/collector.rs +++ b/src/optimizer/core/src/collector.rs @@ -437,8 +437,8 @@ impl Visit for HookCollect { fn visit_jsx_opening_element(&mut self, node: &ast::JSXOpeningElement) { let mut stacked = false; if let ast::JSXElementName::Ident(ref ident) = node.name { - let ident_name = ident.sym.as_ref().chars().next().unwrap(); - if let 'A'..='Z' = ident_name { + let ident_name = ident.sym.as_ref().chars().next(); + if let Some('A'..='Z') = ident_name { self.expr_ctxt.push(ExprOrSkip::Expr); } else { self.expr_ctxt.push(ExprOrSkip::Skip); From dc5efbdbdad80e6ffe499f59499515716f00b9f6 Mon Sep 17 00:00:00 2001 From: Manu MA Date: Tue, 4 Jan 2022 16:54:42 +0100 Subject: [PATCH 4/5] =?UTF-8?q?=F0=9F=8D=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/optimizer/core/benches/transform.rs | 2 +- src/optimizer/core/src/code_move.rs | 26 +- src/optimizer/core/src/collector.rs | 8 +- src/optimizer/core/src/entry_strategy.rs | 14 +- src/optimizer/core/src/lib.rs | 1 + .../snapshots/qwik_core__test__example_1.snap | 26 +- .../qwik_core__test__example_10.snap | 12 +- .../qwik_core__test__example_11.snap | 25 +- .../qwik_core__test__example_12.snap | 9 +- .../qwik_core__test__example_13.snap | 9 +- .../snapshots/qwik_core__test__example_2.snap | 26 +- .../snapshots/qwik_core__test__example_3.snap | 26 +- .../snapshots/qwik_core__test__example_4.snap | 26 +- .../snapshots/qwik_core__test__example_5.snap | 28 +- .../snapshots/qwik_core__test__example_6.snap | 10 +- .../snapshots/qwik_core__test__example_7.snap | 41 +- .../snapshots/qwik_core__test__example_8.snap | 13 +- .../snapshots/qwik_core__test__example_9.snap | 12 +- ...e__test__example_functional_component.snap | 75 ++++ ...k_core__test__example_implicit_qhooks.snap | 24 +- .../snapshots/qwik_core__test__issue_118.snap | 7 +- src/optimizer/core/src/test.rs | 6 +- src/optimizer/core/src/transform.rs | 350 +++++++++++------- src/optimizer/core/src/words.rs | 10 + 24 files changed, 481 insertions(+), 305 deletions(-) create mode 100644 src/optimizer/core/src/snapshots/qwik_core__test__example_functional_component.snap create mode 100644 src/optimizer/core/src/words.rs diff --git a/src/optimizer/core/benches/transform.rs b/src/optimizer/core/benches/transform.rs index e5549c696be..9656989d766 100644 --- a/src/optimizer/core/benches/transform.rs +++ b/src/optimizer/core/benches/transform.rs @@ -7,7 +7,7 @@ fn transform_benchmark(b: &mut Criterion) { b.bench_function("transform", |b| { b.iter(|| { let code = r#" - import {qHook} from '@builderio/qwik'; + import {qHook} from '@builder.io/qwik'; const Header2 = qComponent({ diff --git a/src/optimizer/core/src/code_move.rs b/src/optimizer/core/src/code_move.rs index 017a24c6639..c0ac779503a 100644 --- a/src/optimizer/core/src/code_move.rs +++ b/src/optimizer/core/src/code_move.rs @@ -1,17 +1,19 @@ use crate::collector::{new_ident_from_id, GlobalCollect, Id, ImportKind}; use crate::parse::{emit_source_code, HookAnalysis, PathData, TransformModule, TransformOutput}; +use crate::transform::{create_internal_call, create_synthetic_wildcard_import}; +use crate::words::*; use std::collections::HashMap; use std::path::Path; use anyhow::{format_err, Context, Error}; use swc_atoms::JsWord; -use swc_common::comments::{Comments, SingleThreadedComments}; +use swc_common::comments::SingleThreadedComments; use swc_common::{sync::Lrc, SourceMap, DUMMY_SP}; use swc_ecmascript::ast; pub fn new_module( - expr: ast::CallExpr, + expr: Box, path: &PathData, name: &str, origin: &str, @@ -26,6 +28,12 @@ pub fn new_module( shebang: None, }; + // Inject qwik internal import + module.body.push(create_synthetic_wildcard_import( + &QWIK_INTERNAL, + &BUILDER_IO_QWIK, + )); + for id in local_idents { if let Some(import) = global.imports.get(id) { let specifier = match import.kind { @@ -142,11 +150,17 @@ pub fn fix_path, D: AsRef>( } fn create_named_export( - expr: ast::CallExpr, + expr: Box, name: &str, - comments: &SingleThreadedComments, + _comments: &SingleThreadedComments, ) -> ast::ModuleItem { - comments.add_pure_comment(expr.span.lo); + // comments.add_pure_comment(expr.span().lo); + + let call_expr = Box::new(ast::Expr::Call(create_internal_call( + &QHOOK_HANDLER, + vec![*expr], + None, + ))); ast::ModuleItem::ModuleDecl(ast::ModuleDecl::ExportDecl(ast::ExportDecl { span: DUMMY_SP, @@ -161,7 +175,7 @@ fn create_named_export( JsWord::from(name), DUMMY_SP, ))), - init: Some(Box::new(ast::Expr::Call(expr))), + init: Some(call_expr), }], }), })) diff --git a/src/optimizer/core/src/collector.rs b/src/optimizer/core/src/collector.rs index 94a3f2681a5..8b92e1d59b3 100644 --- a/src/optimizer/core/src/collector.rs +++ b/src/optimizer/core/src/collector.rs @@ -196,14 +196,12 @@ pub struct HookCollect { } impl HookCollect { - pub fn new(node: &ast::CallExpr) -> Self { - let mut collect = Self { + pub fn new() -> Self { + Self { local_decl: HashSet::new(), local_idents: HashSet::new(), expr_ctxt: vec![], - }; - node.visit_with(&mut collect); - collect + } } pub fn get_words(self) -> (Vec, Vec) { diff --git a/src/optimizer/core/src/entry_strategy.rs b/src/optimizer/core/src/entry_strategy.rs index 1e214f04d1d..64ab406162a 100644 --- a/src/optimizer/core/src/entry_strategy.rs +++ b/src/optimizer/core/src/entry_strategy.rs @@ -3,7 +3,7 @@ use crate::parse::PathData; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use swc_atoms::JsWord; -use swc_ecmascript::ast::CallExpr; +use swc_ecmascript::ast::Expr; use lazy_static::lazy_static; @@ -30,7 +30,7 @@ pub trait EntryPolicy: Send + Sync { location: &PathData, context: &[String], analytics: &HookCollect, - expr: &CallExpr, + expr: &Expr, ) -> Option; } @@ -44,7 +44,7 @@ impl EntryPolicy for SingleStrategy { _path: &PathData, _context: &[String], _analytics: &HookCollect, - _expr: &CallExpr, + _expr: &Expr, ) -> Option { Some(ENTRY_HOOKS.clone()) } @@ -60,7 +60,7 @@ impl EntryPolicy for PerHookStrategy { _path: &PathData, _context: &[String], _analytics: &HookCollect, - _expr: &CallExpr, + _expr: &Expr, ) -> Option { None } @@ -76,7 +76,7 @@ impl EntryPolicy for PerComponentStrategy { _path: &PathData, context: &[String], _analytics: &HookCollect, - _expr: &CallExpr, + _expr: &Expr, ) -> Option { context.first().map_or_else( || Some(ENTRY_HOOKS.clone()), @@ -95,7 +95,7 @@ impl EntryPolicy for SmartStrategy { _path: &PathData, context: &[String], _analytics: &HookCollect, - _expr: &CallExpr, + _expr: &Expr, ) -> Option { if context.iter().any(|h| h == "onMount") { return Some(ENTRY_SERVER.clone()); @@ -136,7 +136,7 @@ impl EntryPolicy for ManualStrategy { _path: &PathData, _context: &[String], _analytics: &HookCollect, - _expr: &CallExpr, + _expr: &Expr, ) -> Option { let entry = self.map.get(symbol); Some(match entry { diff --git a/src/optimizer/core/src/lib.rs b/src/optimizer/core/src/lib.rs index c4ec9675ad0..c236cfb4464 100644 --- a/src/optimizer/core/src/lib.rs +++ b/src/optimizer/core/src/lib.rs @@ -11,6 +11,7 @@ mod entry_strategy; mod parse; mod transform; mod utils; +mod words; #[cfg(feature = "parallel")] use rayon::prelude::*; diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_1.snap b/src/optimizer/core/src/snapshots/qwik_core__test__example_1.snap index 58185a2b594..28aaf6b185a 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__example_1.snap +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_1.snap @@ -17,28 +17,32 @@ const Header = qComponent({ ============================= test.tsx == +import * as __qwik__ from "@builder.io/qwik"; /*#__PURE__*/ qComponent({ - "onMount": qHook(()=>import("./h_test_header_onmount") + "onMount": __qwik__.qHook(()=>import("./h_test_header_onmount") , "Header_onMount"), - onRender: qHook(()=>import("./h_test_header_onrender") + onRender: __qwik__.qHook(()=>import("./h_test_header_onrender") , "Header_onRender") }); ============================= h_test_header_onmount.tsx (ENTRY POINT)== -export const Header_onMount = /*#__PURE__*/ qHook(()=>{ +import * as __qwik__ from "@builder.io/qwik"; +export const Header_onMount = __qwik__.qHook(()=>{ console.log("mount"); }); ============================= h_test_header_onrender_div_onclick.tsx (ENTRY POINT)== -export const Header_onRender_div_onClick = /*#__PURE__*/ qHook((ctx)=>console.log(ctx) +import * as __qwik__ from "@builder.io/qwik"; +export const Header_onRender_div_onClick = __qwik__.qHook((ctx)=>console.log(ctx) ); ============================= h_test_header_onrender.tsx (ENTRY POINT)== -export const Header_onRender = /*#__PURE__*/ qHook(()=>{ - return (
import("./h_test_header_onrender_div_onclick") +import * as __qwik__ from "@builder.io/qwik"; +export const Header_onRender = __qwik__.qHook(()=>{ + return (
import("./h_test_header_onrender_div_onclick") , "Header_onRender_div_onClick")}/>); }); @@ -52,8 +56,7 @@ export const Header_onRender = /*#__PURE__*/ qHook(()=>{ "canonicalFilename": "h_test_header_onmount", "localDecl": [], "localIdents": [ - "console", - "qHook" + "console" ] }, { @@ -66,8 +69,7 @@ export const Header_onRender = /*#__PURE__*/ qHook(()=>{ ], "localIdents": [ "console", - "ctx", - "qHook" + "ctx" ] }, { @@ -76,9 +78,7 @@ export const Header_onRender = /*#__PURE__*/ qHook(()=>{ "entry": null, "canonicalFilename": "h_test_header_onrender", "localDecl": [], - "localIdents": [ - "qHook" - ] + "localIdents": [] } ] diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_10.snap b/src/optimizer/core/src/snapshots/qwik_core__test__example_10.snap index 0b812bc8334..704ba85d3d1 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__example_10.snap +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_10.snap @@ -27,12 +27,14 @@ const Header = qHook((decl1, {decl2}, [decl3]) => { ============================= project/test.tsx == -qHook(()=>import("../h_test_header") +import * as __qwik__ from "@builder.io/qwik"; +__qwik__.qHook(()=>import("../h_test_header") , "Header"); ============================= h_test_header.tsx (ENTRY POINT)== -export const Header = /*#__PURE__*/ qHook((decl1, { decl2 }, [decl3])=>{ +import * as __qwik__ from "@builder.io/qwik"; +export const Header = __qwik__.qHook((decl1, { decl2 }, [decl3])=>{ const hola = ident1.no; ident2; const a = ident1 + ident3; @@ -73,9 +75,6 @@ export const Header = /*#__PURE__*/ qHook((decl1, { decl2 }, [decl3])=>{ "ident11" ], "localIdents": [ - "decl1", - "decl2", - "decl3", "ident1", "ident10", "ident11", @@ -87,8 +86,7 @@ export const Header = /*#__PURE__*/ qHook((decl1, { decl2 }, [decl3])=>{ "ident6", "ident7", "ident8", - "ident9", - "qHook" + "ident9" ] } ] diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_11.snap b/src/optimizer/core/src/snapshots/qwik_core__test__example_11.snap index 408c581a792..de66435300e 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__example_11.snap +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_11.snap @@ -30,28 +30,31 @@ export const App = qComponent({ ============================= project/test.tsx == +import * as __qwik__ from "@builder.io/qwik"; export const Header = /*#__PURE__*/ qComponent({ - onRender: qHook(()=>import("../entry_hooks") + onRender: __qwik__.qHook(()=>import("../entry_hooks") , "Header_onRender") }); export const App = /*#__PURE__*/ qComponent({ - onRender: qHook(()=>import("../entry_hooks") + onRender: __qwik__.qHook(()=>import("../entry_hooks") , "App_onRender") }); ============================= h_test_header_onrender_header_onclick.tsx == +import * as __qwik__ from "@builder.io/qwik"; import dep3 from "dep3/something"; -export const Header_onRender_Header_onClick = /*#__PURE__*/ qHook((ev)=>dep3(ev) +export const Header_onRender_Header_onClick = __qwik__.qHook((ev)=>dep3(ev) ); ============================= h_test_header_onrender.tsx == +import * as __qwik__ from "@builder.io/qwik"; import { Header } from "./project/test"; import { bar as bbar } from "./state"; import * as dep2 from "dep2"; -export const Header_onRender = /*#__PURE__*/ qHook(()=>{ - return (
import("../entry_hooks") +export const Header_onRender = __qwik__.qHook(()=>{ + return (
import("../entry_hooks") , "Header_onRender_Header_onClick")}> {dep2.stuff()}{bbar()} @@ -61,9 +64,10 @@ export const Header_onRender = /*#__PURE__*/ qHook(()=>{ ============================= h_test_app_onrender.tsx == +import * as __qwik__ from "@builder.io/qwik"; import { Header } from "./project/test"; import { foo } from "./state"; -export const App_onRender = /*#__PURE__*/ qHook(()=>{ +export const App_onRender = __qwik__.qHook(()=>{ return (
{foo()}
); }); @@ -86,8 +90,7 @@ export { App_onRender } from "./h_test_app_onrender"; ], "localIdents": [ "dep3", - "ev", - "qHook" + "ev" ] }, { @@ -99,8 +102,7 @@ export { App_onRender } from "./h_test_app_onrender"; "localIdents": [ "Header", "bbar", - "dep2", - "qHook" + "dep2" ] }, { @@ -111,8 +113,7 @@ export { App_onRender } from "./h_test_app_onrender"; "localDecl": [], "localIdents": [ "Header", - "foo", - "qHook" + "foo" ] } ] diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_12.snap b/src/optimizer/core/src/snapshots/qwik_core__test__example_12.snap index 9684e790059..2d5806c565b 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__example_12.snap +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_12.snap @@ -13,14 +13,16 @@ export const Header = qComponent({ ============================= project/test.tsx == +import * as __qwik__ from "@builder.io/qwik"; export const Header = /*#__PURE__*/ qComponent({ - onRender: qHook(()=>import("../entry_hooks") + onRender: __qwik__.qHook(()=>import("../entry_hooks") , "sym2") }); ============================= h_test_sym2.tsx == -export const sym2 = /*#__PURE__*/ qHook(()=>console.log("hello sym2") +import * as __qwik__ from "@builder.io/qwik"; +export const sym2 = __qwik__.qHook(()=>console.log("hello sym2") ); ============================= entry_hooks.ts (ENTRY POINT)== @@ -37,8 +39,7 @@ export { sym2 } from "./h_test_sym2"; "canonicalFilename": "h_test_sym2", "localDecl": [], "localIdents": [ - "console", - "qHook" + "console" ] } ] diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_13.snap b/src/optimizer/core/src/snapshots/qwik_core__test__example_13.snap index 585d5c68819..a14b706265f 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__example_13.snap +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_13.snap @@ -13,14 +13,16 @@ export const Header = qComponent({ ============================= project/test.tsx == +import * as __qwik__ from "@builder.io/qwik"; export const Header = /*#__PURE__*/ qComponent({ - onRender: qHook(()=>import("../entry_hooks") + onRender: __qwik__.qHook(()=>import("../entry_hooks") , "Header_onRender") }); ============================= h_test_header_onrender.tsx == -export const Header_onRender = /*#__PURE__*/ qHook(()=>console.log("hello sym2") +import * as __qwik__ from "@builder.io/qwik"; +export const Header_onRender = __qwik__.qHook(()=>console.log("hello sym2") ); ============================= entry_hooks.ts (ENTRY POINT)== @@ -37,8 +39,7 @@ export { Header_onRender } from "./h_test_header_onrender"; "canonicalFilename": "h_test_header_onrender", "localDecl": [], "localIdents": [ - "console", - "qHook" + "console" ] } ] diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_2.snap b/src/optimizer/core/src/snapshots/qwik_core__test__example_2.snap index 75b1c4b21a1..c8a77d0c56e 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__example_2.snap +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_2.snap @@ -17,28 +17,32 @@ export const Header = qComponent({ ============================= test.tsx == +import * as __qwik__ from "@builder.io/qwik"; export const Header = /*#__PURE__*/ qComponent({ - "onMount": qHook(()=>import("./h_test_header_onmount") + "onMount": __qwik__.qHook(()=>import("./h_test_header_onmount") , "Header_onMount"), - onRender: qHook(()=>import("./h_test_header_onrender") + onRender: __qwik__.qHook(()=>import("./h_test_header_onrender") , "Header_onRender") }); ============================= h_test_header_onmount.tsx (ENTRY POINT)== -export const Header_onMount = /*#__PURE__*/ qHook(()=>{ +import * as __qwik__ from "@builder.io/qwik"; +export const Header_onMount = __qwik__.qHook(()=>{ console.log("mount"); }); ============================= h_test_header_onrender_div_onclick.tsx (ENTRY POINT)== -export const Header_onRender_div_onClick = /*#__PURE__*/ qHook((ctx)=>console.log(ctx) +import * as __qwik__ from "@builder.io/qwik"; +export const Header_onRender_div_onClick = __qwik__.qHook((ctx)=>console.log(ctx) ); ============================= h_test_header_onrender.tsx (ENTRY POINT)== -export const Header_onRender = /*#__PURE__*/ qHook(()=>{ - return (
import("./h_test_header_onrender_div_onclick") +import * as __qwik__ from "@builder.io/qwik"; +export const Header_onRender = __qwik__.qHook(()=>{ + return (
import("./h_test_header_onrender_div_onclick") , "Header_onRender_div_onClick")}/>); }); @@ -52,8 +56,7 @@ export const Header_onRender = /*#__PURE__*/ qHook(()=>{ "canonicalFilename": "h_test_header_onmount", "localDecl": [], "localIdents": [ - "console", - "qHook" + "console" ] }, { @@ -66,8 +69,7 @@ export const Header_onRender = /*#__PURE__*/ qHook(()=>{ ], "localIdents": [ "console", - "ctx", - "qHook" + "ctx" ] }, { @@ -76,9 +78,7 @@ export const Header_onRender = /*#__PURE__*/ qHook(()=>{ "entry": null, "canonicalFilename": "h_test_header_onrender", "localDecl": [], - "localIdents": [ - "qHook" - ] + "localIdents": [] } ] diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_3.snap b/src/optimizer/core/src/snapshots/qwik_core__test__example_3.snap index 4ffc64a3cb7..c7e07978b18 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__example_3.snap +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_3.snap @@ -20,11 +20,12 @@ export const App = () => { ============================= test.tsx == +import * as __qwik__ from "@builder.io/qwik"; export const App = ()=>{ const Header = /*#__PURE__*/ qComponent({ - "onMount": qHook(()=>import("./h_test_app_header_onmount") + "onMount": __qwik__.qHook(()=>import("./h_test_app_header_onmount") , "App_Header_onMount"), - onRender: qHook(()=>import("./h_test_app_header_onrender") + onRender: __qwik__.qHook(()=>import("./h_test_app_header_onrender") , "App_Header_onRender") }); return Header; @@ -32,19 +33,22 @@ export const App = ()=>{ ============================= h_test_app_header_onmount.tsx (ENTRY POINT)== -export const App_Header_onMount = /*#__PURE__*/ qHook(()=>{ +import * as __qwik__ from "@builder.io/qwik"; +export const App_Header_onMount = __qwik__.qHook(()=>{ console.log("mount"); }); ============================= h_test_app_header_onrender_div_onclick.tsx (ENTRY POINT)== -export const App_Header_onRender_div_onClick = /*#__PURE__*/ qHook((ctx)=>console.log(ctx) +import * as __qwik__ from "@builder.io/qwik"; +export const App_Header_onRender_div_onClick = __qwik__.qHook((ctx)=>console.log(ctx) ); ============================= h_test_app_header_onrender.tsx (ENTRY POINT)== -export const App_Header_onRender = /*#__PURE__*/ qHook(()=>{ - return (
import("./h_test_app_header_onrender_div_onclick") +import * as __qwik__ from "@builder.io/qwik"; +export const App_Header_onRender = __qwik__.qHook(()=>{ + return (
import("./h_test_app_header_onrender_div_onclick") , "App_Header_onRender_div_onClick")}/>); }); @@ -58,8 +62,7 @@ export const App_Header_onRender = /*#__PURE__*/ qHook(()=>{ "canonicalFilename": "h_test_app_header_onmount", "localDecl": [], "localIdents": [ - "console", - "qHook" + "console" ] }, { @@ -72,8 +75,7 @@ export const App_Header_onRender = /*#__PURE__*/ qHook(()=>{ ], "localIdents": [ "console", - "ctx", - "qHook" + "ctx" ] }, { @@ -82,9 +84,7 @@ export const App_Header_onRender = /*#__PURE__*/ qHook(()=>{ "entry": null, "canonicalFilename": "h_test_app_header_onrender", "localDecl": [], - "localIdents": [ - "qHook" - ] + "localIdents": [] } ] diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_4.snap b/src/optimizer/core/src/snapshots/qwik_core__test__example_4.snap index 12eea2bd7f2..33199c7cfb3 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__example_4.snap +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_4.snap @@ -20,11 +20,12 @@ export function App() { ============================= test.tsx == +import * as __qwik__ from "@builder.io/qwik"; export function App() { const Header = /*#__PURE__*/ qComponent({ - "onMount": qHook(()=>import("./h_test_app_header_onmount") + "onMount": __qwik__.qHook(()=>import("./h_test_app_header_onmount") , "App_Header_onMount"), - onRender: qHook(()=>import("./h_test_app_header_onrender") + onRender: __qwik__.qHook(()=>import("./h_test_app_header_onrender") , "App_Header_onRender") }); return Header; @@ -32,19 +33,22 @@ export function App() { ============================= h_test_app_header_onmount.tsx (ENTRY POINT)== -export const App_Header_onMount = /*#__PURE__*/ qHook(()=>{ +import * as __qwik__ from "@builder.io/qwik"; +export const App_Header_onMount = __qwik__.qHook(()=>{ console.log("mount"); }); ============================= h_test_app_header_onrender_div_onclick.tsx (ENTRY POINT)== -export const App_Header_onRender_div_onClick = /*#__PURE__*/ qHook((ctx)=>console.log(ctx) +import * as __qwik__ from "@builder.io/qwik"; +export const App_Header_onRender_div_onClick = __qwik__.qHook((ctx)=>console.log(ctx) ); ============================= h_test_app_header_onrender.tsx (ENTRY POINT)== -export const App_Header_onRender = /*#__PURE__*/ qHook(()=>{ - return (
import("./h_test_app_header_onrender_div_onclick") +import * as __qwik__ from "@builder.io/qwik"; +export const App_Header_onRender = __qwik__.qHook(()=>{ + return (
import("./h_test_app_header_onrender_div_onclick") , "App_Header_onRender_div_onClick")}/>); }); @@ -58,8 +62,7 @@ export const App_Header_onRender = /*#__PURE__*/ qHook(()=>{ "canonicalFilename": "h_test_app_header_onmount", "localDecl": [], "localIdents": [ - "console", - "qHook" + "console" ] }, { @@ -72,8 +75,7 @@ export const App_Header_onRender = /*#__PURE__*/ qHook(()=>{ ], "localIdents": [ "console", - "ctx", - "qHook" + "ctx" ] }, { @@ -82,9 +84,7 @@ export const App_Header_onRender = /*#__PURE__*/ qHook(()=>{ "entry": null, "canonicalFilename": "h_test_app_header_onrender", "localDecl": [], - "localIdents": [ - "qHook" - ] + "localIdents": [] } ] diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_5.snap b/src/optimizer/core/src/snapshots/qwik_core__test__example_5.snap index 4ac3ddcfb43..13fae13de02 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__example_5.snap +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_5.snap @@ -19,30 +19,34 @@ export const Header = qComponent({ ============================= test.tsx == +import * as __qwik__ from "@builder.io/qwik"; export const Header = /*#__PURE__*/ qComponent({ - onRender: qHook(()=>import("./h_test_header_onrender") + onRender: __qwik__.qHook(()=>import("./h_test_header_onrender") , "Header_onRender") }); ============================= h_test_header_onrender_div_onclick.tsx (ENTRY POINT)== -export const Header_onRender_div_onClick = /*#__PURE__*/ qHook((ctx)=>console.log("1") +import * as __qwik__ from "@builder.io/qwik"; +export const Header_onRender_div_onClick = __qwik__.qHook((ctx)=>console.log("1") ); ============================= h_test_header_onrender_div_onclick2.tsx (ENTRY POINT)== -export const Header_onRender_div_onClick2 = /*#__PURE__*/ qHook((ctx)=>console.log("2") +import * as __qwik__ from "@builder.io/qwik"; +export const Header_onRender_div_onClick2 = __qwik__.qHook((ctx)=>console.log("2") ); ============================= h_test_header_onrender.tsx (ENTRY POINT)== -export const Header_onRender = /*#__PURE__*/ qHook(()=>{ +import * as __qwik__ from "@builder.io/qwik"; +export const Header_onRender = __qwik__.qHook(()=>{ return (<> -
import("./h_test_header_onrender_div_onclick") +
import("./h_test_header_onrender_div_onclick") , "Header_onRender_div_onClick")}/> -
import("./h_test_header_onrender_div_onclick2") +
import("./h_test_header_onrender_div_onclick2") , "Header_onRender_div_onClick2")}/> ); @@ -60,9 +64,7 @@ export const Header_onRender = /*#__PURE__*/ qHook(()=>{ "ctx" ], "localIdents": [ - "console", - "ctx", - "qHook" + "console" ] }, { @@ -74,9 +76,7 @@ export const Header_onRender = /*#__PURE__*/ qHook(()=>{ "ctx" ], "localIdents": [ - "console", - "ctx", - "qHook" + "console" ] }, { @@ -85,9 +85,7 @@ export const Header_onRender = /*#__PURE__*/ qHook(()=>{ "entry": null, "canonicalFilename": "h_test_header_onrender", "localDecl": [], - "localIdents": [ - "qHook" - ] + "localIdents": [] } ] diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_6.snap b/src/optimizer/core/src/snapshots/qwik_core__test__example_6.snap index 9efdd140f57..566c0caf4e7 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__example_6.snap +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_6.snap @@ -10,12 +10,14 @@ export const sym1 = qHook((ctx) => console.log("1")); ============================= test.tsx == -export const sym1 = qHook(()=>import("./h_test_sym1") +import * as __qwik__ from "@builder.io/qwik"; +export const sym1 = __qwik__.qHook(()=>import("./h_test_sym1") , "sym1"); ============================= h_test_sym1.tsx (ENTRY POINT)== -export const sym1 = /*#__PURE__*/ qHook((ctx)=>console.log("1") +import * as __qwik__ from "@builder.io/qwik"; +export const sym1 = __qwik__.qHook((ctx)=>console.log("1") ); == HOOKS == @@ -30,9 +32,7 @@ export const sym1 = /*#__PURE__*/ qHook((ctx)=>console.log("1") "ctx" ], "localIdents": [ - "console", - "ctx", - "qHook" + "console" ] } ] diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_7.snap b/src/optimizer/core/src/snapshots/qwik_core__test__example_7.snap index 5d9ab1b9670..00dcf055e59 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__example_7.snap +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_7.snap @@ -6,7 +6,7 @@ expression: output ==INPUT== -import {qHook} from '@builderio/qwik'; +import {qHook} from '@builder.io/qwik'; const Header = qComponent({ @@ -27,43 +27,43 @@ const App = qComponent({ }); ============================= test.tsx == -import { qHook } from '@builderio/qwik'; +import * as __qwik__ from "@builder.io/qwik"; /*#__PURE__*/ qComponent({ - "onMount": qHook(()=>import("./h_test_header_onmount") + "onMount": __qwik__.qHook(()=>import("./h_test_header_onmount") , "Header_onMount"), - onRender: qHook(()=>import("./h_test_header_onrender") + onRender: __qwik__.qHook(()=>import("./h_test_header_onrender") , "Header_onRender") }); /*#__PURE__*/ qComponent({ - onRender: qHook(()=>import("./h_test_app_onrender") + onRender: __qwik__.qHook(()=>import("./h_test_app_onrender") , "App_onRender") }); ============================= h_test_header_onmount.tsx (ENTRY POINT)== -import { qHook } from "@builderio/qwik"; -export const Header_onMount = /*#__PURE__*/ qHook(()=>{ +import * as __qwik__ from "@builder.io/qwik"; +export const Header_onMount = __qwik__.qHook(()=>{ console.log("mount"); }); ============================= h_test_header_onrender_div_onclick.tsx (ENTRY POINT)== -import { qHook } from "@builderio/qwik"; -export const Header_onRender_div_onClick = /*#__PURE__*/ qHook((ctx)=>console.log(ctx) +import * as __qwik__ from "@builder.io/qwik"; +export const Header_onRender_div_onClick = __qwik__.qHook((ctx)=>console.log(ctx) ); ============================= h_test_header_onrender.tsx (ENTRY POINT)== -import { qHook } from "@builderio/qwik"; -export const Header_onRender = /*#__PURE__*/ qHook(()=>{ - return (
import("./h_test_header_onrender_div_onclick") +import * as __qwik__ from "@builder.io/qwik"; +export const Header_onRender = __qwik__.qHook(()=>{ + return (
import("./h_test_header_onrender_div_onclick") , "Header_onRender_div_onClick")}/>); }); ============================= h_test_app_onrender.tsx (ENTRY POINT)== -import { qHook } from "@builderio/qwik"; -export const App_onRender = /*#__PURE__*/ qHook(()=>{ +import * as __qwik__ from "@builder.io/qwik"; +export const App_onRender = __qwik__.qHook(()=>{ return (
); }); @@ -77,8 +77,7 @@ export const App_onRender = /*#__PURE__*/ qHook(()=>{ "canonicalFilename": "h_test_header_onmount", "localDecl": [], "localIdents": [ - "console", - "qHook" + "console" ] }, { @@ -91,8 +90,7 @@ export const App_onRender = /*#__PURE__*/ qHook(()=>{ ], "localIdents": [ "console", - "ctx", - "qHook" + "ctx" ] }, { @@ -101,9 +99,7 @@ export const App_onRender = /*#__PURE__*/ qHook(()=>{ "entry": null, "canonicalFilename": "h_test_header_onrender", "localDecl": [], - "localIdents": [ - "qHook" - ] + "localIdents": [] }, { "origin": "test.tsx", @@ -112,8 +108,7 @@ export const App_onRender = /*#__PURE__*/ qHook(()=>{ "canonicalFilename": "h_test_app_onrender", "localDecl": [], "localIdents": [ - "Header", - "qHook" + "Header" ] } ] diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_8.snap b/src/optimizer/core/src/snapshots/qwik_core__test__example_8.snap index a989062f8c9..6600bc3525e 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__example_8.snap +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_8.snap @@ -6,7 +6,7 @@ expression: output ==INPUT== -import {qHook} from '@builderio/qwik'; +import {qHook} from '@builder.io/qwik'; const Header = qComponent({ onRender: qHook((hola) => { @@ -21,16 +21,16 @@ const Header = qComponent({ ============================= test.tsx == -import { qHook } from '@builderio/qwik'; +import * as __qwik__ from "@builder.io/qwik"; /*#__PURE__*/ qComponent({ - onRender: qHook(()=>import("./h_test_header_onrender") + onRender: __qwik__.qHook(()=>import("./h_test_header_onrender") , "Header_onRender") }); ============================= h_test_header_onrender.tsx (ENTRY POINT)== -import { qHook } from "@builderio/qwik"; -export const Header_onRender = /*#__PURE__*/ qHook((hola)=>{ +import * as __qwik__ from "@builder.io/qwik"; +export const Header_onRender = __qwik__.qHook((hola)=>{ const hola = this; const { something , styff } = hola; const hello = hola.nothere.stuff[global]; @@ -54,8 +54,7 @@ export const Header_onRender = /*#__PURE__*/ qHook((hola)=>{ "localIdents": [ "Header", "global", - "hola", - "qHook" + "hola" ] } ] diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_9.snap b/src/optimizer/core/src/snapshots/qwik_core__test__example_9.snap index 0fb658f2165..abe12e6c37f 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__example_9.snap +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_9.snap @@ -1,5 +1,5 @@ --- -source: src/test.rs +source: src/optimizer/core/src/test.rs expression: output --- @@ -20,12 +20,14 @@ const Header = qHook((decl1, {decl2}, [decl3]) => { ============================= test.tsx == -qHook(()=>import("./h_test_header") +import * as __qwik__ from "@builder.io/qwik"; +__qwik__.qHook(()=>import("./h_test_header") , "Header"); ============================= h_test_header.tsx (ENTRY POINT)== -export const Header = /*#__PURE__*/ qHook((decl1, { decl2 }, [decl3])=>{ +import * as __qwik__ from "@builder.io/qwik"; +export const Header = __qwik__.qHook((decl1, { decl2 }, [decl3])=>{ const { decl4 , key: decl5 } = this; let [decl6, ...decl7] = stuff; const decl8 = 1, decl9; @@ -73,10 +75,6 @@ export const Header = /*#__PURE__*/ qHook((decl1, { decl2 }, [decl3])=>{ "decl9" ], "localIdents": [ - "decl1", - "decl2", - "decl3", - "qHook", "stuff" ] } diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_functional_component.snap b/src/optimizer/core/src/snapshots/qwik_core__test__example_functional_component.snap new file mode 100644 index 00000000000..b86fa05589e --- /dev/null +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_functional_component.snap @@ -0,0 +1,75 @@ +--- +source: src/optimizer/core/src/test.rs +expression: output + +--- +==INPUT== + + +const Header = qComponent(() => { + const thing = useState(); + const {foo, bar} = foo(); + + return ( +
+ ) +}); + +============================= test.tsx == + +/*#__PURE__*/ qComponent({ + onMount: qHook(()=>import("./h_test_header_onmount") + , "Header_onMount"), + onRender: qHook(()=>import("./h_test_header_onrender") + , "Header_onRender") +}); + +============================= h_test_header_onmount.tsx (ENTRY POINT)== + +export const Header_onMount = qHook(()=>{ + const _closure = useClosure(); + const thing = useState(); + _closure.thing = thing; + const { foo , bar } = foo(); + _closure.bar = bar; + _closure.foo = foo; +}); + +============================= h_test_header_onrender.tsx (ENTRY POINT)== + +export const Header_onRender = qHook(()=>{ + return (
); +}); + +== HOOKS == + +[ + { + "origin": "test.tsx", + "name": "Header_onMount", + "entry": null, + "canonicalFilename": "h_test_header_onmount", + "localDecl": [ + "_closure", + "bar", + "foo", + "thing" + ], + "localIdents": [ + "foo", + "useState" + ] + }, + { + "origin": "test.tsx", + "name": "Header_onRender", + "entry": null, + "canonicalFilename": "h_test_header_onrender", + "localDecl": [], + "localIdents": [] + } +] + +== DIAGNOSTICS == + +[] diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__example_implicit_qhooks.snap b/src/optimizer/core/src/snapshots/qwik_core__test__example_implicit_qhooks.snap index c8dcbf8e0ab..be14453d67a 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__example_implicit_qhooks.snap +++ b/src/optimizer/core/src/snapshots/qwik_core__test__example_implicit_qhooks.snap @@ -21,37 +21,42 @@ const Header = qComponent({ ============================= test.tsx == +import * as __qwik__ from "@builder.io/qwik"; /*#__PURE__*/ qComponent({ - "onMount": qHook(()=>import("./h_test_header_onmount") + "onMount": __qwik__.qHook(()=>import("./h_test_header_onmount") , "Header_onMount"), - "otherThing": qHook(()=>import("./h_test_header_otherthing") + "otherThing": __qwik__.qHook(()=>import("./h_test_header_otherthing") , "Header_otherThing"), - onRender: qHook(()=>import("./h_test_header_onrender") + onRender: __qwik__.qHook(()=>import("./h_test_header_onrender") , "Header_onRender") }); ============================= h_test_header_onmount.tsx (ENTRY POINT)== -export const Header_onMount = /*#__PURE__*/ qHook(()=>{ +import * as __qwik__ from "@builder.io/qwik"; +export const Header_onMount = __qwik__.qHook(()=>{ console.log("mount"); }); ============================= h_test_header_otherthing.tsx (ENTRY POINT)== -export const Header_otherThing = /*#__PURE__*/ qHook(()=>{ +import * as __qwik__ from "@builder.io/qwik"; +export const Header_otherThing = __qwik__.qHook(()=>{ console.log("otherThing"); }); ============================= h_test_header_onrender_div_onclick.tsx (ENTRY POINT)== -export const Header_onRender_div_onclick = /*#__PURE__*/ qHook((ctx)=>console.log(ctx) +import * as __qwik__ from "@builder.io/qwik"; +export const Header_onRender_div_onclick = __qwik__.qHook((ctx)=>console.log(ctx) ); ============================= h_test_header_onrender.tsx (ENTRY POINT)== -export const Header_onRender = /*#__PURE__*/ qHook(()=>{ +import * as __qwik__ from "@builder.io/qwik"; +export const Header_onRender = __qwik__.qHook(()=>{ return (
console.log(ctx) - } on:click={qHook(()=>import("./h_test_header_onrender_div_onclick") + } on:click={__qwik__.qHook(()=>import("./h_test_header_onrender_div_onclick") , "Header_onRender_div_onclick")}/>); }); @@ -65,8 +70,7 @@ export const Header_onRender = /*#__PURE__*/ qHook(()=>{ "canonicalFilename": "h_test_header_onmount", "localDecl": [], "localIdents": [ - "console", - "qHook" + "console" ] }, { diff --git a/src/optimizer/core/src/snapshots/qwik_core__test__issue_118.snap b/src/optimizer/core/src/snapshots/qwik_core__test__issue_118.snap index d93fbbab8e4..f5db38cc34a 100644 --- a/src/optimizer/core/src/snapshots/qwik_core__test__issue_118.snap +++ b/src/optimizer/core/src/snapshots/qwik_core__test__issue_118.snap @@ -6,7 +6,7 @@ expression: output ==INPUT== -import {qHook, h} from '@builderio/qwik'; +import {qHook, h} from '@builder.io/qwik'; import thing from 'lib'; import * as all from 'lib'; import {s as se} from 'lib'; @@ -23,10 +23,10 @@ export const Footer = qComponent(); ============================= project/test.js == -import{qHook as a,h as b}from"@builderio/qwik";export const Header=/*#__PURE__*/ qComponent({onMount:/*#__PURE__*/ b("div",null),onRender:a(()=>import("../entry_hooks"),"Header_onRender")});export const Footer=/*#__PURE__*/ qComponent(); +import*as a from"@builder.io/qwik";import{h as b}from"@builder.io/qwik";export const Header=/*#__PURE__*/ qComponent({onMount:/*#__PURE__*/ b("div",null),onRender:a.qHook(()=>import("../entry_hooks"),"Header_onRender")});export const Footer=/*#__PURE__*/ qComponent(); ============================= h_test_header_onrender.js == -import{Footer as a}from"./project/test";import*as b from"lib";import{h as c}from"@builderio/qwik";import{qHook as d}from"@builderio/qwik";import{s as e}from"lib";import f from"lib";export const Header_onRender=/*#__PURE__*/ d(()=>c(a,null,f,b(),e())); +import*as a from"@builder.io/qwik";import{Footer as b}from"./project/test";import*as c from"lib";import{h as d}from"@builder.io/qwik";import{s as e}from"lib";import f from"lib";export const Header_onRender=a.qHook(()=>d(b,null,f,c(),e())); ============================= entry_hooks.js (ENTRY POINT)== export { Header_onRender } from "./h_test_header_onrender"; @@ -44,7 +44,6 @@ export { Header_onRender } from "./h_test_header_onrender"; "Footer", "all", "h", - "qHook", "se", "thing" ] diff --git a/src/optimizer/core/src/test.rs b/src/optimizer/core/src/test.rs index a6aed9366e4..6dd2683265a 100644 --- a/src/optimizer/core/src/test.rs +++ b/src/optimizer/core/src/test.rs @@ -127,7 +127,7 @@ fn example_7() { test_input( "test.tsx", r#" -import {qHook} from '@builderio/qwik'; +import {qHook} from '@builder.io/qwik'; const Header = qComponent({ @@ -157,7 +157,7 @@ fn example_8() { test_input( "test.tsx", r#" -import {qHook} from '@builderio/qwik'; +import {qHook} from '@builder.io/qwik'; const Header = qComponent({ onRender: qHook((hola) => { @@ -336,7 +336,7 @@ fn issue_118() { test_input( "project/test.tsx", r#" -import {qHook, h} from '@builderio/qwik'; +import {qHook, h} from '@builder.io/qwik'; import thing from 'lib'; import * as all from 'lib'; import {s as se} from 'lib'; diff --git a/src/optimizer/core/src/transform.rs b/src/optimizer/core/src/transform.rs index 51a7460001c..ac55d5bfb85 100644 --- a/src/optimizer/core/src/transform.rs +++ b/src/optimizer/core/src/transform.rs @@ -4,27 +4,24 @@ use crate::code_move::fix_path; use crate::collector::{HookCollect, Id}; use crate::entry_strategy::EntryPolicy; use crate::parse::PathData; +use crate::words::*; + use anyhow::{bail, Error}; use lazy_static::lazy_static; use regex::Regex; use std::sync::{Arc, Mutex}; use swc_atoms::JsWord; use swc_common::comments::{Comments, SingleThreadedComments}; -use swc_common::{errors::HANDLER, Span, DUMMY_SP}; +use swc_common::{errors::HANDLER, Mark, DUMMY_SP}; use swc_ecmascript::ast; -use swc_ecmascript::visit::{noop_fold_type, Fold, FoldWith}; - -lazy_static! { - static ref QHOOK: JsWord = JsWord::from("qHook"); - static ref QCOMPONENT: JsWord = JsWord::from("qComponent"); -} +use swc_ecmascript::visit::{fold_expr, noop_fold_type, visit_expr, Fold, FoldWith}; #[derive(Debug)] pub struct Hook { pub entry: Option, pub canonical_filename: String, pub name: String, - pub expr: ast::CallExpr, + pub expr: Box, pub local_decl: Vec, pub local_idents: Vec, pub origin: String, @@ -62,6 +59,7 @@ pub struct HookTransform<'a> { hooks: &'a mut Vec, path_data: &'a PathData, + qhook_mark: swc_common::Mark, comments: Option<&'a SingleThreadedComments>, entry_policy: &'a dyn EntryPolicy, @@ -80,6 +78,7 @@ impl<'a> HookTransform<'a> { stack_ctxt: Vec::with_capacity(16), position_ctxt: Vec::with_capacity(16), hooks, + qhook_mark: Mark::fresh(Mark::root()), root_sym: None, comments, entry_policy, @@ -133,102 +132,108 @@ impl<'a> HookTransform<'a> { } fn create_synthetic_qhook(&mut self, fn_expr: ast::ArrowExpr) -> ast::CallExpr { - ast::CallExpr { - callee: ast::ExprOrSuper::Expr(Box::new(ast::Expr::Ident(ast::Ident::new( - QHOOK.clone(), - DUMMY_SP, - )))), - span: fn_expr.span, - type_args: None, - args: vec![ast::ExprOrSpread { - spread: None, - expr: Box::new(ast::Expr::Arrow(fn_expr)), - }], - } + create_internal_call( + &QHOOK, + vec![ast::Expr::Arrow(fn_expr)], + Some(self.qhook_mark), + ) } - fn handle_qhook(&mut self, node: ast::CallExpr, qhook_span: Span) -> ast::CallExpr { - let mut node = node; + fn handle_qhook(&mut self, node: ast::CallExpr) -> ast::CallExpr { let mut user_symbol = None; - if let Some(second_arg) = node.args.get(1) { - if let ast::Expr::Lit(ast::Lit::Str(ref str)) = *second_arg.expr { - if validate_sym(&str.value) { - let custom_sym = str.value.to_string(); - user_symbol = Some(custom_sym); - } else { + let mut node = node; + node.args.reverse(); + + if let Some(ast::ExprOrSpread { + expr: first_arg, .. + }) = node.args.pop() + { + if let Some(second_arg) = node.args.pop() { + if let ast::Expr::Lit(ast::Lit::Str(ref str)) = *second_arg.expr { + if validate_sym(&str.value) { + let custom_sym = str.value.to_string(); + user_symbol = Some(custom_sym); + } else { + HANDLER.with(|handler| { + handler + .struct_span_err( + str.span, + "Second argument should be the name of a valid identifier", + ) + .emit(); + }); + } + } + } + + let symbol_name = match self.register_context_name(&user_symbol) { + Ok(symbol_name) => symbol_name, + Err(err) => { HANDLER.with(|handler| { handler - .struct_span_err( - str.span, - "Second argument should be the name of a valid identifier", - ) + .struct_span_err(node.span, &format!("{}", err)) .emit(); }); + user_symbol.unwrap() } - } + }; + + let mut canonical_filename = + ["h_", &self.path_data.file_prefix, "_", &symbol_name].concat(); + canonical_filename.make_ascii_lowercase(); + + let folded = fold_expr(self, *first_arg); + let mut hook_collect = HookCollect::new(); + visit_expr(&mut hook_collect, &folded); + + let entry = self.entry_policy.get_entry_for_sym( + &symbol_name, + self.path_data, + &self.stack_ctxt, + &hook_collect, + &folded, + ); + + let import_path = fix_path( + "a", + &self.path_data.path, + &format!( + "./{}", + entry + .as_ref() + .map(|e| e.as_ref()) + .unwrap_or(&canonical_filename) + ), + ) + // TODO: check with manu + .unwrap(); + + let (local_decl, local_idents) = hook_collect.get_words(); + self.hooks.push(Hook { + entry, + canonical_filename, + name: symbol_name.clone(), + expr: Box::new(folded), + local_decl, + local_idents, + origin: self.path_data.path.to_string_lossy().into(), + }); + + create_inline_qhook(import_path, &symbol_name) + } else { + node } - - let symbol_name = match self.register_context_name(&user_symbol) { - Ok(symbol_name) => symbol_name, - Err(err) => { - HANDLER.with(|handler| { - handler - .struct_span_err(node.span, &format!("{}", err)) - .emit(); - }); - user_symbol.unwrap() - } - }; - - let mut canonical_filename = - ["h_", &self.path_data.file_prefix, "_", &symbol_name].concat(); - canonical_filename.make_ascii_lowercase(); - - // Remove last arguments - node.args.drain(1..); - - let folded = node.fold_children_with(self); - let hook_collect = HookCollect::new(&folded); - let entry = self.entry_policy.get_entry_for_sym( - &symbol_name, - self.path_data, - &self.stack_ctxt, - &hook_collect, - &folded, - ); - - let import_path = fix_path( - "a", - &self.path_data.path, - &format!( - "./{}", - entry - .as_ref() - .map(|e| e.as_ref()) - .unwrap_or(&canonical_filename) - ), - ) - // TODO: check with manu - .unwrap(); - - let (local_decl, local_idents) = hook_collect.get_words(); - self.hooks.push(Hook { - entry, - canonical_filename, - name: symbol_name.clone(), - expr: folded, - local_decl, - local_idents, - origin: self.path_data.path.to_string_lossy().into(), - }); - - create_inline_qhook(import_path, &symbol_name, qhook_span) } } impl<'a> Fold for HookTransform<'a> { noop_fold_type!(); + fn fold_module(&mut self, node: ast::Module) -> ast::Module { + let node = add_qwik_runtime_import(node); + node.fold_children_with(self) + } + fn fold_module_item(&mut self, item: ast::ModuleItem) -> ast::ModuleItem { match item { ast::ModuleItem::Stmt(ast::Stmt::Decl(ast::Decl::Var(node))) => { @@ -377,8 +382,9 @@ impl<'a> Fold for HookTransform<'a> { fn fold_call_expr(&mut self, node: ast::CallExpr) -> ast::CallExpr { let mut open_component = false; if let ast::ExprOrSuper::Expr(expr) = &node.callee { - if let ast::Expr::Ident(id) = &**expr { - let qhook_span = id.span; + if node.span.has_mark(self.qhook_mark) { + return self.handle_qhook(node); + } else if let ast::Expr::Ident(id) = &**expr { if QCOMPONENT.eq(&id.sym) { self.position_ctxt.push(PositionToken::QComponent); open_component = true; @@ -386,7 +392,8 @@ impl<'a> Fold for HookTransform<'a> { comments.add_pure_comment(node.span.lo); } } else if QHOOK.eq(&id.sym) { - return self.handle_qhook(node, qhook_span); + println!("{}", node.span.has_mark(self.qhook_mark)); + return self.handle_qhook(node); } } } @@ -399,52 +406,129 @@ impl<'a> Fold for HookTransform<'a> { } } -fn create_inline_qhook(url: JsWord, symbol: &str, span: Span) -> ast::CallExpr { - ast::CallExpr { - callee: ast::ExprOrSuper::Expr(Box::new(ast::Expr::Ident(ast::Ident::new( - QHOOK.clone(), - span, - )))), +fn add_qwik_runtime_import(mut module: ast::Module) -> ast::Module { + let mut body = Vec::with_capacity(module.body.len() + 1); + body.push(create_synthetic_wildcard_import( + &QWIK_INTERNAL, + &BUILDER_IO_QWIK, + )); + body.append(&mut module.body); + ast::Module { body, ..module } +} + +pub fn create_synthetic_wildcard_import(local: &JsWord, src: &JsWord) -> ast::ModuleItem { + ast::ModuleItem::ModuleDecl(ast::ModuleDecl::Import(ast::ImportDecl { span: DUMMY_SP, - type_args: None, - args: vec![ - ast::ExprOrSpread { - spread: None, - expr: Box::new(ast::Expr::Arrow(ast::ArrowExpr { - is_async: false, - is_generator: false, - span: DUMMY_SP, - params: vec![], - return_type: None, - type_params: None, - body: ast::BlockStmtOrExpr::Expr(Box::new(ast::Expr::Call(ast::CallExpr { - callee: ast::ExprOrSuper::Expr(Box::new(ast::Expr::Ident( - ast::Ident::new("import".into(), DUMMY_SP), - ))), - span: DUMMY_SP, - type_args: None, - args: vec![ast::ExprOrSpread { - spread: None, - expr: Box::new(ast::Expr::Lit(ast::Lit::Str(ast::Str { - span: DUMMY_SP, - value: url, - has_escape: false, - kind: ast::StrKind::Synthesized, - }))), - }], - }))), - })), + src: ast::Str { + span: DUMMY_SP, + has_escape: false, + value: src.clone(), + kind: ast::StrKind::Normal { + contains_quote: false, }, - ast::ExprOrSpread { - spread: None, - expr: Box::new(ast::Expr::Lit(ast::Lit::Str(ast::Str { + }, + asserts: None, + type_only: false, + specifiers: vec![ast::ImportSpecifier::Namespace( + ast::ImportStarAsSpecifier { + local: ast::Ident::new(local.clone(), DUMMY_SP), + span: DUMMY_SP, + }, + )], + })) +} + +// fn create_synthetic_named_import( +// local: &JsWord, +// imported: &JsWord, +// src: &JsWord, +// ) -> ast::ModuleItem { +// ast::ModuleItem::ModuleDecl(ast::ModuleDecl::Import(ast::ImportDecl { +// span: DUMMY_SP, +// src: ast::Str { +// span: DUMMY_SP, +// has_escape: false, +// value: src.clone(), +// kind: ast::StrKind::Normal { +// contains_quote: false, +// }, +// }, +// asserts: None, +// type_only: false, +// specifiers: vec![ast::ImportSpecifier::Named(ast::ImportNamedSpecifier { +// is_type_only: false, +// span: DUMMY_SP, +// local: ast::Ident::new(local.clone(), DUMMY_SP), +// imported: Some(ast::Ident::new(imported.clone(), DUMMY_SP)), +// })], +// })) +// } + +fn create_inline_qhook(url: JsWord, symbol: &str) -> ast::CallExpr { + create_internal_call( + &QHOOK, + vec![ + ast::Expr::Arrow(ast::ArrowExpr { + is_async: false, + is_generator: false, + span: DUMMY_SP, + params: vec![], + return_type: None, + type_params: None, + body: ast::BlockStmtOrExpr::Expr(Box::new(ast::Expr::Call(ast::CallExpr { + callee: ast::ExprOrSuper::Expr(Box::new(ast::Expr::Ident(ast::Ident::new( + "import".into(), + DUMMY_SP, + )))), span: DUMMY_SP, - value: symbol.into(), - has_escape: false, - kind: ast::StrKind::Synthesized, + type_args: None, + args: vec![ast::ExprOrSpread { + spread: None, + expr: Box::new(ast::Expr::Lit(ast::Lit::Str(ast::Str { + span: DUMMY_SP, + value: url, + has_escape: false, + kind: ast::StrKind::Synthesized, + }))), + }], }))), - }, + }), + ast::Expr::Lit(ast::Lit::Str(ast::Str { + span: DUMMY_SP, + value: symbol.into(), + has_escape: false, + kind: ast::StrKind::Synthesized, + })), ], + None, + ) +} + +pub fn create_internal_call( + fn_name: &JsWord, + exprs: Vec, + mark: Option, +) -> ast::CallExpr { + let span = mark.map_or(DUMMY_SP, |mark| DUMMY_SP.apply_mark(mark)); + ast::CallExpr { + callee: ast::ExprOrSuper::Expr(Box::new(ast::Expr::Member(ast::MemberExpr { + obj: ast::ExprOrSuper::Expr(Box::new(ast::Expr::Ident(ast::Ident::new( + QWIK_INTERNAL.clone(), + DUMMY_SP, + )))), + prop: Box::new(ast::Expr::Ident(ast::Ident::new(fn_name.clone(), DUMMY_SP))), + computed: false, + span: DUMMY_SP, + }))), + span, + type_args: None, + args: exprs + .into_iter() + .map(|expr| ast::ExprOrSpread { + spread: None, + expr: Box::new(expr), + }) + .collect(), } } diff --git a/src/optimizer/core/src/words.rs b/src/optimizer/core/src/words.rs new file mode 100644 index 00000000000..78ade0b576c --- /dev/null +++ b/src/optimizer/core/src/words.rs @@ -0,0 +1,10 @@ +use lazy_static::lazy_static; +use swc_atoms::JsWord; + +lazy_static! { + pub static ref QHOOK: JsWord = JsWord::from("qHook"); + pub static ref QWIK_INTERNAL: JsWord = JsWord::from("__qwik__"); + pub static ref BUILDER_IO_QWIK: JsWord = JsWord::from("@builder.io/qwik"); + pub static ref QCOMPONENT: JsWord = JsWord::from("qComponent"); + pub static ref QHOOK_HANDLER: JsWord = JsWord::from("qHook"); +} From 02f5810a0d50eb96b3a0eca80f2284cf5ad87970 Mon Sep 17 00:00:00 2001 From: Manu MA Date: Tue, 4 Jan 2022 17:26:16 +0100 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=A7=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/optimizer/core/src/words.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/optimizer/core/src/words.rs b/src/optimizer/core/src/words.rs index 78ade0b576c..9d199f0ed7d 100644 --- a/src/optimizer/core/src/words.rs +++ b/src/optimizer/core/src/words.rs @@ -2,9 +2,9 @@ use lazy_static::lazy_static; use swc_atoms::JsWord; lazy_static! { - pub static ref QHOOK: JsWord = JsWord::from("qHook"); - pub static ref QWIK_INTERNAL: JsWord = JsWord::from("__qwik__"); - pub static ref BUILDER_IO_QWIK: JsWord = JsWord::from("@builder.io/qwik"); - pub static ref QCOMPONENT: JsWord = JsWord::from("qComponent"); - pub static ref QHOOK_HANDLER: JsWord = JsWord::from("qHook"); + pub static ref QHOOK: JsWord = JsWord::from("qHook"); + pub static ref QWIK_INTERNAL: JsWord = JsWord::from("__qwik__"); + pub static ref BUILDER_IO_QWIK: JsWord = JsWord::from("@builder.io/qwik"); + pub static ref QCOMPONENT: JsWord = JsWord::from("qComponent"); + pub static ref QHOOK_HANDLER: JsWord = JsWord::from("qHook"); }