Skip to content

Commit

Permalink
fix(transformer): fix inserting require with front option (#6188)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
overlookmotel committed Oct 1, 2024
1 parent e9eeae0 commit a28926f
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions crates/oxc_transformer/src/common/module_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
}

Expand Down

0 comments on commit a28926f

Please sign in to comment.