Skip to content

Commit

Permalink
Forbid invalid attributes on assignments (#2412)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Oct 5, 2024
1 parent a101dd5 commit 85c6e05
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
17 changes: 15 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,18 +480,31 @@ impl<'run, 'src> Parser<'run, 'src> {
fn parse_assignment(
&mut self,
export: bool,
attributes: BTreeSet<Attribute<'_>>,
attributes: BTreeSet<Attribute<'src>>,
) -> CompileResult<'src, Assignment<'src>> {
let name = self.parse_name()?;
self.presume(ColonEquals)?;
let value = self.parse_expression()?;
self.expect_eol()?;

let private = attributes.contains(&Attribute::Private);

for attribute in attributes {
if attribute != Attribute::Private {
return Err(name.error(CompileErrorKind::InvalidAttribute {
item_kind: "Assignment",
item_name: name.lexeme(),
attribute,
}));
}
}

Ok(Assignment {
file_depth: self.file_depth,
export,
name,
value,
private: name.lexeme().starts_with('_') || attributes.contains(&Attribute::Private),
private: private || name.lexeme().starts_with('_'),
})
}

Expand Down
47 changes: 36 additions & 11 deletions tests/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,54 @@ use super::*;
test! {
name: set_export_parse_error,
justfile: "
set export := fals
set export := fals
",
stdout: "",
stderr: "
error: Expected keyword `true` or `false` but found identifier `fals`
|
1 | set export := fals
| ^^^^
β€”β€”β–Ά justfile:1:15
β”‚
1 β”‚ set export := fals
β”‚ ^^^^
",
status: EXIT_FAILURE,
}

test! {
name: set_export_parse_error_EOL,
name: set_export_parse_error_eol,
justfile: "
set export := fals
set export :=
",
stdout: "",
stderr: "
error: Expected keyword `true` or `false` but found `end of line`
|
1 | set export :=
| ^
error: Expected identifier, but found end of line
β€”β€”β–Ά justfile:1:14
β”‚
1 β”‚ set export :=
β”‚ ^
",
status: EXIT_FAILURE,
}
}

#[test]
fn invalid_attributes_are_an_error() {
Test::new()
.justfile(
"
[group: 'bar']
x := 'foo'
",
)
.args(["--evaluate", "x"])
.stderr(
"
error: Assignment `x` has invalid attribute `group`
β€”β€”β–Ά justfile:2:1
β”‚
2 β”‚ x := 'foo'
β”‚ ^
",
)
.status(EXIT_FAILURE)
.run();
}
1 change: 1 addition & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ mod allow_duplicate_variables;
mod assert_stdout;
mod assert_success;
mod assertions;
mod assignment;
mod attributes;
mod backticks;
mod byte_order_mark;
Expand Down

0 comments on commit 85c6e05

Please sign in to comment.