mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
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.
This commit is contained in:
parent
e9eeae0f4a
commit
a28926f316
1 changed files with 16 additions and 9 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue