diff --git a/src/sourcemap_jsc/JSSourceMap.rs b/src/sourcemap_jsc/JSSourceMap.rs index f284e4b80e42..3f611c7e928f 100644 --- a/src/sourcemap_jsc/JSSourceMap.rs +++ b/src/sourcemap_jsc/JSSourceMap.rs @@ -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); diff --git a/test/js/node/module/sourcemap.test.js b/test/js/node/module/sourcemap.test.js index 58cd458697c1..8642d77317d9 100644 --- a/test/js/node/module/sourcemap.test.js +++ b/test/js/node/module/sourcemap.test.js @@ -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,