From cf88c8eef3dbc0f010f09e8a2572aaed7d67f1e4 Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Sun, 27 Mar 2022 12:52:09 +1300 Subject: [PATCH] Improve escaping in string interpolation (#4982) --- crates/nu-parser/src/parser.rs | 9 ++++++++- src/tests/test_parser.rs | 5 +++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index 97e9cbf95f..cadce688a5 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -1438,7 +1438,14 @@ pub fn parse_string_interpolation( #[allow(clippy::needless_range_loop)] while b != end { - if contents[b - start] == b'(' && mode == InterpolationMode::String { + if contents[b - start] == b'(' + && (if double_quote && (b - start) > 0 { + contents[b - start - 1] != b'\\' + } else { + true + }) + && mode == InterpolationMode::String + { mode = InterpolationMode::Expression; if token_start < b { let span = Span { diff --git a/src/tests/test_parser.rs b/src/tests/test_parser.rs index 877e06f798..4b6eaab57a 100644 --- a/src/tests/test_parser.rs +++ b/src/tests/test_parser.rs @@ -345,6 +345,11 @@ fn string_escape_interpolation() -> TestResult { run_test(r#"$"\u015B(char hamburger)abc""#, "ś≡abc") } +#[test] +fn string_escape_interpolation2() -> TestResult { + run_test(r#"$"2 + 2 is \(2 + 2)""#, "2 + 2 is (2 + 2)") +} + #[test] fn proper_rest_types() -> TestResult { run_test(