refactor(es/fixer): Move comments with the insertion order (#7097)

This commit is contained in:
Austaras 2023-03-20 13:28:08 +08:00 committed by GitHub
parent 85c51a81e7
commit f250f243cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 4 deletions

1
Cargo.lock generated
View File

@ -3844,6 +3844,7 @@ dependencies = [
"better_scoped_tls",
"bitflags",
"criterion",
"indexmap",
"once_cell",
"phf",
"rayon",

View File

@ -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 }

View File

@ -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, Span>,
span_map: IndexMap<Span, Span, BuildHasherDefault<FxHasher>>,
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);
}