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`. /// Not available under `no_module`.
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
Export(Box<[(Ident, Option<Ident>)]>, Position), Export(Box<[(Ident, Ident)]>, Position),
/// Convert a variable to shared. /// Convert a variable to shared.
/// ///
/// Not available under `no_closure`. /// Not available under `no_closure`.

View File

@ -2951,11 +2951,13 @@ impl Engine {
// Export statement // Export statement
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
Stmt::Export(list, _) => { 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 // Mark scope variables as public
if let Some(index) = scope.get_index(name).map(|(i, _)| i) { if let Some((index, _)) = scope.get_index(name) {
let alias = rename.as_ref().map(|x| &x.name).unwrap_or_else(|| name); scope.add_entry_alias(
scope.add_entry_alias(index, alias.clone()); index,
if rename.is_empty() { name } else { rename }.clone(),
);
} else { } else {
return EvalAltResult::ErrorVariableNotFound(name.to_string(), *pos).into(); 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 { loop {
let (id, id_pos) = parse_var_name(input)?; 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)?; let (name, pos) = parse_var_name(input)?;
if exports.iter().any(|(_, alias)| match alias { if exports.iter().any(|(_, alias)| alias.name == name) {
Some(Ident { name: alias, .. }) if alias == &name => true,
_ => false,
}) {
return Err(PERR::DuplicatedVariable(name).into_err(pos)); return Err(PERR::DuplicatedVariable(name).into_err(pos));
} }
let name = state.get_identifier(name); (name, pos)
Some(Ident { name, pos })
} else { } else {
None (Default::default(), Position::NONE)
}; };
exports.push(( exports.push((
@ -2436,7 +2432,10 @@ fn parse_export(
name: state.get_identifier(id), name: state.get_identifier(id),
pos: id_pos, pos: id_pos,
}, },
rename, Ident {
name: state.get_identifier(rename),
pos: rename_pos,
},
)); ));
match input.peek().expect(NEVER_ENDS) { match input.peek().expect(NEVER_ENDS) {