Skip to content

Commit 27def14

Browse files
authored
fix: Properly remap source_content to source_index (#87)
1 parent 93c0627 commit 27def14

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

parcel_sourcemap/src/lib.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,15 +238,12 @@ impl SourceMap {
238238

239239
pub fn get_source_index(&self, source: &str) -> Result<Option<u32>, SourceMapError> {
240240
let normalized_source = make_relative_path(self.project_root.as_str(), source);
241-
match self
241+
Ok(self
242242
.inner
243243
.sources
244244
.iter()
245245
.position(|s| normalized_source.eq(s))
246-
{
247-
Some(i) => Ok(Some(i as u32)),
248-
None => Ok(None),
249-
}
246+
.map(|v| v as u32))
250247
}
251248

252249
pub fn get_source(&self, index: u32) -> Result<&str, SourceMapError> {
@@ -529,7 +526,9 @@ impl SourceMap {
529526

530527
self.inner.sources_content.reserve(sources_content.len());
531528
for (i, source_content) in sources_content.iter().enumerate() {
532-
self.set_source_content(i, source_content)?;
529+
if let Some(source_index) = source_indexes.get(i) {
530+
self.set_source_content(*source_index as usize, source_content)?;
531+
}
533532
}
534533

535534
let mut input = input.iter().cloned().peekable();

parcel_sourcemap_node/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ fn get_source_index(ctx: CallContext) -> Result<JsNumber> {
9595

9696
let source = ctx.get::<JsString>(0)?.into_utf8()?;
9797
let source_index = source_map_instance.get_source_index(source.as_str()?)?;
98-
9998
match source_index {
10099
Some(i) => ctx.env.create_uint32(i),
101100
None => ctx.env.create_int32(-1),

parcel_sourcemap_wasm/src/lib.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,11 @@ impl SourceMap {
173173
}
174174

175175
pub fn getSourceIndex(&self, source: &str) -> Result<JsValue, JsValue> {
176-
Ok(JsValue::from(
177-
self.map
178-
.get_source_index(source)?
179-
.map(|v| i32::try_from(v).unwrap())
180-
.unwrap_or(-1),
181-
))
176+
let mapped_val: i32 = match self.map.get_source_index(source)? {
177+
Some(found_source_index) => i32::try_from(found_source_index).unwrap_or(-1),
178+
None => -1,
179+
};
180+
Ok(JsValue::from(mapped_val))
182181
}
183182

184183
pub fn addIndexedMappings(&mut self, mappings_arr: &[i32]) {
@@ -258,7 +257,6 @@ impl SourceMap {
258257

259258
pub fn getSourceContentBySource(&self, source: &str) -> Result<JsValue, JsValue> {
260259
let source_index = self.map.get_source_index(source)?;
261-
262260
match source_index {
263261
Some(i) => {
264262
let source_content = self.map.get_source_content(i)?;

test/basic.test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ describe('SourceMap - Basics', () => {
249249
assert.deepEqual(map.addSources(['test.js', 'execute.js']), [2, 3]);
250250

251251
assert.deepEqual(map.addSource('abc.js'), 4);
252-
assert.deepEqual(map.addSource("dist/rörfokus/4784.js"), 5);
252+
assert.deepEqual(map.addSource('dist/rörfokus/4784.js'), 5);
253253
});
254254

255255
it('Should be able to handle absolute url sources', () => {
@@ -409,4 +409,20 @@ describe('SourceMap - Basics', () => {
409409

410410
assert.equal(map.getSourceContent('helloworld.coffee'), 'module.exports = () => "hello world";');
411411
});
412+
413+
it('Should deduplicate sources and sourcesContent from a VLQ Map', () => {
414+
let map = new SourceMap('/test-root');
415+
map.addVLQMap({
416+
mappings: SIMPLE_SOURCE_MAP.mappings,
417+
sources: ['./index.js', './index.js'],
418+
sourcesContent: ['first', 'second'],
419+
names: ['a'],
420+
});
421+
422+
const stringifiedMap = map.toVLQ();
423+
assert.equal(stringifiedMap.mappings, SIMPLE_SOURCE_MAP.mappings);
424+
assert.deepEqual(stringifiedMap.sources, ['index.js']);
425+
assert.deepEqual(stringifiedMap.sourcesContent, ['second']);
426+
assert.deepEqual(stringifiedMap.names, ['a']);
427+
});
412428
});

0 commit comments

Comments
 (0)