Skip to content

Commit b24b82e

Browse files
committed
Enhance vector variable expansion to properly handle command names
To eliminate false positives in rust-analyzer. Also add compile_fail tests to ensure invalid syntax is still rejected.
1 parent 74d1a95 commit b24b82e

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

macros/src/lexer.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,22 @@ impl Lexer {
417417
if g.delimiter() == Delimiter::Brace {
418418
self.extend_last_arg(quote!(#var.as_os_str()));
419419
} else {
420+
// For vector variables $[var], we need to check if there's content
421+
// immediately before (without space), which would be invalid like: xxx$[var]
422+
// But if last_arg_str is just the command name, it should be flushed first
420423
if !self.last_arg_str.is_empty() {
421-
abort!(span, "vector variable can only be used alone");
424+
// Check if this is the first argument (command name)
425+
if self.args.is_empty() {
426+
// Flush the command name as the first argument
427+
self.args.push(ParseArg::ArgStr(self.last_arg_str.clone()));
428+
self.last_arg_str = TokenStream::new();
429+
} else {
430+
// This is an error: something like xxx$[var] without space
431+
abort!(
432+
span,
433+
"vector variable cannot have content immediately before it"
434+
);
435+
}
422436
}
423437
self.args.push(ParseArg::ArgVec(quote!(#var)));
424438
}

macros/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@
5656
//! run_fun!(echo "${msg 0}");
5757
//! ```
5858
//!
59+
//! ### Invalid vector variable syntax
60+
//!
61+
//! Vector variables cannot have text immediately before them:
62+
//! ```compile_fail
63+
//! # use cmd_lib::*;
64+
//! let opts = ["-a", "-f"];
65+
//! run_cmd!(ls xxx$[opts]);
66+
//! ```
67+
//!
68+
//! ```compile_fail
69+
//! # use cmd_lib::*;
70+
//! let msg = "hello";
71+
//! let opts = ["-a", "-f"];
72+
//! run_cmd!(ls $msg$[opts]);
73+
//! ```
74+
//!
5975
//! ### Invalid redirect syntax
6076
//!
6177
//! Invalid redirect operator spacing:

tests/test_macros.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,9 @@ fn test_empty_arg() {
372372
fn test_env_var_with_equal_sign() {
373373
assert!(run_cmd!(A="-c B=c" echo).is_ok());
374374
}
375+
376+
#[test]
377+
fn test_vector_variable() {
378+
let opts = ["-n", "hello"];
379+
assert!(run_cmd!(echo $[opts]).is_ok())
380+
}

0 commit comments

Comments
 (0)