madonoharu / tsify

A library for generating TypeScript definitions from rust code.
Apache License 2.0
311 stars 42 forks source link

Rust-analyzer marks structs with snake-case warning #42

Open FredrikNoren opened 10 months ago

FredrikNoren commented 10 months ago

I'm getting the following warning on all structs that have Tsify:

Function `__wbg_instanceof_JsType_24d65669860e1289` should have snake_case name, e.g. `__wbg_instanceof_js_type_24d65669860e1289`
rouzwelt commented 10 months ago

yup I get this too, would be good if we get a solution for this

zomem commented 10 months ago

有一个办法,在文件第一行加上下面的代码,就可以不显示警告了。

#![allow(non_snake_case)]
rouzwelt commented 10 months ago

有一个办法,在文件第一行加上下面的代码,就可以不显示警告了。

#![allow(non_snake_case)]

this not a good approach and is overall discouraged by the community to change compiler settings

the solution is that the maintainers fix the underlying cause

nappa85 commented 9 months ago

Last code update has been 8 months ago, there are PR stuck since July, is this crate dead?

Pistonight commented 8 months ago

This seems like a rust-analyzer issue. The ___wbg_instanceof is from wasm-bindgen for JsCast and the surrounding block has #[automatically_derived] which should suppress this kind of warnings

Pistonight commented 8 months ago

This might be a workaround:

#[allow(non_snake_case)]
mod tsify_derive {
    use super::*;
    #[derive(Tsify, Serialize, Deserialize)]
    #[tsify(into_wasm_abi, from_wasm_abi)]
    pub struct MyStruct {
        ...
    }
}
pub use tsify_derive::MyStruct;

Or make a proc macro that does this for you (I just typed it ad-hoc so there might be a few errors, but the idea is there)

use quote::quote;
use proc_macro2::{Ident, Span};
use syn::parse_macro_input;

#[proc_macro_attr]
pub fn suppress_derive_case_warnings(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let parsed = parse_macro_input!(input as DeriveInput);
    let name = &parsed.ident;
    let vis = &parsed.vis;

    let mod_name = Ident::new(&format!("__tsify_mod_{}", name), Span::call_site());

    let expanded = quote! {
        #[allow(non_snake_case)]
        #[automatically_derived]
        mod #mod_name {
            use super::*;

            #parsed
        }
        #vis use #mod_name::#name;
    };

    expanded.into()
}

Then use it like this

#[suppress_derive_case_warnings]
#[derive(Tsify, Serialize, Deserialize)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct MyStruct {
    ...
}
Sharpiro commented 6 months ago

I am only getting this error with rust-analyzer using VS Code, the error itself is not coming from my Rust installation.

I was able to suppress the error with the VS Code setting:

"rust-analyzer.diagnostics.disabled": ["non_snake_case"]

Also needed to restart the rust-analyzer language server.