1

Why is the document object unknown. In fact, everything, even the window.document call is not linted. Linting is quite important for me to lean a new programming language/library.

lib.rs

use wasm_bindgen::prelude::*;

#[wasm_bindgen(start)]
pub fn run() -> Result<(), JsValue> {
    let window = web_sys::window().expect("could not get window handle");
    let document = window.document().expect("could not get document handle");

    let body = document.body().expect("could not get body handle");

    let val = document.create_element("p")?;
    val.set_text_content(Some("Hello from rust"));

    body.append_child(&val)?;

    Ok(())
}

cargo.toml

[lib]
crate-type = ["cdylib"]

[dependencies]
serde = { version = "1.0", features = ["derive"] }
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }

[dependencies.web-sys]
version = "0.3.53"
features = [
    "Document",
    "Element",
    "HtmlElement",
    "Node",
    "Window",
]

enter image description here

Elias
  • 3,592
  • 2
  • 19
  • 42
  • `rust-analyzer` isn't perfect, it can't always deduce types. You can add manual type ascriptions if this is an issue for you. – smitop Aug 24 '21 at 19:03
  • @Smitop Well adding types does not help, the methods of the struct don't get listed anyways. They now show the correct type on hover, but that's not of much use :( – Elias Aug 24 '21 at 19:05
  • @Smitop in fact, even if I take in an `cont: &HtmlElement` as function argument, no methods are linted on `cont`. Sadly the rust language server is even worse :( Could it be that it is not correctly detecting the features array? – Elias Aug 24 '21 at 19:10
  • I doubt you'll get an answer beyond what is already stated: *"rust-analyzer isn't perfect"*. I see it fail like this most often under macro-heavy code (`web_sys` relies heavily on `#[wasm_bindgen]` for its types). In lieu of proper IDE type annotations, I suggest taking the time to explore the documentation on [docs.rs](https://docs.rs/web-sys/0.3.53/web_sys/struct.Window.html#method.document). – kmdreko Aug 24 '21 at 19:18
  • @kmdreko okay... is it worth opening some sort of issue? I'll do what I can to help but sadly enough I'm nowhere near skilled enough to work on something like this. – Elias Aug 24 '21 at 19:20
  • The maintainers of rust-analyzer are almost definitely aware. There's [a lot of issues](https://github.com/rust-analyzer/rust-analyzer/labels/A-macro) when dealing with macros. [This one](https://github.com/rust-analyzer/rust-analyzer/issues/9868) seems particularly relevant, and shows code completion under attribute macros is one of the next things on their radar. – kmdreko Aug 24 '21 at 19:31

0 Answers0