Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forbid invalid attributes on assignments #2412

Merged
merged 1 commit into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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