From f250f243cba49b9cdcdc920d37c92662c14941bc Mon Sep 17 00:00:00 2001 From: Austaras Date: Mon, 20 Mar 2023 13:28:08 +0800 Subject: [PATCH] refactor(es/fixer): Move comments with the insertion order (#7097) --- Cargo.lock | 1 + crates/swc_ecma_transforms_base/Cargo.toml | 1 + crates/swc_ecma_transforms_base/src/fixer.rs | 11 +++++++---- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9dd361193a50..b72851c8072e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3844,6 +3844,7 @@ dependencies = [ "better_scoped_tls", "bitflags", "criterion", + "indexmap", "once_cell", "phf", "rayon", diff --git a/crates/swc_ecma_transforms_base/Cargo.toml b/crates/swc_ecma_transforms_base/Cargo.toml index 34228a9c2af9..6a5d9e73bd20 100644 --- a/crates/swc_ecma_transforms_base/Cargo.toml +++ b/crates/swc_ecma_transforms_base/Cargo.toml @@ -19,6 +19,7 @@ concurrent-renamer = ["rayon"] [dependencies] better_scoped_tls = { version = "0.1.0", path = "../better_scoped_tls" } bitflags = "1" +indexmap = "1.6.1" once_cell = "1.10.0" phf = { version = "0.10", features = ["macros"] } rayon = { version = "1", optional = true } diff --git a/crates/swc_ecma_transforms_base/src/fixer.rs b/crates/swc_ecma_transforms_base/src/fixer.rs index a77725778cd5..39df28df988d 100644 --- a/crates/swc_ecma_transforms_base/src/fixer.rs +++ b/crates/swc_ecma_transforms_base/src/fixer.rs @@ -1,4 +1,7 @@ -use rustc_hash::FxHashMap; +use std::{hash::BuildHasherDefault, ops::RangeFull}; + +use indexmap::IndexMap; +use rustc_hash::FxHasher; use swc_atoms::js_word; use swc_common::{comments::Comments, util::take::Take, Span, Spanned}; use swc_ecma_ast::*; @@ -37,7 +40,7 @@ struct Fixer<'a> { /// /// Key is span of inner expression, and value is span of the paren /// expression. - span_map: FxHashMap, + span_map: IndexMap>, in_for_stmt_head: bool, @@ -613,7 +616,7 @@ impl VisitMut for Fixer<'_> { n.visit_mut_children_with(self); if let Some(c) = self.comments { - for (to, from) in self.span_map.drain() { + for (to, from) in self.span_map.drain(RangeFull).rev() { c.move_leading(from.lo, to.lo); c.move_trailing(from.hi, to.hi); } @@ -673,7 +676,7 @@ impl VisitMut for Fixer<'_> { n.visit_mut_children_with(self); if let Some(c) = self.comments { - for (to, from) in self.span_map.drain() { + for (to, from) in self.span_map.drain(RangeFull).rev() { c.move_leading(from.lo, to.lo); c.move_trailing(from.hi, to.hi); }