Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
name: CI


Copy link

Copilot AI Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Extra blank line at the beginning of the file. Consider removing it for consistency.

Suggested change

Copilot uses AI. Check for mistakes.
on:
merge_group:
pull_request:
types: ["opened", "reopened", "synchronize"]
push:
branches:
- master
- "release/**"
pull_request:
- main

env:
RUSTFLAGS: -Dwarnings
Expand Down
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"rust-analyzer.cargo.features": "all",
}
17 changes: 9 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "sourcemap"
version = "9.3.0"
authors = ["Sentry <hello@sentry.io>"]
name = "swc_sourcemap"
version = "9.3.4"
authors = ["Sentry <hello@sentry.io>", "swc-project <https://swc.rs>"]
keywords = ["javascript", "sourcemap", "sourcemaps"]
description = "Basic sourcemap handling for Rust"
homepage = "https://github.com/getsentry/rust-sourcemap"
repository = "https://github.com/getsentry/rust-sourcemap"
description = "Forked from https://github.com/getsentry/rust-sourcemap"
homepage = "https://github.com/swc-project/swc-sourcemap"
repository = "https://github.com/swc-project/swc-sourcemap"
license = "BSD-3-Clause"
readme = "README.md"
edition = "2018"
Expand All @@ -24,8 +24,8 @@ all-features = true

[dependencies]
url = "2.1.1"
serde = { version = "1.0.104", features = ["derive"] }
serde_json = "1.0.48"
serde = { version = "1.0.104", features = ["derive", "rc"] }
serde_json = { version = "1.0.48", features = ["raw_value"] }
unicode-id-start = "1"
if_chain = "1.0.0"
scroll = { version = "0.12.0", features = ["derive"], optional = true }
Expand All @@ -34,6 +34,7 @@ debugid = {version = "0.8.0", features = ["serde"] }
base64-simd = { version = "0.8" }
bitvec = "1.0.1"
rustc-hash = "2.1.1"
bytes-str = { version = "0.2.4", features = ["serde"] }

[features]
ram_bundle = ["scroll"]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ memory intensive.
Usage:

