Add [ATTRIBUTE: VALUE] shorthand (#2136)

This commit is contained in:
Greg Shuflin 2024-06-08 11:33:45 -07:00 committed by GitHub
parent 4fbd03735a
commit 1ca53e8b22
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 43 additions and 7 deletions

View File

@ -57,9 +57,7 @@ impl<'src> Attribute<'src> {
})?;
let found = argument.as_ref().iter().count();
let range = discriminant.argument_range();
if !range.contains(&found) {
return Err(
name.error(CompileErrorKind::AttributeArgumentCountMismatch {

View File

@ -987,15 +987,18 @@ impl<'run, 'src> Parser<'run, 'src> {
loop {
let name = self.parse_name()?;
let argument = if self.accepted(ParenL)? {
let argument = self.parse_string_literal()?;
let maybe_argument = if self.accepted(Colon)? {
let arg = self.parse_string_literal()?;
Some(arg)
} else if self.accepted(ParenL)? {
let arg = self.parse_string_literal()?;
self.expect(ParenR)?;
Some(argument)
Some(arg)
} else {
None
};
let attribute = Attribute::new(name, argument)?;
let attribute = Attribute::new(name, maybe_argument)?;
if let Some(line) = attributes.get(&attribute) {
return Err(name.error(CompileErrorKind::DuplicateAttribute {
@ -1158,6 +1161,18 @@ mod tests {
tree: (justfile (alias t test)),
}
test! {
name: single_argument_attribute_shorthand,
text: "[group: 'some-group']\nalias t := test",
tree: (justfile (alias t test)),
}
test! {
name: single_argument_attribute_shorthand_multiple_same_line,
text: "[group: 'some-group', group: 'some-other-group']\nalias t := test",
tree: (justfile (alias t test)),
}
test! {
name: aliases_multiple,
text: "alias t := test\nalias b := build",

View File

@ -72,7 +72,7 @@ fn multiple_attributes_one_line_error_message() {
)
.stderr(
"
error: Expected ']', ',', or '(', but found identifier
error: Expected ']', ':', ',', or '(', but found identifier
justfile:1:17
1 [macos, windows linux]

View File

@ -144,3 +144,26 @@ fn list_groups_with_custom_prefix() {
)
.run();
}
#[test]
fn list_groups_with_shorthand_syntax() {
Test::new()
.justfile(
"
[group: 'B']
foo:
[group: 'A', group: 'B']
bar:
",
)
.args(["--groups", "--list-prefix", "..."])
.stdout(
"
Recipe groups:
...A
...B
",
)
.run();
}