Skip to content

Commit

Permalink
refactor(es/typescript): Extract type annotation proposal out (#9127)
Browse files Browse the repository at this point in the history
**Description:**

The Node.js team wants a Wasm package that only implements type annotation proposals. So, we need to refactor our TypeScript transform to allow it.


**Related issue:**

 - nodejs/loaders#208 (comment)
  • Loading branch information
kdy1 authored Jul 3, 2024
1 parent eda493c commit dfee5f8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
23 changes: 11 additions & 12 deletions bindings/binding_typescript_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ use swc_core::{
parser::{
parse_file_as_module, parse_file_as_program, parse_file_as_script, Syntax, TsSyntax,
},
transforms::base::{
fixer::fixer,
helpers::{inject_helpers, Helpers, HELPERS},
hygiene::hygiene,
resolver,
transforms::{
base::{
fixer::fixer,
helpers::{inject_helpers, Helpers, HELPERS},
hygiene::hygiene,
resolver,
},
typescript::strip_type,
},
visit::VisitMutWith,
},
Expand Down Expand Up @@ -52,9 +55,8 @@ pub struct Options {
#[serde(default)]
pub source_maps: bool,

#[serde(default)]
pub transform: swc_core::ecma::transforms::typescript::Config,

// #[serde(default)]
// pub transform: swc_core::ecma::transforms::typescript::Config,
#[serde(default)]
pub codegen: swc_core::ecma::codegen::Config,
}
Expand Down Expand Up @@ -148,10 +150,7 @@ fn operate(input: String, options: Options) -> Result<TransformOutput, Error> {

// Strip typescript types

program.visit_mut_with(&mut swc_core::ecma::transforms::typescript::typescript(
options.transform,
top_level_mark,
));
program.visit_mut_with(&mut strip_type());

// Apply external helpers

Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_transforms_typescript/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![allow(clippy::vec_box)]
#![allow(clippy::mutable_key_type)]

pub use self::typescript::*;
pub use self::{strip_type::*, typescript::*};
mod config;
mod macros;
mod strip_import_export;
Expand Down
4 changes: 4 additions & 0 deletions crates/swc_ecma_transforms_typescript/src/strip_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ use swc_ecma_visit::{VisitMut, VisitMutWith};

use crate::{type_to_none, unreachable_visit_mut_type};

pub fn strip_type() -> impl VisitMut {
StripType::default()
}

/// This Module will strip all types/generics/interface/declares
/// and type import/export
#[derive(Default)]
Expand Down

1 comment on commit dfee5f8

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: dfee5f8 Previous: f409bc0 Ratio
es/full/bugs-1 271823 ns/iter (± 3130) 274000 ns/iter (± 3596) 0.99
es/full/minify/libraries/antd 1863842592 ns/iter (± 53582346) 1867388876 ns/iter (± 70678388) 1.00
es/full/minify/libraries/d3 352590093 ns/iter (± 7910659) 352110699 ns/iter (± 2553705) 1.00
es/full/minify/libraries/echarts 1582995152 ns/iter (± 38987641) 1538011402 ns/iter (± 32109226) 1.03
es/full/minify/libraries/jquery 104773834 ns/iter (± 987597) 102811585 ns/iter (± 428763) 1.02
es/full/minify/libraries/lodash 114513042 ns/iter (± 1865510) 114093012 ns/iter (± 369905) 1.00
es/full/minify/libraries/moment 59679843 ns/iter (± 282125) 59739007 ns/iter (± 232313) 1.00
es/full/minify/libraries/react 18297506 ns/iter (± 87423) 18341306 ns/iter (± 23418) 1.00
es/full/minify/libraries/terser 284575376 ns/iter (± 6247233) 272550378 ns/iter (± 2323114) 1.04
es/full/minify/libraries/three 485299015 ns/iter (± 7869889) 486859159 ns/iter (± 9105687) 1.00
es/full/minify/libraries/typescript 3345884124 ns/iter (± 43121690) 3254511146 ns/iter (± 23506092) 1.03
es/full/minify/libraries/victory 796110990 ns/iter (± 34243512) 747799052 ns/iter (± 11886263) 1.06
es/full/minify/libraries/vue 150026429 ns/iter (± 2035132) 146022982 ns/iter (± 468114) 1.03
es/full/codegen/es3 58972 ns/iter (± 205) 58446 ns/iter (± 233) 1.01
es/full/codegen/es5 59118 ns/iter (± 247) 58594 ns/iter (± 172) 1.01
es/full/codegen/es2015 58838 ns/iter (± 104) 58969 ns/iter (± 560) 1.00
es/full/codegen/es2016 58992 ns/iter (± 135) 58602 ns/iter (± 234) 1.01
es/full/codegen/es2017 58651 ns/iter (± 213) 58238 ns/iter (± 198) 1.01
es/full/codegen/es2018 58908 ns/iter (± 174) 58463 ns/iter (± 120) 1.01
es/full/codegen/es2019 58869 ns/iter (± 172) 58494 ns/iter (± 174) 1.01
es/full/codegen/es2020 59093 ns/iter (± 197) 58398 ns/iter (± 125) 1.01
es/full/all/es3 164581425 ns/iter (± 1635223) 164288782 ns/iter (± 714946) 1.00
es/full/all/es5 157774218 ns/iter (± 459558) 158522167 ns/iter (± 1498397) 1.00
es/full/all/es2015 116786440 ns/iter (± 635207) 116970665 ns/iter (± 602849) 1.00
es/full/all/es2016 116088298 ns/iter (± 732581) 116391936 ns/iter (± 765342) 1.00
es/full/all/es2017 115458818 ns/iter (± 500864) 115888413 ns/iter (± 926220) 1.00
es/full/all/es2018 113615567 ns/iter (± 1445391) 113785430 ns/iter (± 737822) 1.00
es/full/all/es2019 113108279 ns/iter (± 481733) 112918411 ns/iter (± 693290) 1.00
es/full/all/es2020 108897053 ns/iter (± 541266) 108808199 ns/iter (± 1226154) 1.00
es/full/parser 476073 ns/iter (± 3527) 483864 ns/iter (± 2768) 0.98
es/full/base/fixer 16326 ns/iter (± 142) 16262 ns/iter (± 226) 1.00
es/full/base/resolver_and_hygiene 80185 ns/iter (± 272) 81469 ns/iter (± 297) 0.98
serialization of serde 272 ns/iter (± 1) 276 ns/iter (± 2) 0.99
css/minify/libraries/bootstrap 21988681 ns/iter (± 106960) 22045300 ns/iter (± 107001) 1.00
css/visitor/compare/clone 1378273 ns/iter (± 4730) 1352744 ns/iter (± 9654) 1.02
css/visitor/compare/visit_mut_span 1509342 ns/iter (± 8078) 1485512 ns/iter (± 7914) 1.02
css/visitor/compare/visit_mut_span_panic 1493289 ns/iter (± 40735) 1518785 ns/iter (± 17201) 0.98
css/visitor/compare/fold_span 2002076 ns/iter (± 7929) 1978817 ns/iter (± 8989) 1.01
css/visitor/compare/fold_span_panic 2127608 ns/iter (± 10023) 2110123 ns/iter (± 5153) 1.01
css/lexer/bootstrap_5_1_3 3473056 ns/iter (± 2527) 3434317 ns/iter (± 1757) 1.01
css/lexer/foundation_6_7_4 2800058 ns/iter (± 1531) 2776297 ns/iter (± 2381) 1.01
css/lexer/tailwind_3_1_1 540376 ns/iter (± 967) 533906 ns/iter (± 165) 1.01
css/parser/bootstrap_5_1_3 15936355 ns/iter (± 32436) 15929727 ns/iter (± 31783) 1.00
css/parser/foundation_6_7_4 12771280 ns/iter (± 45300) 12706319 ns/iter (± 15995) 1.01
css/parser/tailwind_3_1_1 2524658 ns/iter (± 5329) 2511700 ns/iter (± 19665) 1.01
es/codegen/colors 723865 ns/iter (± 398987) 721114 ns/iter (± 396159) 1.00
es/codegen/large 2998386 ns/iter (± 1604948) 2972283 ns/iter (± 1574576) 1.01
es/codegen/with-parser/colors 40497 ns/iter (± 569) 40684 ns/iter (± 266) 1.00
es/codegen/with-parser/large 457452 ns/iter (± 2546) 445924 ns/iter (± 1268) 1.03
es/minify/libraries/antd 1601709836 ns/iter (± 26132240) 1520230922 ns/iter (± 23791607) 1.05
es/minify/libraries/d3 331733566 ns/iter (± 20435978) 298198337 ns/iter (± 4293539) 1.11
es/minify/libraries/echarts 1318155078 ns/iter (± 24239452) 1255871698 ns/iter (± 23614936) 1.05
es/minify/libraries/jquery 86351469 ns/iter (± 1132482) 83584753 ns/iter (± 875823) 1.03
es/minify/libraries/lodash 105698229 ns/iter (± 1601013) 104785760 ns/iter (± 338982) 1.01
es/minify/libraries/moment 49110479 ns/iter (± 733539) 48568632 ns/iter (± 121285) 1.01
es/minify/libraries/react 16433213 ns/iter (± 124186) 16267307 ns/iter (± 32374) 1.01
es/minify/libraries/terser 240845266 ns/iter (± 8559171) 222394666 ns/iter (± 1291884) 1.08
es/minify/libraries/three 421388584 ns/iter (± 6999588) 392850393 ns/iter (± 2777221) 1.07
es/minify/libraries/typescript 2913831474 ns/iter (± 29123005) 2834660403 ns/iter (± 29148899) 1.03
es/minify/libraries/victory 726810287 ns/iter (± 22853968) 660626365 ns/iter (± 7188725) 1.10
es/minify/libraries/vue 126931774 ns/iter (± 3676179) 122886922 ns/iter (± 323236) 1.03
es/visitor/compare/clone 1728206 ns/iter (± 12213) 1716401 ns/iter (± 45944) 1.01
es/visitor/compare/visit_mut_span 2090080 ns/iter (± 25475) 2050994 ns/iter (± 10554) 1.02
es/visitor/compare/visit_mut_span_panic 2084262 ns/iter (± 11733) 2075724 ns/iter (± 55146) 1.00
es/visitor/compare/fold_span 2831836 ns/iter (± 14039) 2772107 ns/iter (± 14394) 1.02
es/visitor/compare/fold_span_panic 2881987 ns/iter (± 10009) 2808046 ns/iter (± 5320) 1.03
es/lexer/colors 9387 ns/iter (± 70) 9316 ns/iter (± 25) 1.01
es/lexer/angular 4882527 ns/iter (± 37025) 4899378 ns/iter (± 4030) 1.00
es/lexer/backbone 634496 ns/iter (± 9494) 636589 ns/iter (± 2191) 1.00
es/lexer/jquery 3677202 ns/iter (± 7965) 3686099 ns/iter (± 10051) 1.00
es/lexer/jquery mobile 5293844 ns/iter (± 16815) 5410374 ns/iter (± 66093) 0.98
es/lexer/mootools 2890028 ns/iter (± 48916) 2943459 ns/iter (± 12490) 0.98
es/lexer/underscore 536675 ns/iter (± 1144) 521192 ns/iter (± 1575) 1.03
es/lexer/three 16250513 ns/iter (± 111407) 16034195 ns/iter (± 278863) 1.01
es/lexer/yui 2849974 ns/iter (± 5241) 2883930 ns/iter (± 14874) 0.99
es/lexer/cal-com 13147498 ns/iter (± 98228) 13102589 ns/iter (± 29622) 1.00
es/lexer/typescript 94250171 ns/iter (± 726624) 91030338 ns/iter (± 281894) 1.04
es/parser/colors 23620 ns/iter (± 60) 23426 ns/iter (± 117) 1.01
es/parser/angular 12908302 ns/iter (± 216169) 12436912 ns/iter (± 199609) 1.04
es/parser/backbone 1819739 ns/iter (± 5954) 1837345 ns/iter (± 22427) 0.99
es/parser/jquery 10243780 ns/iter (± 187543) 10152342 ns/iter (± 68470) 1.01
es/parser/jquery mobile 15632117 ns/iter (± 138604) 15232232 ns/iter (± 94607) 1.03
es/parser/mootools 7968342 ns/iter (± 41819) 7894225 ns/iter (± 133839) 1.01
es/parser/underscore 1595909 ns/iter (± 9262) 1604871 ns/iter (± 6154) 0.99
es/parser/three 45962006 ns/iter (± 982007) 44907720 ns/iter (± 612858) 1.02
es/parser/yui 7456901 ns/iter (± 117778) 7510838 ns/iter (± 43268) 0.99
es/parser/cal-com 42144574 ns/iter (± 569423) 39138120 ns/iter (± 358779) 1.08
es/parser/typescript 276738464 ns/iter (± 3192106) 270060428 ns/iter (± 688433) 1.02
es/preset-env/usage/builtin_type 138226 ns/iter (± 33534) 134992 ns/iter (± 33375) 1.02
es/preset-env/usage/property 17616 ns/iter (± 143) 16471 ns/iter (± 96) 1.07
es/resolver/typescript 104402449 ns/iter (± 2279347) 109498620 ns/iter (± 2601643) 0.95
es/fixer/typescript 82721425 ns/iter (± 2785561) 88414745 ns/iter (± 4427929) 0.94
es/hygiene/typescript 190348197 ns/iter (± 3851904) 205070702 ns/iter (± 2222072) 0.93
es/resolver_with_hygiene/typescript 319020439 ns/iter (± 5635033) 341406730 ns/iter (± 3201739) 0.93
es/visitor/base-perf/module_clone 56642 ns/iter (± 498) 56740 ns/iter (± 528) 1.00
es/visitor/base-perf/fold_empty 59913 ns/iter (± 724) 59981 ns/iter (± 719) 1.00
es/visitor/base-perf/fold_noop_impl_all 59871 ns/iter (± 880) 59977 ns/iter (± 422) 1.00
es/visitor/base-perf/fold_noop_impl_vec 59692 ns/iter (± 648) 59944 ns/iter (± 314) 1.00
es/visitor/base-perf/boxing_boxed_clone 60 ns/iter (± 0) 60 ns/iter (± 0) 1
es/visitor/base-perf/boxing_unboxed_clone 47 ns/iter (± 0) 47 ns/iter (± 0) 1
es/visitor/base-perf/boxing_boxed 105 ns/iter (± 1) 107 ns/iter (± 0) 0.98
es/visitor/base-perf/boxing_unboxed 82 ns/iter (± 0) 80 ns/iter (± 0) 1.02
es/visitor/base-perf/visit_contains_this 2627 ns/iter (± 46) 2582 ns/iter (± 10) 1.02
es/base/parallel/resolver/typescript 2662199161 ns/iter (± 140752733) 2442378562 ns/iter (± 180396665) 1.09
es/base/parallel/hygiene/typescript 3409978863 ns/iter (± 51603757) 3435136332 ns/iter (± 53731001) 0.99
babelify-only 497291 ns/iter (± 5436) 490100 ns/iter (± 8892) 1.01
parse_and_babelify_angular 39709218 ns/iter (± 2472568) 39977301 ns/iter (± 2093467) 0.99
parse_and_babelify_backbone 4365349 ns/iter (± 92937) 4354402 ns/iter (± 140298) 1.00
parse_and_babelify_jquery 28766150 ns/iter (± 309415) 28239574 ns/iter (± 285335) 1.02
parse_and_babelify_jquery_mobile 51352103 ns/iter (± 2803304) 51146058 ns/iter (± 834379) 1.00
parse_and_babelify_mootools 26121763 ns/iter (± 229065) 25631561 ns/iter (± 255560) 1.02
parse_and_babelify_underscore 3669482 ns/iter (± 20035) 3667919 ns/iter (± 16147) 1.00
parse_and_babelify_yui 24298944 ns/iter (± 248841) 23449832 ns/iter (± 223276) 1.04
html/minify/document/css_spec 47251719 ns/iter (± 595231) 45289935 ns/iter (± 156115) 1.04
html/minify/document/github 18393028 ns/iter (± 329081) 18274309 ns/iter (± 23456) 1.01
html/minify/document/stackoverflow 16662719 ns/iter (± 758246) 16580111 ns/iter (± 230515) 1.00
html/minify/document_fragment/css_spec 44313062 ns/iter (± 199996) 43873100 ns/iter (± 63900) 1.01
html/minify/document_fragment/github 17962012 ns/iter (± 45508) 17911039 ns/iter (± 22782) 1.00
html/minify/document_fragment/stackoverflow 16331340 ns/iter (± 165477) 16265635 ns/iter (± 14847) 1.00
html/document/visitor/compare/clone 167204 ns/iter (± 1941) 166954 ns/iter (± 1561) 1.00
html/document/visitor/compare/visit_mut_span 183179 ns/iter (± 2963) 182278 ns/iter (± 2945) 1.00
html/document/visitor/compare/visit_mut_span_panic 186801 ns/iter (± 442) 190867 ns/iter (± 7363) 0.98
html/document/visitor/compare/fold_span 231660 ns/iter (± 1082) 227234 ns/iter (± 616) 1.02
html/document/visitor/compare/fold_span_panic 276823 ns/iter (± 753) 281008 ns/iter (± 939) 0.99
html/document_fragment/visitor/compare/clone 166545 ns/iter (± 942) 167005 ns/iter (± 948) 1.00
html/document_fragment/visitor/compare/visit_mut_span 183070 ns/iter (± 1092) 182419 ns/iter (± 1323) 1.00
html/document_fragment/visitor/compare/visit_mut_span_panic 189401 ns/iter (± 1790) 187169 ns/iter (± 1082) 1.01
html/document_fragment/visitor/compare/fold_span 230544 ns/iter (± 1214) 228960 ns/iter (± 1618) 1.01
html/document_fragment/visitor/compare/fold_span_panic 283048 ns/iter (± 1329) 277331 ns/iter (± 572) 1.02
html/lexer/css_2021_spec 12775580 ns/iter (± 85025) 12895190 ns/iter (± 214953) 0.99
html/lexer/github_com_17_05_2022 4909491 ns/iter (± 28744) 4921691 ns/iter (± 12304) 1.00
html/lexer/stackoverflow_com_17_05_2022 4673112 ns/iter (± 42666) 4684767 ns/iter (± 9282) 1.00
html/parser/parser_document/css_2021_spec 25572001 ns/iter (± 275804) 25457245 ns/iter (± 103314) 1.00
html/parser/parser_document/github_com_17_05_2022 8479603 ns/iter (± 24880) 8486059 ns/iter (± 27318) 1.00
html/parser/parser_document/stackoverflow_com_17_05_2022 7467099 ns/iter (± 50552) 7412278 ns/iter (± 16997) 1.01
html/parser/parser_document_fragment/css_2021_spec 25148434 ns/iter (± 55767) 25121061 ns/iter (± 261376) 1.00
html/parser/parser_document_fragment/github_com_17_05_2022 8459539 ns/iter (± 13820) 8434238 ns/iter (± 24915) 1.00
html/parser/parser_document_fragment/stackoverflow_com_17_05_2022 7420591 ns/iter (± 15160) 7321335 ns/iter (± 14924) 1.01

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.