Skip to content

Commit

Permalink
fix(es/typescript): Rename wrong unresolved_mark (#8018)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes: #8016
  • Loading branch information
magic-akari authored Sep 27, 2023
1 parent 8231f87 commit 5817268
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 36 deletions.
38 changes: 18 additions & 20 deletions crates/swc_ecma_transforms_typescript/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use crate::{
/// [export and import require]: https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require
#[derive(Debug, Default)]
pub(crate) struct Transform {
unresolved_mark: Mark,
top_level_mark: Mark,
top_level_ctxt: SyntaxContext,

import_export_assign_config: TsImportExportAssignConfig,
Expand All @@ -64,13 +64,13 @@ pub(crate) struct Transform {
}

pub fn transform(
unresolved_mark: Mark,
top_level_mark: Mark,
import_export_assign_config: TsImportExportAssignConfig,
verbatim_module_syntax: bool,
) -> impl Fold + VisitMut {
as_folder(Transform {
unresolved_mark,
top_level_ctxt: SyntaxContext::empty().apply_mark(unresolved_mark),
top_level_mark,
top_level_ctxt: SyntaxContext::empty().apply_mark(top_level_mark),
import_export_assign_config,
verbatim_module_syntax,
..Default::default()
Expand Down Expand Up @@ -502,7 +502,7 @@ impl Transform {
&id.to_id(),
&default_init,
&self.record,
self.unresolved_mark,
self.top_level_mark,
);

default_init = value.inc();
Expand Down Expand Up @@ -560,14 +560,14 @@ impl Transform {
enum_id: &Id,
default_init: &TsEnumRecordValue,
record: &TsEnumRecord,
unresolved_mark: Mark,
top_level_mark: Mark,
) -> TsEnumRecordValue {
member
.init
.map(|expr| {
EnumValueComputer {
enum_id,
unresolved_mark,
top_level_mark,
record,
}
.compute(expr)
Expand Down Expand Up @@ -934,9 +934,13 @@ impl Transform {
let mut should_inject = false;
let create_require = private_ident!("_createRequire");
let require = private_ident!("__require");
let global_span = DUMMY_SP.apply_mark(self.unresolved_mark);
let cjs_require = quote_ident!(global_span, "require");
let cjs_exports = quote_ident!(global_span, "exports");

// NOTE: This is not correct!
// However, all unresolved_span are used in TsImportExportAssignConfig::Classic
// which is deprecated and not used in real world.
let unresolved_span = DUMMY_SP.apply_mark(self.top_level_mark);
let cjs_require = quote_ident!(unresolved_span, "require");
let cjs_exports = quote_ident!(unresolved_span, "exports");

let mut cjs_export_assign = None;

Expand Down Expand Up @@ -1097,16 +1101,10 @@ impl Transform {
n.push(
Stmt::Expr(ExprStmt {
span,
expr: Box::new(
expr.make_assign_to(
op!("="),
member_expr!(
DUMMY_SP.apply_mark(self.unresolved_mark),
module.exports
)
.as_pat_or_expr(),
),
),
expr: Box::new(expr.make_assign_to(
op!("="),
member_expr!(unresolved_span, module.exports).as_pat_or_expr(),
)),
})
.into(),
);
Expand Down
6 changes: 3 additions & 3 deletions crates/swc_ecma_transforms_typescript/src/ts_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl From<f64> for TsEnumRecordValue {

pub(crate) struct EnumValueComputer<'a> {
pub enum_id: &'a Id,
pub unresolved_mark: Mark,
pub top_level_mark: Mark,
pub record: &'a TsEnumRecord,
}

Expand All @@ -111,12 +111,12 @@ impl<'a> EnumValueComputer<'a> {
span,
sym: js_word!("NaN"),
..
}) if span.ctxt.has_mark(self.unresolved_mark) => TsEnumRecordValue::Number(f64::NAN),
}) if span.ctxt.has_mark(self.top_level_mark) => TsEnumRecordValue::Number(f64::NAN),
Expr::Ident(Ident {
span,
sym: js_word!("Infinity"),
..
}) if span.ctxt.has_mark(self.unresolved_mark) => {
}) if span.ctxt.has_mark(self.top_level_mark) => {
TsEnumRecordValue::Number(f64::INFINITY)
}
Expr::Ident(ref ident) => self
Expand Down
26 changes: 13 additions & 13 deletions crates/swc_ecma_transforms_typescript/src/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ use swc_ecma_visit::{as_folder, Fold, VisitMut, VisitMutWith};
pub use crate::config::*;
use crate::{strip_import_export::StripImportExport, strip_type::StripType, transform::transform};

pub fn typescript(config: Config, unresolved_mark: Mark) -> impl Fold + VisitMut {
pub fn typescript(config: Config, top_level_mark: Mark) -> impl Fold + VisitMut {
as_folder(TypeScript {
config,
unresolved_mark,
top_level_mark,
id_usage: Default::default(),
})
}

pub fn strip(unresolved_mark: Mark) -> impl Fold + VisitMut {
typescript(Config::default(), unresolved_mark)
pub fn strip(top_level_mark: Mark) -> impl Fold + VisitMut {
typescript(Config::default(), top_level_mark)
}

pub(crate) struct TypeScript {
pub config: Config,
pub unresolved_mark: Mark,
pub top_level_mark: Mark,

id_usage: AHashSet<Id>,
}
Expand All @@ -45,7 +45,7 @@ impl VisitMut for TypeScript {
n.visit_mut_with(&mut StripType::default());

n.visit_mut_with(&mut transform(
self.unresolved_mark,
self.top_level_mark,
self.config.import_export_assign_config,
self.config.verbatim_module_syntax,
));
Expand Down Expand Up @@ -104,7 +104,7 @@ pub fn tsx<C>(
config: Config,
tsx_config: TsxConfig,
comments: C,
unresolved_mark: Mark,
top_level_mark: Mark,
) -> impl Fold + VisitMut
where
C: Comments,
Expand All @@ -115,7 +115,7 @@ where
id_usage: Default::default(),
comments,
cm,
unresolved_mark,
top_level_mark,
})
}

Expand All @@ -142,7 +142,7 @@ where
id_usage: AHashSet<Id>,
comments: C,
cm: Lrc<SourceMap>,
unresolved_mark: Mark,
top_level_mark: Mark,
}

impl<C> VisitMut for TypeScriptReact<C>
Expand All @@ -162,7 +162,7 @@ where
.pragma
.clone()
.unwrap_or_else(|| "React.createElement".to_string()),
self.unresolved_mark,
self.top_level_mark,
);

let pragma_frag = parse_expr_for_jsx(
Expand All @@ -172,7 +172,7 @@ where
.pragma_frag
.clone()
.unwrap_or_else(|| "React.Fragment".to_string()),
self.unresolved_mark,
self.top_level_mark,
);

let pragma_id = id_for_jsx(&pragma);
Expand All @@ -190,7 +190,7 @@ where
pragma_frag,
..
} = self.comments.with_leading(span.lo, |comments| {
JsxDirectives::from_comments(&self.cm, span, comments, self.unresolved_mark)
JsxDirectives::from_comments(&self.cm, span, comments, self.top_level_mark)
});

if let Some(pragma) = pragma {
Expand All @@ -214,7 +214,7 @@ where

n.visit_mut_with(&mut TypeScript {
config: mem::take(&mut self.config),
unresolved_mark: self.unresolved_mark,
top_level_mark: self.top_level_mark,
id_usage: mem::take(&mut self.id_usage),
});
}
Expand Down

1 comment on commit 5817268

@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: 5817268 Previous: 2f01aba Ratio
es/full/bugs-1 284593 ns/iter (± 4996) 289650 ns/iter (± 12923) 0.98
es/full/minify/libraries/antd 1338196435 ns/iter (± 15375926) 1318521810 ns/iter (± 11772819) 1.01
es/full/minify/libraries/d3 273285161 ns/iter (± 3932503) 277048233 ns/iter (± 2568365) 0.99
es/full/minify/libraries/echarts 1061781553 ns/iter (± 6140023) 1059470217 ns/iter (± 6933353) 1.00
es/full/minify/libraries/jquery 83670064 ns/iter (± 348666) 84488195 ns/iter (± 446466) 0.99
es/full/minify/libraries/lodash 96999967 ns/iter (± 286219) 97431602 ns/iter (± 229058) 1.00
es/full/minify/libraries/moment 49428648 ns/iter (± 103449) 49949626 ns/iter (± 95031) 0.99
es/full/minify/libraries/react 17862869 ns/iter (± 65568) 18057161 ns/iter (± 48928) 0.99
es/full/minify/libraries/terser 216595625 ns/iter (± 897632) 218249351 ns/iter (± 768750) 0.99
es/full/minify/libraries/three 388864484 ns/iter (± 2240336) 386881625 ns/iter (± 2662566) 1.01
es/full/minify/libraries/typescript 2714244243 ns/iter (± 31156729) 2599310602 ns/iter (± 13363509) 1.04
es/full/minify/libraries/victory 578801457 ns/iter (± 5121085) 556328667 ns/iter (± 5115047) 1.04
es/full/minify/libraries/vue 119346698 ns/iter (± 372556) 119371277 ns/iter (± 383121) 1.00
es/full/codegen/es3 35560 ns/iter (± 597) 34488 ns/iter (± 119) 1.03
es/full/codegen/es5 35802 ns/iter (± 422) 34762 ns/iter (± 145) 1.03
es/full/codegen/es2015 35728 ns/iter (± 113) 34702 ns/iter (± 143) 1.03
es/full/codegen/es2016 35693 ns/iter (± 137) 34548 ns/iter (± 137) 1.03
es/full/codegen/es2017 35694 ns/iter (± 203) 34716 ns/iter (± 232) 1.03
es/full/codegen/es2018 35705 ns/iter (± 115) 34639 ns/iter (± 145) 1.03
es/full/codegen/es2019 35722 ns/iter (± 205) 34583 ns/iter (± 145) 1.03
es/full/codegen/es2020 35682 ns/iter (± 127) 34605 ns/iter (± 106) 1.03
es/full/all/es3 165089211 ns/iter (± 1474649) 164274922 ns/iter (± 1036964) 1.00
es/full/all/es5 157001871 ns/iter (± 900164) 158376299 ns/iter (± 1382441) 0.99
es/full/all/es2015 116301140 ns/iter (± 582343) 116430147 ns/iter (± 594054) 1.00
es/full/all/es2016 115777511 ns/iter (± 475661) 115619287 ns/iter (± 890435) 1.00
es/full/all/es2017 115136476 ns/iter (± 608547) 115556294 ns/iter (± 875264) 1.00
es/full/all/es2018 113503622 ns/iter (± 685860) 113089083 ns/iter (± 508588) 1.00
es/full/all/es2019 111837033 ns/iter (± 922310) 112513220 ns/iter (± 347625) 0.99
es/full/all/es2020 108476122 ns/iter (± 528342) 108517256 ns/iter (± 2045340) 1.00
es/full/parser 487206 ns/iter (± 5325) 494166 ns/iter (± 6429) 0.99
es/full/base/fixer 17668 ns/iter (± 65) 18487 ns/iter (± 131) 0.96
es/full/base/resolver_and_hygiene 81056 ns/iter (± 197) 81429 ns/iter (± 843) 1.00
serialization of serde 299 ns/iter (± 6) 297 ns/iter (± 4) 1.01
css/minify/libraries/bootstrap 28626959 ns/iter (± 54720) 28595787 ns/iter (± 229472) 1.00
css/visitor/compare/clone 1676891 ns/iter (± 6166) 1650418 ns/iter (± 11572) 1.02
css/visitor/compare/visit_mut_span 1808256 ns/iter (± 12351) 1751935 ns/iter (± 10007) 1.03
css/visitor/compare/visit_mut_span_panic 1891666 ns/iter (± 8518) 1850548 ns/iter (± 11928) 1.02
css/visitor/compare/fold_span 2593686 ns/iter (± 11559) 2532507 ns/iter (± 22848) 1.02
css/visitor/compare/fold_span_panic 2765662 ns/iter (± 10810) 2766829 ns/iter (± 26902) 1.00
css/lexer/bootstrap_5_1_3 4436016 ns/iter (± 3393) 4395740 ns/iter (± 34431) 1.01
css/lexer/foundation_6_7_4 3722066 ns/iter (± 4353) 3693441 ns/iter (± 31056) 1.01
css/lexer/tailwind_3_1_1 707369 ns/iter (± 1260) 701641 ns/iter (± 3783) 1.01
css/parser/bootstrap_5_1_3 20021603 ns/iter (± 76482) 19000559 ns/iter (± 116004) 1.05
css/parser/foundation_6_7_4 15956825 ns/iter (± 24219) 15207148 ns/iter (± 133650) 1.05
css/parser/tailwind_3_1_1 3047750 ns/iter (± 2176) 2944303 ns/iter (± 13316) 1.04
es/codegen/colors 745254 ns/iter (± 405718) 738262 ns/iter (± 401637) 1.01
es/codegen/large 2885270 ns/iter (± 1519264) 2889338 ns/iter (± 1528039) 1.00
es/codegen/with-parser/colors 46270 ns/iter (± 315) 45302 ns/iter (± 534) 1.02
es/codegen/with-parser/large 493887 ns/iter (± 1350) 491966 ns/iter (± 2636) 1.00
es/minify/libraries/antd 1194928517 ns/iter (± 9130868) 1139945903 ns/iter (± 12863935) 1.05
es/minify/libraries/d3 239955393 ns/iter (± 732659) 239211206 ns/iter (± 684945) 1.00
es/minify/libraries/echarts 931644924 ns/iter (± 8100766) 907577178 ns/iter (± 8750979) 1.03
es/minify/libraries/jquery 73311822 ns/iter (± 98934) 72948516 ns/iter (± 150513) 1.00
es/minify/libraries/lodash 86884660 ns/iter (± 269888) 86755822 ns/iter (± 150345) 1.00
es/minify/libraries/moment 43579872 ns/iter (± 78319) 43296453 ns/iter (± 76669) 1.01
es/minify/libraries/react 16047549 ns/iter (± 39462) 15931433 ns/iter (± 51221) 1.01
es/minify/libraries/terser 187350148 ns/iter (± 673700) 185341320 ns/iter (± 447762) 1.01
es/minify/libraries/three 324443452 ns/iter (± 1333672) 322886385 ns/iter (± 6531198) 1.00
es/minify/libraries/typescript 2332854828 ns/iter (± 15934029) 2253431295 ns/iter (± 7466823) 1.04
es/minify/libraries/victory 477798223 ns/iter (± 12903764) 473587192 ns/iter (± 8754992) 1.01
es/minify/libraries/vue 106322230 ns/iter (± 538141) 106078479 ns/iter (± 266428) 1.00
es/visitor/compare/clone 1946679 ns/iter (± 7511) 1948461 ns/iter (± 5253) 1.00
es/visitor/compare/visit_mut_span 2280199 ns/iter (± 5837) 2285280 ns/iter (± 4790) 1.00
es/visitor/compare/visit_mut_span_panic 2325427 ns/iter (± 4278) 2327924 ns/iter (± 11633) 1.00
es/visitor/compare/fold_span 3404458 ns/iter (± 11481) 3351705 ns/iter (± 13478) 1.02
es/visitor/compare/fold_span_panic 3521894 ns/iter (± 8581) 3532970 ns/iter (± 6674) 1.00
es/lexer/colors 13416 ns/iter (± 17) 13022 ns/iter (± 67) 1.03
es/lexer/angular 6113881 ns/iter (± 3835) 6057985 ns/iter (± 32564) 1.01
es/lexer/backbone 788810 ns/iter (± 1441) 775851 ns/iter (± 9075) 1.02
es/lexer/jquery 4462639 ns/iter (± 17302) 4472820 ns/iter (± 12456) 1.00
es/lexer/jquery mobile 6870837 ns/iter (± 4721) 6704024 ns/iter (± 72810) 1.02
es/lexer/mootools 3550969 ns/iter (± 3559) 3508012 ns/iter (± 21022) 1.01
es/lexer/underscore 658075 ns/iter (± 232) 658221 ns/iter (± 4165) 1.00
es/lexer/three 21325653 ns/iter (± 10253) 21145913 ns/iter (± 81998) 1.01
es/lexer/yui 3810915 ns/iter (± 4849) 3806050 ns/iter (± 14136) 1.00
es/parser/colors 27407 ns/iter (± 70) 27221 ns/iter (± 42) 1.01
es/parser/angular 13715464 ns/iter (± 59055) 13452375 ns/iter (± 130084) 1.02
es/parser/backbone 2007394 ns/iter (± 6065) 1956639 ns/iter (± 9591) 1.03
es/parser/jquery 10980688 ns/iter (± 33890) 10725163 ns/iter (± 71423) 1.02
es/parser/jquery mobile 16834117 ns/iter (± 82369) 16308758 ns/iter (± 145672) 1.03
es/parser/mootools 8418806 ns/iter (± 22672) 8200816 ns/iter (± 101071) 1.03
es/parser/underscore 1728808 ns/iter (± 11205) 1670062 ns/iter (± 18640) 1.04
es/parser/three 47295480 ns/iter (± 148701) 46210742 ns/iter (± 761338) 1.02
es/parser/yui 8346771 ns/iter (± 24164) 8266161 ns/iter (± 102601) 1.01
es/preset-env/usage/builtin_type 136566 ns/iter (± 31621) 138109 ns/iter (± 32747) 0.99
es/preset-env/usage/property 16656 ns/iter (± 56) 16396 ns/iter (± 67) 1.02
es/resolver/typescript 92252677 ns/iter (± 1498571) 91397801 ns/iter (± 1453116) 1.01
es/fixer/typescript 64672963 ns/iter (± 614213) 62514198 ns/iter (± 1070787) 1.03
es/hygiene/typescript 131854904 ns/iter (± 598336) 129511869 ns/iter (± 552996) 1.02
es/resolver_with_hygiene/typescript 248059133 ns/iter (± 1590101) 244155314 ns/iter (± 949650) 1.02
es/visitor/base-perf/module_clone 59087 ns/iter (± 584) 59188 ns/iter (± 151) 1.00
es/visitor/base-perf/fold_empty 62587 ns/iter (± 158) 63084 ns/iter (± 305) 0.99
es/visitor/base-perf/fold_noop_impl_all 63080 ns/iter (± 573) 63140 ns/iter (± 381) 1.00
es/visitor/base-perf/fold_noop_impl_vec 63130 ns/iter (± 205) 63291 ns/iter (± 402) 1.00
es/visitor/base-perf/boxing_boxed_clone 56 ns/iter (± 0) 56 ns/iter (± 0) 1
es/visitor/base-perf/boxing_unboxed_clone 39 ns/iter (± 0) 39 ns/iter (± 0) 1
es/visitor/base-perf/boxing_boxed 108 ns/iter (± 0) 108 ns/iter (± 0) 1
es/visitor/base-perf/boxing_unboxed 77 ns/iter (± 0) 77 ns/iter (± 0) 1
es/visitor/base-perf/visit_empty 0 ns/iter (± 0) 0 ns/iter (± 0) NaN
es/visitor/base-perf/visit_contains_this 2522 ns/iter (± 12) 2515 ns/iter (± 24) 1.00
es/base/parallel/resolver/typescript 4754632007 ns/iter (± 249426718) 4338239439 ns/iter (± 215466078) 1.10
es/base/parallel/hygiene/typescript 1443650950 ns/iter (± 20362924) 1433205572 ns/iter (± 14363859) 1.01
misc/visitors/time-complexity/time 5 124 ns/iter (± 1) 140 ns/iter (± 1) 0.89
misc/visitors/time-complexity/time 10 399 ns/iter (± 5) 305 ns/iter (± 0) 1.31
misc/visitors/time-complexity/time 15 680 ns/iter (± 20) 599 ns/iter (± 3) 1.14
misc/visitors/time-complexity/time 20 1056 ns/iter (± 6) 1057 ns/iter (± 7) 1.00
misc/visitors/time-complexity/time 40 3657 ns/iter (± 42) 4131 ns/iter (± 23) 0.89
misc/visitors/time-complexity/time 60 7559 ns/iter (± 423) 7739 ns/iter (± 81) 0.98

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

Please sign in to comment.