Attribute shorthand

This commit is contained in:
Greg Shuflin 2024-06-06 11:45:00 -05:00
parent 38873dcb74
commit 393dbabc9b
5 changed files with 41 additions and 17 deletions

View File

@ -42,7 +42,7 @@ impl AttributeDiscriminant {
impl<'src> Attribute<'src> {
pub(crate) fn new(
name: Name<'src>,
argument: Option<StringLiteral<'src>>,
mut arguments: Vec<StringLiteral<'src>>,
) -> CompileResult<'src, Self> {
use AttributeDiscriminant::*;
@ -56,8 +56,7 @@ impl<'src> Attribute<'src> {
})
})?;
let found = argument.as_ref().iter().count();
let found = arguments.len();
let range = discriminant.argument_range();
if !range.contains(&found) {
@ -72,9 +71,9 @@ impl<'src> Attribute<'src> {
}
Ok(match discriminant {
Confirm => Self::Confirm(argument),
Doc => Self::Doc(argument),
Group => Self::Group(argument.unwrap()),
Confirm => Self::Confirm(arguments.pop()),
Doc => Self::Doc(arguments.pop()),
Group => Self::Group(arguments.pop().unwrap()),
Linux => Self::Linux,
Macos => Self::Macos,
NoCd => Self::NoCd,

View File

@ -987,15 +987,29 @@ impl<'run, 'src> Parser<'run, 'src> {
loop {
let name = self.parse_name()?;
let argument = if self.accepted(ParenL)? {
let argument = self.parse_string_literal()?;
self.expect(ParenR)?;
Some(argument)
let arguments: Vec<StringLiteral> = if self.next_is(Colon) {
self.presume(Colon)?;
let single_arg = self.parse_string_literal()?;
vec![single_arg]
} else if self.next_is(ParenL) {
self.presume(ParenL)?;
let mut args = Vec::new();
loop {
args.push(self.parse_string_literal()?);
if self.next_is(ParenR) {
self.presume(ParenR)?;
break;
}
if self.next_is(Comma) {
self.presume(Comma)?;
}
}
args
} else {
None
Vec::new()
};
let attribute = Attribute::new(name, argument)?;
let attribute = Attribute::new(name, arguments)?;
if let Some(line) = attributes.get(&attribute) {
return Err(name.error(CompileErrorKind::DuplicateAttribute {
@ -1158,6 +1172,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

@ -130,7 +130,7 @@ fn confirm_recipe_with_prompt_too_many_args() {
echo confirmed
",
)
.stderr("error: Expected ')', but found ','\n ——▶ justfile:1:64\n\n1 │ [confirm(\"This is dangerous - are you sure you want to run it?\",\"this second argument is not supported\")]\n ^\n")
.stderr("error: Attribute `confirm` got 2 arguments but takes at most 1 argument\n ——▶ justfile:1:2\n\n1 │ [confirm(\"This is dangerous - are you sure you want to run it?\",\"this second argument is not supported\")]\n^^^^^^^\n")
.stdout("")
.status(1)
.run();

View File

@ -126,11 +126,10 @@ fn list_groups_with_custom_prefix() {
Test::new()
.justfile(
"
[group('B')]
[group: 'B']
foo:
[group('A')]
[group('B')]
[group: 'A', group: 'B']
bar:
",
)