Merge pull request #72 from nushell/import_patterns

Add import lists
This commit is contained in:
JT 2021-09-27 13:32:36 +13:00 committed by GitHub
commit 0a48bc973d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 1 deletions

View File

@ -400,6 +400,24 @@ pub fn parse_use(
new_exports
}
ImportPatternMember::List { names } => {
let mut output = vec![];
for (name, span) in names {
let mut new_exports: Vec<(Vec<u8>, usize)> = exports
.iter()
.filter_map(|x| if &x.0 == name { Some(x.clone()) } else { None })
.collect();
if new_exports.is_empty() {
error = error.or(Some(ParseError::ExportNotFound(*span)))
} else {
output.append(&mut new_exports)
}
}
output
}
}
};

View File

@ -1376,7 +1376,7 @@ pub fn parse_type(_working_set: &StateWorkingSet, bytes: &[u8]) -> Type {
}
pub fn parse_import_pattern(
working_set: &StateWorkingSet,
working_set: &mut StateWorkingSet,
span: Span,
) -> (ImportPattern, Option<ParseError>) {
let source = working_set.get_span_contents(span);
@ -1409,6 +1409,38 @@ pub fn parse_import_pattern(
},
error,
)
} else if tail.starts_with(b"[") {
let (result, err) = parse_list_expression(working_set, tail_span, &SyntaxShape::String);
error = error.or(err);
let mut output = vec![];
match result {
Expression {
expr: Expr::List(list),
..
} => {
for l in list {
let contents = working_set.get_span_contents(l.span);
output.push((contents.to_vec(), l.span));
}
(
ImportPattern {
head,
members: vec![ImportPatternMember::List { names: output }],
},
error,
)
}
_ => (
ImportPattern {
head,
members: vec![],
},
Some(ParseError::ExportNotFound(result.span)),
),
}
} else {
(
ImportPattern {

View File

@ -4,6 +4,7 @@ use crate::Span;
pub enum ImportPatternMember {
Glob { span: Span },
Name { name: Vec<u8>, span: Span },
List { names: Vec<(Vec<u8>, Span)> },
}
#[derive(Debug, Clone)]

View File

@ -374,3 +374,11 @@ fn module_imports_4() -> TestResult {
"not find import",
)
}
#[test]
fn module_imports_5() -> TestResult {
run_test(
r#"module foo { def a [] { 1 }; def b [] { 2 }; def c [] { 3 } }; use foo.[a, c]; c"#,
"3",
)
}