```rust
use sourcemap::SourceMap;
use swc_sourcemap::SourceMap;
Copy link

Copilot AI Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] While the code example on line 31 has been updated correctly to use swc_sourcemap, the installation instructions earlier in the README (lines 7-18, outside the diff) still reference the old sourcemap package name and getsentry repository. These should ideally be updated for consistency, though they fall outside the changed region.

Copilot uses AI. Check for mistakes.
let input: &[_] = b"{
\"version\":3,
\"sources\":[\"coolstuff.js\"],
Expand Down
2 changes: 1 addition & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::PathBuf;
use std::process;

use argh::FromArgs;
use sourcemap::{DecodedMap, SourceView, Token};
use swc_sourcemap::{DecodedMap, SourceView, Token};

/// Utility for working with source maps.
#[derive(FromArgs, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion examples/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::env;
use std::fs;
use std::io::Read;

use sourcemap::{decode, DecodedMap, RewriteOptions, SourceMap};
use swc_sourcemap::{decode, DecodedMap, RewriteOptions, SourceMap};

fn load_from_reader<R: Read>(mut rdr: R) -> SourceMap {
match decode(&mut rdr).unwrap() {
Expand Down
4 changes: 2 additions & 2 deletions examples/rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fs;
use std::io::Read;
use std::path::Path;

use sourcemap::{decode, DecodedMap, RewriteOptions, SourceMap};
use swc_sourcemap::{decode, DecodedMap, RewriteOptions, SourceMap};

fn test(sm: &SourceMap) {
for (src_id, source) in sm.sources().enumerate() {
Expand All @@ -14,7 +14,7 @@ fn test(sm: &SourceMap) {
if f.read_to_string(&mut contents).ok().is_none() {
continue;
}
if Some(contents.as_str()) != sm.get_source_contents(src_id as u32) {
if Some(contents.as_str()) != sm.get_source_contents(src_id as u32).map(|v| &**v) {
println!(" !!! {source}");
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/split_ram_bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::fs;
use std::fs::File;
use std::path::Path;

use sourcemap::ram_bundle::{split_ram_bundle, RamBundle, RamBundleType};
use sourcemap::SourceMapIndex;
use swc_sourcemap::ram_bundle::{split_ram_bundle, RamBundle, RamBundleType};
use swc_sourcemap::SourceMapIndex;

const USAGE: &str = "
Usage:
Expand Down
62 changes: 31 additions & 31 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::env;
use std::fs;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use bytes_str::BytesStr;
use debugid::DebugId;
use rustc_hash::FxHashMap;
use url::Url;
Expand All @@ -20,14 +20,14 @@ use crate::types::{RawToken, SourceMap, Token};
/// objects is generally not very comfortable. As a general aid this
/// type can help.
pub struct SourceMapBuilder {
file: Option<Arc<str>>,
name_map: FxHashMap<Arc<str>, u32>,
names: Vec<Arc<str>>,
file: Option<BytesStr>,
name_map: FxHashMap<BytesStr, u32>,
names: Vec<BytesStr>,
tokens: Vec<RawToken>,
source_map: FxHashMap<Arc<str>, u32>,
source_root: Option<Arc<str>>,
sources: Vec<Arc<str>>,
source_contents: Vec<Option<Arc<str>>>,
source_map: FxHashMap<BytesStr, u32>,
source_root: Option<BytesStr>,
sources: Vec<BytesStr>,
source_contents: Vec<Option<BytesStr>>,
sources_mapping: Vec<u32>,
ignore_list: BTreeSet<u32>,
debug_id: Option<DebugId>,
Expand All @@ -52,9 +52,9 @@ fn resolve_local_reference(base: &Url, reference: &str) -> Option<PathBuf> {

impl SourceMapBuilder {
/// Creates a new source map builder and sets the file.
pub fn new(file: Option<&str>) -> SourceMapBuilder {
pub fn new(file: Option<BytesStr>) -> SourceMapBuilder {
SourceMapBuilder {
file: file.map(Into::into),
file,
name_map: FxHashMap::default(),
names: vec![],
tokens: vec![],
Expand All @@ -74,7 +74,7 @@ impl SourceMapBuilder {
}

/// Sets the file for the sourcemap (optional)
pub fn set_file<T: Into<Arc<str>>>(&mut self, value: Option<T>) {
pub fn set_file<T: Into<BytesStr>>(&mut self, value: Option<T>) {
self.file = value.map(Into::into);
}

Expand All @@ -84,7 +84,7 @@ impl SourceMapBuilder {
}

/// Sets a new value for the source_root.
pub fn set_source_root<T: Into<Arc<str>>>(&mut self, value: Option<T>) {
pub fn set_source_root<T: Into<BytesStr>>(&mut self, value: Option<T>) {
self.source_root = value.map(Into::into);
}

Expand All @@ -94,24 +94,24 @@ impl SourceMapBuilder {
}

/// Registers a new source with the builder and returns the source ID.
pub fn add_source(&mut self, src: &str) -> u32 {
pub fn add_source(&mut self, src: BytesStr) -> u32 {
self.add_source_with_id(src, !0)
}

fn add_source_with_id(&mut self, src: &str, old_id: u32) -> u32 {
fn add_source_with_id(&mut self, src: BytesStr, old_id: u32) -> u32 {
let count = self.sources.len() as u32;
let id = *self.source_map.entry(src.into()).or_insert(count);
let id = *self.source_map.entry(src.clone()).or_insert(count);
if id == count {
self.sources.push(src.into());
self.sources.push(src);
self.sources_mapping.push(old_id);
}
id
}

/// Changes the source name for an already set source.
pub fn set_source(&mut self, src_id: u32, src: &str) {
pub fn set_source(&mut self, src_id: u32, src: BytesStr) {
assert!(src_id != !0, "Cannot set sources for tombstone source id");
self.sources[src_id as usize] = src.into();
self.sources[src_id as usize] = src;
}

/// Looks up a source name for an ID.
Expand All @@ -124,12 +124,12 @@ impl SourceMapBuilder {
}

/// Sets the source contents for an already existing source.
pub fn set_source_contents(&mut self, src_id: u32, contents: Option<&str>) {
pub fn set_source_contents(&mut self, src_id: u32, contents: Option<BytesStr>) {
assert!(src_id != !0, "Cannot set sources for tombstone source id");
if self.sources.len() > self.source_contents.len() {
self.source_contents.resize(self.sources.len(), None);
}
self.source_contents[src_id as usize] = contents.map(Into::into);
self.source_contents[src_id as usize] = contents;
}

/// Returns the current source contents for a source.
Expand Down Expand Up @@ -169,7 +169,7 @@ impl SourceMapBuilder {
if let Ok(mut f) = fs::File::open(path) {
let mut contents = String::new();
if f.read_to_string(&mut contents).is_ok() {
self.set_source_contents(src_id, Some(&contents));
self.set_source_contents(src_id, Some(contents.into()));
}
}
}
Expand All @@ -178,11 +178,11 @@ impl SourceMapBuilder {
}

/// Registers a name with the builder and returns the name ID.
pub fn add_name(&mut self, name: &str) -> u32 {
pub fn add_name(&mut self, name: BytesStr) -> u32 {
let count = self.names.len() as u32;
let id = *self.name_map.entry(name.into()).or_insert(count);
let id = *self.name_map.entry(name.clone()).or_insert(count);
if id == count {
self.names.push(name.into());
self.names.push(name);
}
id
}
Expand All @@ -195,8 +195,8 @@ impl SourceMapBuilder {
dst_col: u32,
src_line: u32,
src_col: u32,
source: Option<&str>,
name: Option<&str>,
source: Option<BytesStr>,
name: Option<BytesStr>,
is_range: bool,
) -> RawToken {
self.add_with_id(
Expand All @@ -211,9 +211,9 @@ impl SourceMapBuilder {
dst_col: u32,
src_line: u32,
src_col: u32,
source: Option<&str>,
source: Option<BytesStr>,
source_id: u32,
name: Option<&str>,
name: Option<BytesStr>,
is_range: bool,
) -> RawToken {
let src_id = match source {
Expand Down Expand Up @@ -273,9 +273,9 @@ impl SourceMapBuilder {
token.get_dst_col(),
token.get_src_line(),
token.get_src_col(),
token.get_source(),
token.get_source().cloned(),
token.get_src_id(),
name,
name.cloned(),
token.is_range(),
)
}
Expand All @@ -289,7 +289,7 @@ impl SourceMapBuilder {
prefix.push('/');
}
if source.starts_with(&prefix) {
*source = source[prefix.len()..].into();
source.advance(prefix.len());
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ pub fn strip_junk_header(slice: &[u8]) -> io::Result<&[u8]> {
}

/// Decodes range mappping bitfield string into index
fn decode_rmi(rmi_str: &str, val: &mut BitVec<u8, Lsb0>) -> Result<()> {
pub(crate) fn decode_rmi(rmi_str: &str, val: &mut BitVec<u8, Lsb0>) -> Result<()> {
val.clear();
val.resize(rmi_str.len() * 6, false);

Expand Down Expand Up @@ -295,7 +295,7 @@ fn decode_index(rsm: RawSourceMap) -> Result<SourceMapIndex> {

// file sometimes is not a string for unexplicable reasons
let file = rsm.file.map(|val| match val {
Value::String(s) => s,
Value::String(s) => s.into(),
_ => "<invalid>".into(),
});

Expand Down
4 changes: 2 additions & 2 deletions src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ pub fn encode<M: Encodable, W: Write>(sm: &M, mut w: W) -> Result<()> {
Ok(())
}

fn encode_vlq_diff(out: &mut String, a: u32, b: u32) {
pub(crate) fn encode_vlq_diff(out: &mut String, a: u32, b: u32) {
encode_vlq(out, i64::from(a) - i64::from(b))
}

fn encode_rmi(out: &mut Vec<u8>, data: &[u8]) {
pub(crate) fn encode_rmi(out: &mut Vec<u8>, data: &[u8]) {
fn encode_byte(b: u8) -> u8 {
match b {
0..=25 => b + b'A',
Expand Down
13 changes: 6 additions & 7 deletions src/hermes.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use bytes_str::BytesStr;

use crate::decoder::{decode, decode_regular, decode_slice};
use crate::encoder::{encode, Encodable};
use crate::errors::{Error, Result};
Expand All @@ -21,7 +23,7 @@ pub struct HermesScopeOffset {

#[derive(Debug, Clone, PartialEq)]
pub struct HermesFunctionMap {
names: Vec<String>,
names: Vec<BytesStr>,
mappings: Vec<HermesScopeOffset>,
}

Expand Down Expand Up @@ -93,14 +95,14 @@ impl SourceMapHermes {

/// Given a bytecode offset, this will find the enclosing scopes function
/// name.
pub fn get_original_function_name(&self, bytecode_offset: u32) -> Option<&str> {
pub fn get_original_function_name(&self, bytecode_offset: u32) -> Option<&BytesStr> {
let token = self.sm.lookup_token(0, bytecode_offset)?;

self.get_scope_for_token(token)
}

/// Resolves the name of the enclosing function for the given [`Token`].
pub fn get_scope_for_token(&self, token: Token) -> Option<&str> {
pub fn get_scope_for_token(&self, token: Token) -> Option<&BytesStr> {
let function_map = self
.function_maps
.get(token.get_src_id() as usize)?
Expand All @@ -115,10 +117,7 @@ impl SourceMapHermes {
&(token.get_src_line() + 1, token.get_src_col()),
|o| (o.line, o.column),
)?;
function_map
.names
.get(mapping.name_index as usize)
.map(|n| n.as_str())
function_map.names.get(mapping.name_index as usize)
}

/// This rewrites the sourcemap according to the provided rewrite
Expand Down
3 changes: 2 additions & 1 deletion src/jsontypes.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bytes_str::BytesStr;
use debugid::DebugId;
use serde::de::IgnoredAny;
use serde::{Deserialize, Deserializer, Serialize};
Expand All @@ -19,7 +20,7 @@ pub struct RawSection {

#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub struct FacebookScopeMapping {
pub names: Vec<String>,
pub names: Vec<BytesStr>,
pub mappings: String,
}

Expand Down
Loading