getsentry / rust-sourcemap

A library for rust that implements basic sourcemap handling
Other
224 stars 28 forks source link

fix: Correct sourceRoot logic #76

Closed loewenheim closed 1 year ago

loewenheim commented 1 year ago

This changes sourcemap parsing so that source names are not automatically prefixed with the sourceRoot field. The problem with doing that is that reading and then immediately writing a sourcemap would result in the source names changing.

Instead, SourceMap now maintains a copy of the vector of source names with the sourceRoot prefix prepended. Obviously this copy needs to be kept in sync with the original source names whenever a source name or the sourceRoot changes.

Note that this PR changes the behavior of SourceMapBuilder somewhat. Now, there is no difference between parsing a sourcemap like

{ "sourceRoot": "root", "sources": ["foo.js", "bar.js"], [...]}

and constructing it with a builder:

let mut builder = SourceMapBuilder::new(None);
builder.set_source_root(Some("root"));
builder.add_source("foo.js");
builder.add_source("bar.js");
builder.into_sourcemap();

Before, the parsed version would've contained the sources "root/foo.js" and "root/bar.js", while the constructed version would've had "foo.js" and "bar.js"

Swatinem commented 1 year ago

Or maybe such a test already exists? Which might be even better :-)

loewenheim commented 1 year ago

Fair point. I'll have a look to see if this case is already covered.