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
10 changes: 6 additions & 4 deletions src/sourcemap_jsc/JSSourceMap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,12 @@ impl JSSourceMap {
// Parse the payload to create a proper sourcemap

// Extract mappings string from payload
let Some(mappings_value) = payload_arg.get_stringish(global, b"mappings")? else {
return Err(
global.throw_invalid_arguments(format_args!("payload 'mappings' must be a string"))
);
let mappings_value = match payload_arg.get(global, b"mappings")? {
Some(value) if !value.is_undefined_or_null() => value.to_bun_string(global)?,
_ => {
return Err(global
.throw_invalid_arguments(format_args!("payload 'mappings' must be a string")));
}
};
let mappings_value = bstring::OwnedString::new(mappings_value);

Expand Down
33 changes: 33 additions & 0 deletions test/js/node/module/sourcemap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,39 @@ test("SourceMap payload must be an object", () => {
);
});

test("SourceMap accepts an empty mappings string", () => {
// esbuild and tsc emit `"mappings": ""` for modules that produce no code,
// such as re-export-only or side-effect-only modules.
const payload = { version: 3, sources: ["empty.js"], names: [], mappings: "" };
const sourceMap = new SourceMap(payload);

expect(sourceMap).toBeInstanceOf(SourceMap);
expect(sourceMap.payload).toEqual(payload);
expect(sourceMap.findEntry(0, 0)).toEqual({});
expect(sourceMap.findOrigin(1, 1)).toEqual({});
});

test("SourceMap accepts mappings made only of line separators", () => {
const sourceMap = new SourceMap({ version: 3, sources: ["a.js"], names: [], mappings: ";;;" });

expect(sourceMap.findEntry(0, 0)).toEqual({});
expect(sourceMap.findEntry(3, 0)).toEqual({});
});

test.each([
["missing", { version: 3, sources: ["a.js"], names: [] }],
["undefined", { version: 3, sources: ["a.js"], names: [], mappings: undefined }],
["null", { version: 3, sources: ["a.js"], names: [], mappings: null }],
])("SourceMap constructor rejects %s mappings", (_, payload) => {
expect(() => new SourceMap(payload)).toThrow(
expect.objectContaining({
name: "TypeError",
code: "ERR_INVALID_ARG_TYPE",
message: "payload 'mappings' must be a string",
}),
);
});

test("SourceMap instance has expected methods", () => {
const sourceMap = new SourceMap({
version: 3,
Expand Down
Loading