1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
use crate::perf::Check; use swc_ecma_ast::*; use swc_ecma_transforms_macros::fast_path; use swc_ecma_visit::{noop_fold_type, noop_visit_type, Fold, FoldWith, Node, Visit, VisitWith}; struct OptionalCatchBinding; pub fn optional_catch_binding() -> impl Fold { OptionalCatchBinding } #[fast_path(ShouldWork)] impl Fold for OptionalCatchBinding { noop_fold_type!(); fn fold_catch_clause(&mut self, mut cc: CatchClause) -> CatchClause { cc = cc.fold_children_with(self); if cc.param.is_some() { return cc; } CatchClause { param: Some(private_ident!("e").into()), ..cc } } } #[derive(Default)] struct ShouldWork { found: bool, } impl Visit for ShouldWork { noop_visit_type!(); fn visit_catch_clause(&mut self, n: &CatchClause, _: &dyn Node) { if n.param.is_none() { self.found = true; return; } n.visit_children_with(self) } } impl Check for ShouldWork { fn should_handle(&self) -> bool { self.found } } #[cfg(test)] mod tests { use super::optional_catch_binding as tr; test!( ::swc_ecma_parser::Syntax::default(), |_| tr(), issue_411, "try {} catch {}", "try {} catch(e) {}" ); }