diff --git a/Cargo.lock b/Cargo.lock index e443b9e7d8..299fb969ed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1328,9 +1328,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.69" +version = "1.0.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e466864e431129c7e0d3476b92f20458e5879919a0596c6472738d9fa2d342f8" +checksum = "e277c495ac6cd1a01a58d0a0c574568b4d1ddf14f59965c6a58b8d96400b54f3" dependencies = [ "itoa", "ryu", diff --git a/crates/nu-parser/src/parse_keywords.rs b/crates/nu-parser/src/parse_keywords.rs index 030ef73882..b6486e7798 100644 --- a/crates/nu-parser/src/parse_keywords.rs +++ b/crates/nu-parser/src/parse_keywords.rs @@ -12,7 +12,7 @@ use crate::{ lex, lite_parse, parser::{ check_name, garbage, garbage_statement, parse, parse_block_expression, - parse_import_pattern, parse_internal_call, parse_signature, parse_string, + parse_import_pattern, parse_internal_call, parse_signature, parse_string, trim_quotes, }, ParseError, }; @@ -357,6 +357,8 @@ pub fn parse_module_block( // parts[2] is safe since it's checked in parse_export already working_set.get_span_contents(pipeline.commands[0].parts[2]); + let decl_name = trim_quotes(decl_name); + let decl_id = working_set .find_decl(decl_name) .expect("internal error: failed to find added declaration"); diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index 3f958b3bb3..f697be0ab1 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -69,6 +69,16 @@ fn is_variable(bytes: &[u8]) -> bool { } } +pub fn trim_quotes(bytes: &[u8]) -> &[u8] { + if (bytes.starts_with(b"\"") && bytes.ends_with(b"\"") && bytes.len() > 1) + || (bytes.starts_with(b"\'") && bytes.ends_with(b"\'") && bytes.len() > 1) + { + &bytes[1..(bytes.len() - 1)] + } else { + bytes + } +} + fn check_call(command: Span, sig: &Signature, call: &Call) -> Option { // Allow the call to pass if they pass in the help flag if call.named.iter().any(|(n, _)| n.item == "help") { @@ -1423,13 +1433,7 @@ pub fn parse_filepath( span: Span, ) -> (Expression, Option) { let bytes = working_set.get_span_contents(span); - let bytes = if (bytes.starts_with(b"\"") && bytes.ends_with(b"\"") && bytes.len() > 1) - || (bytes.starts_with(b"\'") && bytes.ends_with(b"\'") && bytes.len() > 1) - { - &bytes[1..(bytes.len() - 1)] - } else { - bytes - }; + let bytes = trim_quotes(bytes); if let Ok(token) = String::from_utf8(bytes.into()) { ( @@ -1641,13 +1645,7 @@ pub fn parse_glob_pattern( span: Span, ) -> (Expression, Option) { let bytes = working_set.get_span_contents(span); - let bytes = if (bytes.starts_with(b"\"") && bytes.ends_with(b"\"") && bytes.len() > 1) - || (bytes.starts_with(b"\'") && bytes.ends_with(b"\'") && bytes.len() > 1) - { - &bytes[1..(bytes.len() - 1)] - } else { - bytes - }; + let bytes = trim_quotes(bytes); if let Ok(token) = String::from_utf8(bytes.into()) { ( @@ -1672,13 +1670,7 @@ pub fn parse_string( span: Span, ) -> (Expression, Option) { let bytes = working_set.get_span_contents(span); - let bytes = if (bytes.starts_with(b"\"") && bytes.ends_with(b"\"") && bytes.len() > 1) - || (bytes.starts_with(b"\'") && bytes.ends_with(b"\'") && bytes.len() > 1) - { - &bytes[1..(bytes.len() - 1)] - } else { - bytes - }; + let bytes = trim_quotes(bytes); if let Ok(token) = String::from_utf8(bytes.into()) { ( @@ -1853,6 +1845,7 @@ pub fn parse_import_pattern( ), } } else { + let tail = trim_quotes(tail); ( ImportPattern { head, diff --git a/src/tests.rs b/src/tests.rs index bc7355ecd7..6964b8fd23 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -900,6 +900,14 @@ fn record_2() -> TestResult { run_test(r#"{'b': 'c'}.b"#, "c") } +#[test] +fn multi_word_imports() -> TestResult { + run_test( + r#"module spam { export def "foo bar" [] { 10 } }; use spam "foo bar"; foo bar"#, + "10", + ) +} + #[test] fn config_var_1() -> TestResult { // Note: this tests both the config variable and that it is properly captured into a block