Don't evaluate comments (#1358)

This commit is contained in:
Casey Rodarmor 2022-10-04 22:33:19 -07:00 committed by GitHub
parent e445cfb47d
commit cd09d1e6d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 29 deletions

View File

@ -13,41 +13,39 @@ impl<'src> Line<'src> {
} }
pub(crate) fn is_comment(&self) -> bool { pub(crate) fn is_comment(&self) -> bool {
match self.fragments.first() { matches!(
Some(Fragment::Text { token }) => token.lexeme().starts_with('#'), self.fragments.first(),
_ => false, Some(Fragment::Text { token }) if token.lexeme().starts_with('#'),
} )
} }
pub(crate) fn is_continuation(&self) -> bool { pub(crate) fn is_continuation(&self) -> bool {
match self.fragments.last() { matches!(
Some(Fragment::Text { token }) => token.lexeme().ends_with('\\'), self.fragments.last(),
_ => false, Some(Fragment::Text { token }) if token.lexeme().ends_with('\\'),
} )
} }
pub(crate) fn is_shebang(&self) -> bool { pub(crate) fn is_shebang(&self) -> bool {
match self.fragments.first() { matches!(
Some(Fragment::Text { token }) => token.lexeme().starts_with("#!"), self.fragments.first(),
_ => false, Some(Fragment::Text { token }) if token.lexeme().starts_with("#!"),
} )
} }
pub(crate) fn is_quiet(&self) -> bool { pub(crate) fn is_quiet(&self) -> bool {
match self.fragments.first() { matches!(
Some(Fragment::Text { token }) => { self.fragments.first(),
token.lexeme().starts_with('@') || token.lexeme().starts_with("-@") Some(Fragment::Text { token })
} if token.lexeme().starts_with('@') || token.lexeme().starts_with("-@"),
_ => false, )
}
} }
pub(crate) fn is_infallible(&self) -> bool { pub(crate) fn is_infallible(&self) -> bool {
match self.fragments.first() { matches!(
Some(Fragment::Text { token }) => { self.fragments.first(),
token.lexeme().starts_with('-') || token.lexeme().starts_with("@-") Some(Fragment::Text { token })
} if token.lexeme().starts_with('-') || token.lexeme().starts_with("@-"),
_ => false, )
}
} }
} }

View File

@ -124,7 +124,9 @@ impl<'src, D> Recipe<'src, D> {
} }
let line = lines.next().unwrap(); let line = lines.next().unwrap();
line_number += 1; line_number += 1;
if !comment_line {
evaluated += &evaluator.evaluate_line(line, continued)?; evaluated += &evaluator.evaluate_line(line, continued)?;
}
if line.is_continuation() && !comment_line { if line.is_continuation() && !comment_line {
continued = true; continued = true;
evaluated.pop(); evaluated.pop();
@ -132,6 +134,11 @@ impl<'src, D> Recipe<'src, D> {
break; break;
} }
} }
if comment_line {
continue;
}
let mut command = evaluated.as_str(); let mut command = evaluated.as_str();
if quiet_command { if quiet_command {
@ -142,10 +149,6 @@ impl<'src, D> Recipe<'src, D> {
command = &command[1..]; command = &command[1..];
} }
if comment_line {
continue;
}
if command.is_empty() { if command.is_empty() {
continue; continue;
} }

View File

@ -83,3 +83,17 @@ fn continuations_with_echo_comments_true() {
.stderr("# comment lines can be continued echo something-useful\n") .stderr("# comment lines can be continued echo something-useful\n")
.run(); .run();
} }
#[test]
fn dont_evalute_comments() {
Test::new()
.justfile(
"
set ignore-comments
some_recipe:
# {{ error('foo') }}
",
)
.run();
}