From 7e2542cbf83fb4ed16709200189eddd2e61cd0d0 Mon Sep 17 00:00:00 2001 From: Teymour Aldridge Date: Sun, 21 Nov 2021 14:54:49 +0000 Subject: [PATCH] Add basic lints to the HTML macro. (#1748) * Add basic lints to the HTML macro. * f * Fix the examples. * Fix nightly warning message tests. * apply code review suggestions * update error message * rebase onto master * remove unused props * remove console log * remove js void * fix according to PR comments Co-authored-by: Julius Lungys <32368314+voidpumpkin@users.noreply.github.com> --- .github/workflows/pull-request.yml | 27 ++ examples/game_of_life/src/main.rs | 2 +- examples/router/src/components/author_card.rs | 2 +- examples/router/src/components/pagination.rs | 45 ++- examples/router/src/components/post_card.rs | 2 +- examples/router/src/main.rs | 25 +- examples/router/src/pages/author.rs | 2 +- examples/router/src/pages/home.rs | 2 +- examples/router/src/pages/post.rs | 6 +- examples/router/src/pages/post_list.rs | 50 +-- packages/yew-macro/Cargo.toml | 2 + packages/yew-macro/Makefile.toml | 6 + .../yew-macro/src/html_tree/html_block.rs | 4 +- .../src/html_tree/html_dashed_name.rs | 12 + .../yew-macro/src/html_tree/html_element.rs | 10 +- packages/yew-macro/src/html_tree/html_list.rs | 2 +- packages/yew-macro/src/html_tree/lint/mod.rs | 108 +++++ packages/yew-macro/src/html_tree/mod.rs | 5 +- packages/yew-macro/src/lib.rs | 2 + packages/yew-macro/tests/html_lints/fail.rs | 17 + .../yew-macro/tests/html_lints/fail.stderr | 29 ++ packages/yew-macro/tests/html_lints_test.rs | 7 + .../html_macro/html-component-fail.stderr | 371 ++++++++++++++++++ packages/yew-router/src/components/link.rs | 74 +++- website/docs/concepts/html.md | 11 + 25 files changed, 736 insertions(+), 87 deletions(-) create mode 100644 packages/yew-macro/src/html_tree/lint/mod.rs create mode 100644 packages/yew-macro/tests/html_lints/fail.rs create mode 100644 packages/yew-macro/tests/html_lints/fail.stderr create mode 100644 packages/yew-macro/tests/html_lints_test.rs create mode 100644 packages/yew-macro/tests/html_macro/html-component-fail.stderr diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index d22ef376e..8e0f7213e 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -215,3 +215,30 @@ jobs: with: command: test args: --all-targets --workspace --exclude yew + + test-lints: + name: Test lints on nightly + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + toolchain: nightly + override: true + profile: minimal + + - uses: actions/cache@v2 + with: + path: | + ~/.cargo/registry + ~/.cargo/git + target + key: cargo-${{ runner.os }}-${{ hashFiles('**/Cargo.toml') }} + restore-keys: | + cargo-${{ runner.os }}- + + - name: Run tests + run: | + cd packages/yew-macro + cargo +nightly test test_html_lints --features lints diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index d097813ce..1af9a1fae 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -185,7 +185,7 @@ impl Component for Model {
- +

{ "Game of Life" }

diff --git a/examples/router/src/components/author_card.rs b/examples/router/src/components/author_card.rs index caaba80e8..56eff10b1 100644 --- a/examples/router/src/components/author_card.rs +++ b/examples/router/src/components/author_card.rs @@ -33,7 +33,7 @@ impl Component for AuthorCard {
- + Author's profile picture
diff --git a/examples/router/src/components/pagination.rs b/examples/router/src/components/pagination.rs index b5c91cb7c..50e940d0a 100644 --- a/examples/router/src/components/pagination.rs +++ b/examples/router/src/components/pagination.rs @@ -1,12 +1,22 @@ +use serde::Deserialize; +use serde::Serialize; use yew::prelude::*; +use yew_router::prelude::*; + +use crate::Route; const ELLIPSIS: &str = "\u{02026}"; +#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)] +pub struct PageQuery { + pub page: u64, +} + #[derive(Clone, Debug, PartialEq, Properties)] pub struct Props { pub page: u64, pub total_pages: u64, - pub on_switch_page: Callback, + pub route_to_page: Route, } pub struct Pagination; @@ -34,18 +44,21 @@ impl Pagination { fn render_link(&self, to_page: u64, props: &Props) -> Html { let Props { page, - ref on_switch_page, + route_to_page, .. - } = *props; + } = props.clone(); - let onclick = on_switch_page.reform(move |_| to_page); let is_current_class = if to_page == page { "is-current" } else { "" }; html! {
  • - + + classes={classes!("pagination-link", is_current_class)} + to={route_to_page} + query={Some(PageQuery{page: to_page})} + > { to_page } - + >
  • } } @@ -100,23 +113,27 @@ impl Pagination { let Props { page, total_pages, - ref on_switch_page, - } = *props; + route_to_page: to, + } = props.clone(); html! { <> - + classes={classes!("pagination-previous")} disabled={page==1} - onclick={on_switch_page.reform(move |_| page - 1)} + query={Some(PageQuery{page: page - 1})} + to={to.clone()} > { "Previous" } - - > + + classes={classes!("pagination-next")} disabled={page==total_pages} - onclick={on_switch_page.reform(move |_| page + 1)} + query={Some(PageQuery{page: page + 1})} + {to} > { "Next page" } - + > } } diff --git a/examples/router/src/components/post_card.rs b/examples/router/src/components/post_card.rs index 961366428..ef29785f8 100644 --- a/examples/router/src/components/post_card.rs +++ b/examples/router/src/components/post_card.rs @@ -30,7 +30,7 @@ impl Component for PostCard {
    - + This post's image
    diff --git a/examples/router/src/main.rs b/examples/router/src/main.rs index ab4ee7b4b..aa8a3b606 100644 --- a/examples/router/src/main.rs +++ b/examples/router/src/main.rs @@ -80,22 +80,21 @@ impl Model { fn view_nav(&self, link: &Scope) -> Html { let Self { navbar_active, .. } = *self; - let active_class = if navbar_active { "is-active" } else { "" }; + let active_class = if !navbar_active { "is-active" } else { "" }; html! {