Skip to content

Commit 01e12e1

Browse files
simplification refactor
1 parent d4ade49 commit 01e12e1

1 file changed

Lines changed: 33 additions & 26 deletions

File tree

src/utils/sourcemaps.rs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ impl SourceMapProcessor {
300300

301301
let mut explicitly_associated_sourcemaps = HashMap::new();
302302

303-
let unassociated_js_source_locations: HashMap<_, _> = self
303+
let (sources_with_location, sources_without_location) = self
304304
.sources
305305
.values_mut()
306306
.filter(|source| source.ty == SourceFileType::MinifiedSource)
@@ -317,40 +317,47 @@ impl SourceMapProcessor {
317317
})
318318
.ok()
319319
})
320-
.collect();
320+
.fold(
321+
(HashMap::new(), HashSet::new()),
322+
|(mut sources_with_location, mut sources_without_location), (source, location)| {
323+
match location {
324+
Some(location) => {
325+
sources_with_location.insert(source, location);
326+
}
327+
None => {
328+
sources_without_location.insert(source);
329+
}
330+
}
331+
(sources_with_location, sources_without_location)
332+
},
333+
);
321334

322335
// First pass: if location discovered, add to sourcemap_references
323-
unassociated_js_source_locations
324-
.iter()
325-
.filter_map(|(source, location)| location.as_ref().map(|location| (source, location)))
326-
.for_each(|(source, location)| {
327-
// Add location to already associated sourcemaps, so we cannot guess it again.
328-
explicitly_associated_sourcemaps.insert(
329-
fs::path_as_url(
330-
&source
331-
.path
332-
.parent()
333-
.expect("source path has a parent")
334-
.join(location),
335-
),
336-
source.url.clone(),
337-
);
338-
339-
self.sourcemap_references.insert(
340-
source.url.clone(),
341-
Some(SourceMapReference::from_url(location.to_owned())),
342-
);
343-
});
336+
sources_with_location.iter().for_each(|(source, location)| {
337+
let full_sourcemap_path = source
338+
.path
339+
.parent()
340+
.expect("source path has a parent")
341+
.join(location);
342+
343+
// Add location to already associated sourcemaps, so we cannot guess it again.
344+
explicitly_associated_sourcemaps
345+
.insert(fs::path_as_url(&full_sourcemap_path), source.url.clone());
346+
347+
self.sourcemap_references.insert(
348+
source.url.clone(),
349+
Some(SourceMapReference::from_url(location.to_owned())),
350+
);
351+
});
344352

345353
// Second pass: for remaining sourcemaps, try to guess the location
346-
unassociated_js_source_locations
354+
sources_without_location
347355
.into_iter()
348-
.filter(|(_, location)| location.is_none())
349356
.fold(
350357
// Collect sources guessed as associated with each sourcemap. This way, we ensure
351358
// we only associate the sourcemap with any sources if it is only guessed once.
352359
HashMap::new(),
353-
|mut sources_associated_with_sm, (source, _)| {
360+
|mut sources_associated_with_sm, source| {
354361
let sourcemap_reference = guess_sourcemap_reference(&sourcemaps, &source.url)
355362
.inspect_err(|err| {
356363
source.warn(format!(

0 commit comments

Comments
 (0)