Support dashes in template variable/fn names

This commit is contained in:
Gregory Schier
2025-01-13 06:38:21 -08:00
parent 8cd9c031e8
commit 88ff7f4300
2 changed files with 50 additions and 2 deletions

View File

@@ -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 ]}");

View File

@@ -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 (