Optional try catch binding (#422)

swc_ecma_parser:
 - Add test for optional try catch binding
 
swc_ecma_transforms:
 - Implement optional catch binding (#411)
This commit is contained in:
강동윤 2019-09-22 19:31:32 +09:00 committed by GitHub
parent febe34e280
commit d7d22c2d5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 122 additions and 67 deletions

View File

@ -25,8 +25,6 @@
</a>
</p>
<h2 align="center">Supporting swc</h2>
<p align="center">
@ -38,7 +36,6 @@
</a>
</p>
swc is a community-driven project, and is maintained by a group of [volunteers](https://opencollective.com/swc#team). If you'd like to help support the future of the project, please consider:
- Giving developer time on the project. (Message us on [Slack](https://swc-org.slack.com/) for guidance!)
@ -69,7 +66,6 @@ Become a sponsor and get your logo on our README on Github with a link to your s
<a href="https://opencollective.com/swc/backer/8/website?requireActive=false" target="_blank"><img src="https://opencollective.com/swc/backer/8/avatar.svg?requireActive=false"></a>
<a href="https://opencollective.com/swc/backer/9/website?requireActive=false" target="_blank"><img src="https://opencollective.com/swc/backer/9/avatar.svg?requireActive=false"></a>
# Documentation
Check out the documentation [in the website](https://swc-project.github.io/docs/installation).
@ -77,22 +73,27 @@ Check out the documentation [in the website](https://swc-project.github.io/docs/
# Features
## Parsers
- [x] es2019
- [x] jsx
- [x] typescript
- [x] es2019
- [x] jsx
- [x] typescript
## Transforms
New generation javascript to old-days javascript.
- es3
- es3
- [x] member-expression-literals
- [x] property-literals
- [x] reserved-words
- es5
- es5
- [ ] property-mutators
- es2015
- es2015
- [x] arrow-functions
- [x] block-scoped-functions
- [x] block-scoping
@ -115,21 +116,24 @@ New generation javascript to old-days javascript.
- [x] typeof-symbol
- [ ] unicode-regex
- es2016
- es2016
- [x] exponentiation-operator
- es2017
- es2017
- [x] async-to-generator
- es2018
- es2018
- [ ] async-generator-functions
- [ ] dotall-regex
- [x] object-rest-spread
- [ ] Using symbol as a key
- [ ] optional-catch-binding
- [x] optional-catch-binding
- [ ] unicode-property-regex
- react
- react
- [x] jsx
# Performance
@ -137,16 +141,15 @@ New generation javascript to old-days javascript.
The lower bound of the speedup compared to babel is **16**. The benchmarks were run on Macbook pro, dual core, 2.3GHz Intel Core i5, 16 GB ram
| | performance |
| ------------------------ |:--------------------------------------:|
| ------------------ | :-------------------------------------: |
| swc (es3) | 610 ops/sec ±1.76% (82 runs sampled) |
| swc (es2015) | 682 ops/sec ±0.63% (88 runs sampled) |
| swc (es2016) | 1,659 ops/sec ±4.32% (79 runs sampled) |
| swc (es2017) | 1,384 ops/sec ±7.24% (82 runs sampled) |
| swc (es2018) | 1,765 ops/sec ±11.78% (82 runs sampled)|
| swc (es2018) | 1,765 ops/sec ±11.78% (82 runs sampled) |
| swc-optimize (es3) | 535 ops/sec ±1.01% (83 runs sampled) |
| babel | 42.12 ops/sec ±6.27% (55 runs sampled) |
## Contributing
See [CONTRIBUTING.md](CONTRIBUTING.md). You may also find the architecture
@ -159,8 +162,6 @@ and the Apache License (Version 2.0).
See LICENSE-APACHE and LICENSE-MIT for details.
[babel]:https://github.com/babel/babel
[closure compiler]:https://github.com/google/closure-compiler
[rust]:https://www.rust-lang.org
[babel]: https://github.com/babel/babel
[closure compiler]: https://github.com/google/closure-compiler
[rust]: https://www.rust-lang.org

View File

@ -1347,4 +1347,20 @@ export default function waitUntil(callback, options = {}) {
);
}
#[test]
fn issue_411() {
test_parser(
"try {
} catch {}",
Syntax::Es(EsConfig {
..Default::default()
}),
|p| {
p.parse_module().map_err(|mut e| {
e.emit();
()
})
},
);
}
}

View File

@ -1,9 +1,12 @@
pub use self::object_rest_spread::object_rest_spread;
pub use self::{
object_rest_spread::object_rest_spread, optional_catch_binding::optional_catch_binding,
};
use crate::pass::Pass;
use ast::Module;
mod object_rest_spread;
mod optional_catch_binding;
pub fn es2018() -> impl Pass {
object_rest_spread()
chain_at!(Module, object_rest_spread(), optional_catch_binding())
}

View File

@ -0,0 +1,35 @@
use crate::pass::Pass;
use ast::*;
use swc_common::Fold;
struct OptionalCatchBinding;
pub fn optional_catch_binding() -> impl Pass {
OptionalCatchBinding
}
impl Fold<CatchClause> for OptionalCatchBinding {
fn fold(&mut self, cc: CatchClause) -> CatchClause {
if cc.param.is_some() {
return cc;
}
CatchClause {
param: Some(private_ident!("e").into()),
..cc
}
}
}
#[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) {}"
);
}