Fix multiword imports/exports (#336)

This commit is contained in:
JT 2021-11-15 08:40:26 +13:00 committed by GitHub
parent f1b2ab0b27
commit be827e5628
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 24 deletions

4
Cargo.lock generated
View File

@ -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",

View File

@ -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");

View File

@ -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<ParseError> {
// 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<ParseError>) {
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<ParseError>) {
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<ParseError>) {
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,

View File

@ -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