Return Error on str replace RegEx parse fail (#6695)

This commit is contained in:
Jake Albert 2022-10-10 21:27:01 +09:00 committed by GitHub
parent fde56cfe99
commit 34c8b276ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,8 +9,8 @@ use std::sync::Arc;
struct Arguments { struct Arguments {
all: bool, all: bool,
find: String, find: Spanned<String>,
replace: String, replace: Spanned<String>,
column_paths: Vec<CellPath>, column_paths: Vec<CellPath>,
literal_replace: bool, literal_replace: bool,
no_regex: bool, no_regex: bool,
@ -168,8 +168,8 @@ fn operate(
let options = Arc::new(Arguments { let options = Arc::new(Arguments {
all: call.has_flag("all"), all: call.has_flag("all"),
find: find.item, find,
replace: replace.item, replace,
column_paths: call.rest(engine_state, stack, 2)?, column_paths: call.rest(engine_state, stack, 2)?,
literal_replace, literal_replace,
no_regex, no_regex,
@ -214,23 +214,23 @@ fn action(
) -> Value { ) -> Value {
match input { match input {
Value::String { val, .. } => { Value::String { val, .. } => {
let FindReplace(find, replacement) = FindReplace(find, replace); let FindReplace(find_str, replace_str) = FindReplace(&find.item, &replace.item);
if *no_regex { if *no_regex {
// just use regular string replacement vs regular expressions // just use regular string replacement vs regular expressions
if *all { if *all {
Value::String { Value::String {
val: val.replace(find, replacement), val: val.replace(find_str, replace_str),
span: head, span: head,
} }
} else { } else {
Value::String { Value::String {
val: val.replacen(find, replacement, 1), val: val.replacen(find_str, replace_str, 1),
span: head, span: head,
} }
} }
} else { } else {
// use regular expressions to replace strings // use regular expressions to replace strings
let regex = Regex::new(find); let regex = Regex::new(find_str);
match regex { match regex {
Ok(re) => { Ok(re) => {
@ -238,9 +238,9 @@ fn action(
Value::String { Value::String {
val: { val: {
if *literal_replace { if *literal_replace {
re.replace_all(val, NoExpand(replacement)).to_string() re.replace_all(val, NoExpand(replace_str)).to_string()
} else { } else {
re.replace_all(val, replacement).to_string() re.replace_all(val, replace_str).to_string()
} }
}, },
span: head, span: head,
@ -249,18 +249,17 @@ fn action(
Value::String { Value::String {
val: { val: {
if *literal_replace { if *literal_replace {
re.replace(val, NoExpand(replacement)).to_string() re.replace(val, NoExpand(replace_str)).to_string()
} else { } else {
re.replace(val, replacement).to_string() re.replace(val, replace_str).to_string()
} }
}, },
span: head, span: head,
} }
} }
} }
Err(_) => Value::String { Err(e) => Value::Error {
val: val.to_string(), error: ShellError::UnsupportedInput(format!("{e}"), find.span),
span: head,
}, },
} }
} }
@ -282,6 +281,13 @@ mod tests {
use super::*; use super::*;
use super::{action, Arguments, SubCommand}; use super::{action, Arguments, SubCommand};
fn test_spanned_string(val: &str) -> Spanned<String> {
Spanned {
item: String::from(val),
span: Span::test_data(),
}
}
#[test] #[test]
fn test_examples() { fn test_examples() {
use crate::test_examples; use crate::test_examples;
@ -297,8 +303,8 @@ mod tests {
}; };
let options = Arguments { let options = Arguments {
find: String::from("Cargo.(.+)"), find: test_spanned_string("Cargo.(.+)"),
replace: String::from("Carga.$1"), replace: test_spanned_string("Carga.$1"),
column_paths: vec![], column_paths: vec![],
literal_replace: false, literal_replace: false,
all: false, all: false,