Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix types $ #151

Merged
merged 10 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ external
*.
**/*.log
etc
target
temp
tsdoc-metadata.json
**/.DS_Store
Expand Down
68 changes: 38 additions & 30 deletions integration/architecture/architecture.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import {
Fragment,
h,
Host,
component,
$,
component$,
Slot,
component,
PropsOf,
getProps,
useEvent,
onRender,
useState,
onRender$,
useStore,
} from '@builder.io/qwik';
/* eslint no-console: ["off"] */

Expand All @@ -22,8 +24,8 @@ export interface Cmp {

type ArchMode = 'monolith' | 'island' | 'uIslet';

export const ArchApp = component((props: { monolith: Cmp; islands: Cmp; uIslets: Cmp }) => {
return onRender(() => (
export const ArchApp = component$((props: { monolith: Cmp; islands: Cmp; uIslets: Cmp }) => {
return onRender$(() => (
<>
<h1>Monolith</h1>
<b>Examples:</b> Angular, React, Solid, Svelte, Vue, WebComponents
Expand Down Expand Up @@ -59,29 +61,35 @@ export const ArchApp = component((props: { monolith: Cmp; islands: Cmp; uIslets:
));
});

export const Browser = component('browser', () => {
return onRender(() => (
<div class="browser">
<div class="browser-url">
<span>⇦ ⇨ ⟳</span>
<input value="http://localhost/" />
export const Browser = component(
'browser',
$(() => {
return onRender$(() => (
<div class="browser">
<div class="browser-url">
<span>⇦ ⇨ ⟳</span>
<input value="http://localhost/" />
</div>
<div class="browser-body">
<Slot />
</div>
</div>
<div class="browser-body">
<Slot />
</div>
</div>
));
});
));
})
);

export const Component = component('component', (props: { cmp: Cmp; arch: ArchMode }) => {
return onRender(() => (
<Host class={getCmpClass(props.cmp)} on:click={Component_click}>
{props.cmp.children &&
props.cmp.children.map((cmp) => <Component cmp={cmp} arch={props.arch} />)}
{props.cmp.children ? null : '...'}
</Host>
));
});
export const Component = component(
'component',
$((props: { cmp: Cmp; arch: ArchMode }) => {
return onRender$(() => (
<Host class={getCmpClass(props.cmp)} on:click={Component_click}>
{props.cmp.children &&
props.cmp.children.map((cmp) => <Component cmp={cmp} arch={props.arch} />)}
{props.cmp.children ? null : '...'}
</Host>
));
})
);

export const Component_click = async () => {
// TODO(misko): Workaround for the fact that the click listener is sitting on `<Host>` and hence
Expand Down Expand Up @@ -116,9 +124,9 @@ function getCmpClass(cmp: Cmp, ...additionalClasses: string[]) {
return classes.join(' ');
}

export const MonolithScrubber = component((props: { cmp: Cmp }) => {
const state = useState({ step: 1 });
return onRender(() => (
export const MonolithScrubber = component$((props: { cmp: Cmp }) => {
const state = useStore({ step: 1 });
return onRender$(() => (
<>
<ol>
<li class={state.step >= 1 ? 'active' : ''}>
Expand All @@ -135,7 +143,7 @@ export const MonolithScrubber = component((props: { cmp: Cmp }) => {
Framework completes the rehydration of the application.
</li>
</ol>
<button on:click={() => monolithUpdate(props.cmp, ++state.step)}>&gt;&gt;&gt;</button>
<button on$:click={() => monolithUpdate(props.cmp, ++state.step)}>&gt;&gt;&gt;</button>
</>
));
});
Expand Down
8 changes: 4 additions & 4 deletions integration/hello_server/Greeter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://github.com/BuilderIO/qwik/blob/main/LICENSE
*/

import { component, h, useEvent, onRender } from '@builder.io/qwik';
import { component$, h, useEvent, onRender$ } from '@builder.io/qwik';

/**
* Declares the public component `<Greeter>` to be used in parent component.
Expand All @@ -24,8 +24,8 @@ import { component, h, useEvent, onRender } from '@builder.io/qwik';
*
* ```
*/
export const Greeter = component((props: { name: string }) => {
return onRender(() => (
export const Greeter = component$((props: { name: string }) => {
return onRender$(() => (
<div>
<div>
Your name:
Expand All @@ -34,7 +34,7 @@ export const Greeter = component((props: { name: string }) => {
// - Declare a listener on `input` to invoke `Greeter_onKeyup.ts`
// - The `value` should be set to the `event.target.value` property.
// - See `provideQrlExp` in `Greeter_onKeyup.ts` for details.
on:keyup={() =>
on$:keyup={() =>
(props.name = (useEvent<KeyboardEvent>().target as HTMLInputElement).value)
}
/>
Expand Down
12 changes: 9 additions & 3 deletions src/optimizer/core/src/code_move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ pub fn transform_arrow_fn(
) -> ast::ArrowExpr {
match arrow.body {
ast::BlockStmtOrExpr::BlockStmt(mut block) => {
let mut stmts = vec![];
let mut stmts = Vec::with_capacity(1 + block.stmts.len());
if !scoped_idents.is_empty() {
stmts.push(create_use_closure(qwik_ident, scoped_idents));
}
Expand All @@ -320,7 +320,7 @@ pub fn transform_arrow_fn(
}
}
ast::BlockStmtOrExpr::Expr(expr) => {
let mut stmts = vec![];
let mut stmts = Vec::with_capacity(2);
if !scoped_idents.is_empty() {
stmts.push(create_use_closure(qwik_ident, scoped_idents));
}
Expand All @@ -337,7 +337,13 @@ pub fn transform_arrow_fn(
}

pub fn transform_fn(node: ast::FnExpr, qwik_ident: &Id, scoped_idents: &[Id]) -> ast::FnExpr {
let mut stmts = vec![];
let mut stmts = Vec::with_capacity(
1 + node
.function
.body
.as_ref()
.map_or(0, |body| body.stmts.len()),
);
if !scoped_idents.is_empty() {
stmts.push(create_use_closure(qwik_ident, scoped_idents));
}
Expand Down
43 changes: 25 additions & 18 deletions src/optimizer/core/src/collector.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{BTreeMap, HashMap, HashSet};
use std::collections::{HashMap, HashSet};

use swc_atoms::{js_word, JsWord};
use swc_common::{BytePos, Span, SyntaxContext};
Expand Down Expand Up @@ -37,11 +37,12 @@ pub struct Import {
pub source: JsWord,
pub specifier: JsWord,
pub kind: ImportKind,
pub synthetic: bool,
}

pub struct GlobalCollect {
pub imports: BTreeMap<Id, Import>,
pub exports: BTreeMap<Id, Option<JsWord>>,
pub imports: HashMap<Id, Import>,
pub exports: HashMap<Id, Option<JsWord>>,
pub root: HashMap<Id, Span>,

rev_imports: HashMap<(JsWord, JsWord), Id>,
Expand All @@ -50,11 +51,11 @@ pub struct GlobalCollect {

pub fn global_collect(module: &ast::Module) -> GlobalCollect {
let mut collect = GlobalCollect {
imports: BTreeMap::new(),
exports: BTreeMap::new(),
imports: HashMap::with_capacity(16),
exports: HashMap::with_capacity(16),

root: HashMap::new(),
rev_imports: HashMap::new(),
root: HashMap::with_capacity(16),
rev_imports: HashMap::with_capacity(16),

in_export_decl: false,
};
Expand All @@ -70,7 +71,7 @@ impl GlobalCollect {
.map(|s| s.0.clone())
}

pub fn import(&mut self, specifier: JsWord, source: JsWord) -> (Id, bool) {
pub fn import(&mut self, specifier: JsWord, source: JsWord) -> Id {
self.rev_imports
.get(&(specifier.clone(), source.clone()))
.cloned()
Expand All @@ -83,11 +84,12 @@ impl GlobalCollect {
source,
specifier,
kind: ImportKind::Named,
synthetic: true,
},
);
(local, true)
local
},
|local| (local, false),
|local| local,
)
}

Expand All @@ -114,7 +116,9 @@ impl Visit for GlobalCollect {
}
ast::Decl::Var(var) => {
for decl in &var.decls {
collect_from_pat(&decl.name, &mut self.root);
let mut identifiers: Vec<(Id, Span)> = vec![];
collect_from_pat(&decl.name, &mut identifiers);
self.root.extend(identifiers.into_iter());
}
}
_ => {}
Expand All @@ -138,6 +142,7 @@ impl Visit for GlobalCollect {
source: node.src.value.clone(),
specifier: imported,
kind: ImportKind::Named,
synthetic: false,
},
);
}
Expand All @@ -148,6 +153,7 @@ impl Visit for GlobalCollect {
source: node.src.value.clone(),
specifier: js_word!("default"),
kind: ImportKind::Default,
synthetic: false,
},
);
}
Expand All @@ -158,6 +164,7 @@ impl Visit for GlobalCollect {
source: node.src.value.clone(),
specifier: "*".into(),
kind: ImportKind::All,
synthetic: false,
},
);
}
Expand Down Expand Up @@ -272,7 +279,7 @@ impl IdentCollector {
pub fn new() -> Self {
Self {
local_idents: HashSet::new(),
expr_ctxt: vec![],
expr_ctxt: Vec::with_capacity(32),
use_h: false,
use_fragment: false,
}
Expand Down Expand Up @@ -343,10 +350,10 @@ impl Visit for IdentCollector {
}
}

pub fn collect_from_pat(pat: &ast::Pat, identifiers: &mut HashMap<Id, Span>) {
pub fn collect_from_pat(pat: &ast::Pat, identifiers: &mut Vec<(Id, Span)>) {
match pat {
ast::Pat::Ident(ident) => {
identifiers.insert(id!(ident.id), ident.id.span);
identifiers.push((id!(ident.id), ident.id.span));
}
ast::Pat::Array(array) => {
for el in array.elems.iter().flatten() {
Expand All @@ -355,26 +362,26 @@ pub fn collect_from_pat(pat: &ast::Pat, identifiers: &mut HashMap<Id, Span>) {
}
ast::Pat::Rest(rest) => {
if let ast::Pat::Ident(ident) = rest.arg.as_ref() {
identifiers.insert(id!(ident.id), ident.id.span);
identifiers.push((id!(ident.id), ident.id.span));
}
}
ast::Pat::Assign(expr) => {
if let ast::Pat::Ident(ident) = expr.left.as_ref() {
identifiers.insert(id!(ident.id), ident.id.span);
identifiers.push((id!(ident.id), ident.id.span));
}
}
ast::Pat::Object(obj) => {
for prop in &obj.props {
match prop {
ast::ObjectPatProp::Assign(ref v) => {
identifiers.insert(id!(v.key), v.key.span);
identifiers.push((id!(v.key), v.key.span));
}
ast::ObjectPatProp::KeyValue(ref v) => {
collect_from_pat(&v.value, identifiers);
}
ast::ObjectPatProp::Rest(ref v) => {
if let ast::Pat::Ident(ident) = v.arg.as_ref() {
identifiers.insert(id!(ident.id), ident.id.span);
identifiers.push((id!(ident.id), ident.id.span));
}
}
}
Expand Down
Loading