From e8a7643a8867b65823d9176a8b156783b36649fb Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Fri, 4 Oct 2024 22:54:25 -0700 Subject: [PATCH] Forbid invalid attributes on assignments --- src/parser.rs | 17 ++++++++++++++-- tests/assignment.rs | 47 ++++++++++++++++++++++++++++++++++----------- tests/lib.rs | 1 + 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index da2ff76f3f..c9b499d0e6 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -480,18 +480,31 @@ impl<'run, 'src> Parser<'run, 'src> { fn parse_assignment( &mut self, export: bool, - attributes: BTreeSet>, + attributes: BTreeSet>, ) -> 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('_'), }) } diff --git a/tests/assignment.rs b/tests/assignment.rs index ae6ea6ebd2..efacc2c638 100644 --- a/tests/assignment.rs +++ b/tests/assignment.rs @@ -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, -} \ No newline at end of file +} + +#[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(); +} diff --git a/tests/lib.rs b/tests/lib.rs index 681207f3c5..ec71c665da 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -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;