Skip to content

Commit

Permalink
Allow star variadic parameters to accept defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
rjsberry committed Jun 13, 2020
1 parent 1275b18 commit cba69db
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 29 deletions.
7 changes: 0 additions & 7 deletions src/compilation_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,6 @@ impl Display for CompilationError<'_> {
UnterminatedBacktick => {
writeln!(f, "Unterminated backtick")?;
},
ZeroOrMoreVariadicParameterHasDefault { parameter } => {
writeln!(
f,
"Variadic parameter `{}` accepting zero or more arguments has a default value",
parameter
)?;
},
Internal { ref message } => {
writeln!(
f,
Expand Down
3 changes: 0 additions & 3 deletions src/compilation_error_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,4 @@ pub(crate) enum CompilationErrorKind<'src> {
UnterminatedInterpolation,
UnterminatedString,
UnterminatedBacktick,
ZeroOrMoreVariadicParameterHasDefault {
parameter: &'src str,
},
}
14 changes: 4 additions & 10 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,16 +571,10 @@ impl<'tokens, 'src> Parser<'tokens, 'src> {
fn parse_parameter(&mut self, kind: ParameterKind) -> CompilationResult<'src, Parameter<'src>> {
let name = self.parse_name()?;

let default = match (self.accepted(Equals)?, kind) {
(true, ParameterKind::Star) => {
return Err(self.next()?.error(
CompilationErrorKind::ZeroOrMoreVariadicParameterHasDefault {
parameter: name.lexeme(),
},
));
},
(true, _) => Some(self.parse_value()?),
_ => None,
let default = if self.accepted(Equals)? {
Some(self.parse_value()?)
} else {
None
};

Ok(Parameter {
Expand Down
24 changes: 15 additions & 9 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1860,19 +1860,25 @@ a x y *z:
}

test! {
name: star_variadic_disallows_default,
name: star_variadic_ignore_default,
justfile: "
a x y *z='':
a x y *z='HELLO':
echo {{x}} {{y}} {{z}}
",
args: ("a", "0", "1"),
stdout: "",
stderr: "error: Variadic parameter `z` accepting zero or more arguments has a default value
|
2 | a x y *z='':
| ^^
args: ("a", "0", "1", "2", "3", " 4 "),
stdout: "0 1 2 3 4\n",
stderr: "echo 0 1 2 3 4 \n",
}

test! {
name: star_variadic_use_default,
justfile: "
a x y *z='HELLO':
echo {{x}} {{y}} {{z}}
",
status: EXIT_FAILURE,
args: ("a", "0", "1"),
stdout: "0 1 HELLO\n",
stderr: "echo 0 1 HELLO\n",
}

test! {
Expand Down

0 comments on commit cba69db

Please sign in to comment.