mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-02-26 04:25:05 +01:00
Support dashes in template variable/fn names
This commit is contained in:
@@ -268,7 +268,12 @@ impl Parser {
|
||||
let mut text = String::new();
|
||||
while self.pos < self.chars.len() {
|
||||
let ch = self.peek_char();
|
||||
if ch.is_alphanumeric() || ch == '_' {
|
||||
let is_valid = if start_pos == self.pos {
|
||||
ch.is_alphabetic() // First char has to be alphabetic
|
||||
} else {
|
||||
ch.is_alphanumeric() || ch == '-' || ch == '-'
|
||||
};
|
||||
if is_valid {
|
||||
text.push(ch);
|
||||
self.pos += 1;
|
||||
} else {
|
||||
@@ -422,6 +427,49 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn var_dashes() {
|
||||
let mut p = Parser::new("${[ a-b ]}");
|
||||
assert_eq!(
|
||||
p.parse().tokens,
|
||||
vec![
|
||||
Token::Tag {
|
||||
val: Val::Var { name: "a-b".into() }
|
||||
},
|
||||
Token::Eof
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn var_prefixes() {
|
||||
let mut p = Parser::new("${[ -a ]}${[ _a ]}${[ 0a ]}");
|
||||
assert_eq!(
|
||||
p.parse().tokens,
|
||||
vec![
|
||||
Token::Raw {
|
||||
// Shouldn't be parsed, because they're invalid
|
||||
text: "${[ -a ]}${[ _a ]}${[ 0a ]}".into()
|
||||
},
|
||||
Token::Eof
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn var_underscore_prefix() {
|
||||
let mut p = Parser::new("${[ _a ]}");
|
||||
assert_eq!(
|
||||
p.parse().tokens,
|
||||
vec![
|
||||
Token::Raw {
|
||||
text: "${[ _a ]}".into()
|
||||
},
|
||||
Token::Eof
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn var_boolean() {
|
||||
let mut p = Parser::new("${[ true ]}${[ false ]}");
|
||||
|
||||
@@ -159,7 +159,7 @@ const EnvironmentEditor = function ({
|
||||
const validateName = useCallback((name: string) => {
|
||||
// Empty just means the variable doesn't have a name yet, and is unusable
|
||||
if (name === '') return true;
|
||||
return name.match(/^[a-z_][a-z0-9_]*$/i) != null;
|
||||
return name.match(/^[a-z][a-z0-9_-]*$/i) != null;
|
||||
}, []);
|
||||
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user