Skip to content

Commit

Permalink
refactor: use stable Rust (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen authored Nov 28, 2023
1 parent 4f08179 commit d5a6af2
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 122 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "nightly-2022-12-15"
channel = "1.74.0"
profile = "default"
1 change: 0 additions & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
max_width = 80
tab_spaces = 2
format_code_in_doc_comments = true
82 changes: 50 additions & 32 deletions src/concat_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,22 +161,27 @@ impl StreamChunks for ConcatSource {
};
if need_to_cloas_mapping {
if mapping.generated_line != 1 || mapping.generated_column != 0 {
on_chunk(None, Mapping {
generated_line: current_line_offset + 1,
generated_column: current_column_offset,
original: None,
});
on_chunk(
None,
Mapping {
generated_line: current_line_offset + 1,
generated_column: current_column_offset,
original: None,
},
);
}
need_to_cloas_mapping = false;
}
let result_source_index = mapping.original.as_ref().and_then(|original| {
source_index_mapping
.borrow()
.get(&original.source_index)
.copied()
});
let result_source_index =
mapping.original.as_ref().and_then(|original| {
source_index_mapping
.borrow()
.get(&original.source_index)
.copied()
});
let result_name_index = mapping
.original.as_ref()
.original
.as_ref()
.and_then(|original| original.name_index)
.and_then(|name_index| {
name_index_mapping.borrow().get(&name_index).copied()
Expand All @@ -187,8 +192,29 @@ impl StreamChunks for ConcatSource {
mapping.generated_line
};
if options.final_source {
if let Some(result_source_index) = result_source_index && let Some(original) = &mapping.original {
on_chunk(None, Mapping {
if let (Some(result_source_index), Some(original)) =
(result_source_index, &mapping.original)
{
on_chunk(
None,
Mapping {
generated_line: line,
generated_column: column,
original: Some(OriginalLocation {
source_index: result_source_index,
original_line: original.original_line,
original_column: original.original_column,
name_index: result_name_index,
}),
},
);
}
} else if let (Some(result_source_index), Some(original)) =
(result_source_index, &mapping.original)
{
on_chunk(
chunk,
Mapping {
generated_line: line,
generated_column: column,
original: Some(OriginalLocation {
Expand All @@ -197,25 +223,17 @@ impl StreamChunks for ConcatSource {
original_column: original.original_column,
name_index: result_name_index,
}),
});
}
} else if let Some(result_source_index) = result_source_index && let Some(original) = &mapping.original {
on_chunk(chunk, Mapping {
generated_line: line,
generated_column: column,
original: Some(OriginalLocation {
source_index: result_source_index,
original_line: original.original_line,
original_column: original.original_column,
name_index: result_name_index,
}),
});
},
);
} else {
on_chunk(chunk, Mapping {
generated_line: line,
generated_column: column,
original: None,
});
on_chunk(
chunk,
Mapping {
generated_line: line,
generated_column: column,
original: None,
},
);
}
},
&mut |i, source, source_content| {
Expand Down
89 changes: 51 additions & 38 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,16 +284,16 @@ fn encode_full_mappings(mappings: &[Mapping]) -> String {
|acc, mapping| {
if active_mapping && current_line == mapping.generated_line {
// A mapping is still active
if let Some(original) = &mapping.original
&& original.source_index == current_source_index
&& original.original_line == current_original_line
&& original.original_column == current_original_column
&& !active_name
&& original.name_index.is_none()
{
// avoid repeating the same original mapping
return acc;
}
if mapping.original.is_some_and(|original| {
original.source_index == current_source_index
&& original.original_line == current_original_line
&& original.original_column == current_original_column
&& !active_name
&& original.name_index.is_none()
}) {
// avoid repeating the same original mapping
return acc;
}
} else {
// No mapping is active
if mapping.original.is_none() {
Expand Down Expand Up @@ -531,7 +531,9 @@ pub fn stream_chunks_of_raw_source(
line += 1;
last_line = Some(l);
}
if let Some(last_line) = last_line && !last_line.ends_with('\n') {
if let Some(last_line) =
last_line.filter(|last_line| !last_line.ends_with('\n'))
{
GeneratedInfo {
generated_line: line,
generated_column: last_line.len() as u32,
Expand Down Expand Up @@ -753,10 +755,11 @@ fn stream_chunks_of_source_map_full(
}
current_generated_column = mapping.generated_column;
}
if let Some(original) = &mapping.original
&& (mapping.generated_line < final_line
if let Some(original) = &mapping.original.filter(|_| {
mapping.generated_line < final_line
|| (mapping.generated_line == final_line
&& mapping.generated_column < final_column)) {
&& mapping.generated_column < final_column)
}) {
mapping_active = true;
active_mapping_original = Some(*original);
}
Expand Down Expand Up @@ -801,19 +804,23 @@ fn stream_chunks_of_source_map_lines_final(
let mut current_generated_line = 1;

let mut on_mapping = |mapping: &Mapping| {
if let Some(original) = &mapping.original
&& current_generated_line <= mapping.generated_line
&& mapping.generated_line <= final_line {
on_chunk(None, Mapping {
generated_line: mapping.generated_line,
generated_column: 0,
original: Some(OriginalLocation {
source_index: original.source_index,
original_line: original.original_line,
original_column: original.original_column,
name_index: None,
}),
});
if let Some(original) = &mapping.original.filter(|_| {
current_generated_line <= mapping.generated_line
&& mapping.generated_line <= final_line
}) {
on_chunk(
None,
Mapping {
generated_line: mapping.generated_line,
generated_column: 0,
original: Some(OriginalLocation {
source_index: original.source_index,
original_line: original.original_line,
original_column: original.original_column,
name_index: None,
}),
},
);
current_generated_line = mapping.generated_line + 1;
}
};
Expand Down Expand Up @@ -861,17 +868,23 @@ fn stream_chunks_of_source_map_lines_full(
}
current_generated_line += 1;
}
if let Some(original) = &mapping.original && mapping.generated_line as usize <= lines.len() {
on_chunk(Some(lines[mapping.generated_line as usize - 1]), Mapping {
generated_line: mapping.generated_line,
generated_column: 0,
original: Some(OriginalLocation {
source_index: original.source_index,
original_line: original.original_line,
original_column: original.original_column,
name_index: None,
}),
});
if let Some(original) = &mapping
.original
.filter(|_| mapping.generated_line as usize <= lines.len())
{
on_chunk(
Some(lines[mapping.generated_line as usize - 1]),
Mapping {
generated_line: mapping.generated_line,
generated_column: 0,
original: Some(OriginalLocation {
source_index: original.source_index,
original_line: original.original_line,
original_column: original.original_column,
name_index: None,
}),
},
);
current_generated_line += 1;
}
};
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Rusty [`webpack-sources`](https://github.com/webpack/webpack-sources) port.
#![feature(let_chains)]
#![warn(unsafe_code)]
#![deny(missing_docs)]

Expand Down
4 changes: 3 additions & 1 deletion src/original_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ impl StreamChunks for OriginalSource {
line += 1;
last_line = Some(l);
}
if let Some(last_line) = last_line && !last_line.ends_with('\n') {
if let Some(last_line) =
last_line.filter(|last_line| !last_line.ends_with('\n'))
{
GeneratedInfo {
generated_line: line,
generated_column: last_line.len() as u32,
Expand Down
Loading

0 comments on commit d5a6af2

Please sign in to comment.