Skip to content

Commit

Permalink
fix(es/transforms/base): Reimplement hygiene (#2408)
Browse files Browse the repository at this point in the history
swc_ecma_transforms_base:
 - Reimplement `hygiene`.
  • Loading branch information
kdy1 authored Oct 21, 2021
1 parent ecf0d75 commit 26944e1
Show file tree
Hide file tree
Showing 2,243 changed files with 22,510 additions and 21,539 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions bundler/src/bundler/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use swc_ecma_ast::{
ModuleDecl, Str,
};
use swc_ecma_transforms_base::resolver::resolver_with_mark;
use swc_ecma_visit::{noop_visit_type, FoldWith, Node, Visit, VisitWith};
use swc_ecma_visit::{
noop_visit_mut_type, noop_visit_type, FoldWith, Node, Visit, VisitMut, VisitMutWith, VisitWith,
};
/// Module after applying transformations.
#[derive(Debug, Clone)]
pub(crate) struct TransformedModule {
Expand Down Expand Up @@ -126,12 +128,14 @@ where
fn analyze(
&self,
file_name: &FileName,
data: ModuleData,
mut data: ModuleData,
) -> Result<(TransformedModule, Vec<(Source, Lrc<FileName>)>), Error> {
self.run(|| {
tracing::trace!("transform_module({})", data.fm.name);
let (id, local_mark, export_mark) = self.scope.module_id_gen.gen(file_name);

data.module.visit_mut_with(&mut ClearMark);

let mut module = data.module.fold_with(&mut resolver_with_mark(local_mark));

// {
Expand Down Expand Up @@ -475,3 +479,13 @@ impl Visit for Es6ModuleDetector {
}
}
}

#[derive(Clone, Copy)]
struct ClearMark;
impl VisitMut for ClearMark {
noop_visit_mut_type!();

fn visit_mut_ident(&mut self, ident: &mut Ident) {
ident.span.ctxt = SyntaxContext::empty();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const a1 = 'a';
const c1 = 'c';
export { a1 as a };
export { c1 as c };
const a = 'a';
const c = 'c';
export { a as a };
export { c as c };
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const a1 = 'a';
const c1 = 'c';
export { a1 as a };
export { c1 as c };
const a = 'a';
const c = 'c';
export { a as a };
export { c as c };
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const dbPool1 = 1;
const dbPool = 1;
function d() {
}
async function fn() {
Expand All @@ -12,4 +12,4 @@ function router() {
fn1();
}
router();
export { dbPool1 as dbPool };
export { dbPool as dbPool };
4 changes: 2 additions & 2 deletions bundler/tests/fixture/deno-10820/case1/output/entry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const dbPool1 = 1;
const dbPool = 1;
function d() {
}
async function fn() {
Expand All @@ -12,4 +12,4 @@ function router() {
fn1();
}
router();
export { dbPool1 as dbPool };
export { dbPool as dbPool };
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
async function foo1() {
async function foo() {
}
const mod = {
foo: foo1
foo: foo
};
export { mod as foo };
4 changes: 2 additions & 2 deletions bundler/tests/fixture/deno-8148/test-2/output/entry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
async function foo1() {
async function foo() {
}
const mod = {
foo: foo1
foo: foo
};
export { mod as foo };
6 changes: 3 additions & 3 deletions bundler/tests/fixture/deno-8211-1/output/entry.inlined.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Zone1 {
class Zone {
}
class FixedOffsetZone1 extends Zone1 {
class FixedOffsetZone extends Zone {
}
export { Zone1 as Zone, FixedOffsetZone1 as FixedOffsetZone };
export { Zone as Zone, FixedOffsetZone as FixedOffsetZone };
6 changes: 3 additions & 3 deletions bundler/tests/fixture/deno-8211-1/output/entry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Zone1 {
class Zone {
}
class FixedOffsetZone1 extends Zone1 {
class FixedOffsetZone extends Zone {
}
export { Zone1 as Zone, FixedOffsetZone1 as FixedOffsetZone };
export { Zone as Zone, FixedOffsetZone as FixedOffsetZone };
10 changes: 5 additions & 5 deletions bundler/tests/fixture/deno-8211-2/output/entry.inlined.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class Zone1 {
class Zone {
}
class FixedOffsetZone1 extends Zone1 {
class FixedOffsetZone extends Zone {
}
class Info1 {
class Info {
use() {
console.log(FixedOffsetZone1);
console.log(FixedOffsetZone);
}
}
export { Zone1 as Zone, Info1 as Info, FixedOffsetZone1 as FixedOffsetZone };
export { Zone as Zone, Info as Info, FixedOffsetZone as FixedOffsetZone };
10 changes: 5 additions & 5 deletions bundler/tests/fixture/deno-8211-2/output/entry.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class Zone1 {
class Zone {
}
class FixedOffsetZone1 extends Zone1 {
class FixedOffsetZone extends Zone {
}
class Info1 {
class Info {
use() {
console.log(FixedOffsetZone1);
console.log(FixedOffsetZone);
}
}
export { Zone1 as Zone, Info1 as Info, FixedOffsetZone1 as FixedOffsetZone };
export { Zone as Zone, Info as Info, FixedOffsetZone as FixedOffsetZone };
Loading

1 comment on commit 26944e1

@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: 26944e1 Previous: 7af0f8c Ratio
base_tr_fixer 25859 ns/iter (± 2513) 23426 ns/iter (± 357) 1.10
base_tr_resolver_and_hygiene 149394 ns/iter (± 32398) 131292 ns/iter (± 12693) 1.14
codegen_es2015 61184 ns/iter (± 3994) 50999 ns/iter (± 299) 1.20
codegen_es2016 57340 ns/iter (± 6362) 50961 ns/iter (± 284) 1.13
codegen_es2017 60395 ns/iter (± 5167) 51433 ns/iter (± 328) 1.17
codegen_es2018 57154 ns/iter (± 7867) 51630 ns/iter (± 351) 1.11
codegen_es2019 56999 ns/iter (± 5173) 51492 ns/iter (± 390) 1.11
codegen_es2020 56964 ns/iter (± 6608) 51178 ns/iter (± 345) 1.11
codegen_es3 60133 ns/iter (± 7199) 51233 ns/iter (± 337) 1.17
codegen_es5 60192 ns/iter (± 4893) 51567 ns/iter (± 5450) 1.17
config_for_file 15395 ns/iter (± 2600) 12744 ns/iter (± 3556) 1.21
full_es2015 203476282 ns/iter (± 16000093) 176518571 ns/iter (± 4348098) 1.15
full_es2016 156647896 ns/iter (± 13843852) 138169941 ns/iter (± 6104093) 1.13
full_es2017 160797049 ns/iter (± 11520525) 144114626 ns/iter (± 11722115) 1.12
full_es2018 162165918 ns/iter (± 10721144) 145084782 ns/iter (± 9187140) 1.12
full_es2019 161725101 ns/iter (± 13795182) 141053041 ns/iter (± 7301256) 1.15
full_es2020 157532027 ns/iter (± 10061477) 140924940 ns/iter (± 7168548) 1.12
full_es3 232760988 ns/iter (± 19470617) 210544898 ns/iter (± 18312791) 1.11
full_es5 224023510 ns/iter (± 20816361) 196217816 ns/iter (± 11816952) 1.14
parser 744251 ns/iter (± 30614) 645481 ns/iter (± 22193) 1.15
transforms_es2015 1121388 ns/iter (± 179138) 965447 ns/iter (± 24223) 1.16
transforms_es2016 623879 ns/iter (± 67375) 541121 ns/iter (± 22416) 1.15
transforms_es2017 629106 ns/iter (± 86821) 532854 ns/iter (± 23300) 1.18
transforms_es2018 628077 ns/iter (± 73082) 519356 ns/iter (± 14866) 1.21
transforms_es2019 594346 ns/iter (± 48234) 504353 ns/iter (± 37146) 1.18
transforms_es2020 602560 ns/iter (± 44501) 499593 ns/iter (± 28858) 1.21
transforms_es3 1329609 ns/iter (± 178782) 1104588 ns/iter (± 18391) 1.20
transforms_es5 1058554 ns/iter (± 129609) 951836 ns/iter (± 36604) 1.11
ser_ast_node 172 ns/iter (± 8) 146 ns/iter (± 3) 1.18
ser_serde 181 ns/iter (± 16) 158 ns/iter (± 3) 1.15
emit_colors 15870664 ns/iter (± 19860826) 14728563 ns/iter (± 19236504) 1.08
emit_large 107009185 ns/iter (± 166247544) 94551280 ns/iter (± 144868217) 1.13
base_clone 2566507 ns/iter (± 233879) 2336720 ns/iter (± 17392) 1.10
fold_span 4265676 ns/iter (± 370627) 3879717 ns/iter (± 102913) 1.10
fold_span_panic 4577032 ns/iter (± 336902) 4125491 ns/iter (± 73075) 1.11
visit_mut_span 3133641 ns/iter (± 182770) 2853059 ns/iter (± 27704) 1.10
visit_mut_span_panic 3176543 ns/iter (± 212236) 2921650 ns/iter (± 80743) 1.09
boxing_boxed 172 ns/iter (± 15) 143 ns/iter (± 0) 1.20
boxing_boxed_clone 81 ns/iter (± 5) 68 ns/iter (± 0) 1.19
boxing_unboxed 144 ns/iter (± 9) 142 ns/iter (± 0) 1.01
boxing_unboxed_clone 74 ns/iter (± 10) 63 ns/iter (± 0) 1.17

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

Please sign in to comment.