Strip leading spaces after line continuation (#635)

Make line continuations strip leading spaces on the next line.

This changes what is passed to the shell, so this is a breaking change.
However, I don't think that this will break any recipes.
This commit is contained in:
Casey Rodarmor 2020-06-08 22:37:12 -07:00 committed by GitHub
parent 4fdb771731
commit 2a9c29c18f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 9 deletions

View File

@ -145,11 +145,20 @@ impl<'src, 'run> Evaluator<'src, 'run> {
}) })
} }
pub(crate) fn evaluate_line(&mut self, line: &Line<'src>) -> RunResult<'src, String> { pub(crate) fn evaluate_line(
&mut self,
line: &Line<'src>,
continued: bool,
) -> RunResult<'src, String> {
let mut evaluated = String::new(); let mut evaluated = String::new();
for fragment in &line.fragments { for (i, fragment) in line.fragments.iter().enumerate() {
match fragment { match fragment {
Fragment::Text { token } => evaluated += token.lexeme(), Fragment::Text { token } =>
if i == 0 && continued {
evaluated += token.lexeme().trim_start();
} else {
evaluated += token.lexeme();
},
Fragment::Interpolation { expression } => { Fragment::Interpolation { expression } => {
evaluated += &self.evaluate_expression(expression)?; evaluated += &self.evaluate_expression(expression)?;
}, },

View File

@ -89,7 +89,7 @@ impl<'src, D> Recipe<'src, D> {
if self.shebang { if self.shebang {
let mut evaluated_lines = vec![]; let mut evaluated_lines = vec![];
for line in &self.body { for line in &self.body {
evaluated_lines.push(evaluator.evaluate_line(line)?); evaluated_lines.push(evaluator.evaluate_line(line, false)?);
} }
if config.dry_run || self.quiet { if config.dry_run || self.quiet {
@ -205,14 +205,16 @@ impl<'src, D> Recipe<'src, D> {
break; break;
} }
let mut evaluated = String::new(); let mut evaluated = String::new();
let mut continued = false;
loop { loop {
if lines.peek().is_none() { if lines.peek().is_none() {
break; break;
} }
let line = lines.next().unwrap(); let line = lines.next().unwrap();
line_number += 1; line_number += 1;
evaluated += &evaluator.evaluate_line(line)?; evaluated += &evaluator.evaluate_line(line, continued)?;
if line.is_continuation() { if line.is_continuation() {
continued = true;
evaluated.pop(); evaluated.pop();
} else { } else {
break; break;

View File

@ -1292,8 +1292,8 @@ foo:
b \ b \
c c
"#, "#,
stdout: "a b c\n", stdout: "ab c\n",
stderr: "echo a b c\n", stderr: "echo ab c\n",
} }
test! { test! {
@ -1304,8 +1304,8 @@ foo:
b \ b \
c' c'
"#, "#,
stdout: "a b c\n", stdout: "ab c\n",
stderr: "echo 'a b c'\n", stderr: "echo 'ab c'\n",
} }
test! { test! {