Simplify Expr::Export.

This commit is contained in:
Stephen Chung 2021-06-16 16:35:56 +08:00
parent 0a857e6944
commit aa2e04bd25
3 changed files with 16 additions and 15 deletions

View File

@ -1011,7 +1011,7 @@ pub enum Stmt {
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
Export(Box<[(Ident, Option<Ident>)]>, Position),
Export(Box<[(Ident, Ident)]>, Position),
/// Convert a variable to shared.
///
/// Not available under `no_closure`.

View File

@ -2951,11 +2951,13 @@ impl Engine {
// Export statement
#[cfg(not(feature = "no_module"))]
Stmt::Export(list, _) => {
for (Ident { name, pos, .. }, rename) in list.iter() {
for (Ident { name, pos, .. }, Ident { name: rename, .. }) in list.as_ref() {
// Mark scope variables as public
if let Some(index) = scope.get_index(name).map(|(i, _)| i) {
let alias = rename.as_ref().map(|x| &x.name).unwrap_or_else(|| name);
scope.add_entry_alias(index, alias.clone());
if let Some((index, _)) = scope.get_index(name) {
scope.add_entry_alias(
index,
if rename.is_empty() { name } else { rename }.clone(),
);
} else {
return EvalAltResult::ErrorVariableNotFound(name.to_string(), *pos).into();
}

View File

@ -2412,23 +2412,19 @@ fn parse_export(
_ => (),
}
let mut exports = Vec::with_capacity(4);
let mut exports = Vec::<(Ident, Ident)>::with_capacity(4);
loop {
let (id, id_pos) = parse_var_name(input)?;
let rename = if match_token(input, Token::As).0 {
let (rename, rename_pos) = if match_token(input, Token::As).0 {
let (name, pos) = parse_var_name(input)?;
if exports.iter().any(|(_, alias)| match alias {
Some(Ident { name: alias, .. }) if alias == &name => true,
_ => false,
}) {
if exports.iter().any(|(_, alias)| alias.name == name) {
return Err(PERR::DuplicatedVariable(name).into_err(pos));
}
let name = state.get_identifier(name);
Some(Ident { name, pos })
(name, pos)
} else {
None
(Default::default(), Position::NONE)
};
exports.push((
@ -2436,7 +2432,10 @@ fn parse_export(
name: state.get_identifier(id),
pos: id_pos,
},
rename,
Ident {
name: state.get_identifier(rename),
pos: rename_pos,
},
));
match input.peek().expect(NEVER_ENDS) {