From a28926f3162cb32ee3ea239673374175e444f330 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Tue, 1 Oct 2024 08:02:54 +0000 Subject: [PATCH] fix(transformer): fix inserting `require` with `front` option (#6188) Fix a tiny bug in `add_require`. If `front` option is `true` but there's already an entry for the key, it may not be the last entry in the index map, so `move_index(len, 0)` could swap a *different* `require` to be first. This will not make a difference in practice right now as we don't add enough `requires` at present for it to matter. But it might do once we add imports of helpers etc. --- .../src/common/module_imports.rs | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/crates/oxc_transformer/src/common/module_imports.rs b/crates/oxc_transformer/src/common/module_imports.rs index 6ca5bd961..8c1445c8e 100644 --- a/crates/oxc_transformer/src/common/module_imports.rs +++ b/crates/oxc_transformer/src/common/module_imports.rs @@ -26,7 +26,7 @@ use std::cell::RefCell; -use indexmap::IndexMap; +use indexmap::{map::Entry as IndexMapEntry, IndexMap}; use oxc_ast::{ast::*, NONE}; use oxc_semantic::ReferenceFlags; @@ -196,14 +196,21 @@ impl<'a> ModuleImportsStore<'a> { /// TODO(improve-on-babel): `front` option is only required to pass one of Babel's tests. Output /// without it is still valid. Remove this once our output doesn't need to match Babel exactly. pub fn add_require(&self, source: Atom<'a>, import: NamedImport<'a>, front: bool) { - let len = self.imports.borrow().len(); - self.imports - .borrow_mut() - .entry(ImportType::new(ImportKind::Require, source)) - .or_default() - .push(import); - if front { - self.imports.borrow_mut().move_index(len, 0); + match self.imports.borrow_mut().entry(ImportType::new(ImportKind::Require, source)) { + IndexMapEntry::Occupied(mut entry) => { + entry.get_mut().push(import); + if front && entry.index() != 0 { + entry.move_index(0); + } + } + IndexMapEntry::Vacant(entry) => { + let named_imports = vec![import]; + if front { + entry.shift_insert(0, named_imports); + } else { + entry.insert(named_imports); + } + } } }