Jelajahi Sumber

fix(macros): collision when command is named `cmd` (#1802)

Lucas Fernandes Nogueira 4 tahun lalu
induk
melakukan
d36b726926

+ 5 - 0
.changes/fix-command-named-cmd.md

@@ -0,0 +1,5 @@
+---
+"tauri-macros": patch
+---
+
+Fixes a name collision when the command function is named `cmd`.

+ 3 - 3
core/tauri-macros/src/command/handler.rs

@@ -52,11 +52,11 @@ impl From<Handler> for proc_macro::TokenStream {
     }: Handler,
   ) -> Self {
     quote::quote!(move |invoke| {
-      let cmd = invoke.message.command();
-      match cmd {
+      let __tauri_cmd__ = invoke.message.command();
+      match __tauri_cmd__ {
         #(stringify!(#commands) => #wrappers!(#paths, invoke),)*
         _ => {
-          invoke.resolver.reject(format!("command {} not found", cmd))
+          invoke.resolver.reject(format!("command {} not found", __tauri_cmd__))
         },
       }
     })

+ 3 - 0
examples/commands/src-tauri/src/commands.rs

@@ -4,6 +4,9 @@
 
 use tauri::{command, State};
 
+#[command]
+pub fn cmd(_argument: String) {}
+
 #[command]
 pub fn simple_command(argument: String) {
   println!("{}", argument);

+ 2 - 0
examples/commands/src-tauri/src/main.rs

@@ -9,6 +9,7 @@
 
 // we move some basic commands to a separate module just to show it works
 mod commands;
+use commands::cmd;
 
 use serde::Deserialize;
 use tauri::{command, Params, State, Window};
@@ -167,6 +168,7 @@ fn main() {
       force_async_with_result,
       commands::simple_command,
       commands::stateful_command,
+      cmd,
       async_simple_command,
       future_simple_command,
       async_stateful_command,