diff --git a/packages/babel-core/src/transformation/file/merge-map.ts b/packages/babel-core/src/transformation/file/merge-map.ts index 4b902c27bf82..61d377f45ac5 100644 --- a/packages/babel-core/src/transformation/file/merge-map.ts +++ b/packages/babel-core/src/transformation/file/merge-map.ts @@ -30,7 +30,13 @@ export default function mergeSourceMap( if (typeof inputMap.sourceRoot === "string") { result.sourceRoot = inputMap.sourceRoot; } - return result; + + // remapping returns a SourceMap class type, but this breaks code downstream in + // @babel/traverse and @babel/types that relies on data being plain objects. + // When it encounters the sourcemap type it outputs a "don't know how to turn + // this value into a node" error. As a result, we are converting the merged + // sourcemap to a plain js object. + return { ...result }; } function rootless(map: SourceMap): SourceMap { diff --git a/packages/babel-core/test/merge-map.js b/packages/babel-core/test/merge-map.js new file mode 100644 index 000000000000..b3c28a82081f --- /dev/null +++ b/packages/babel-core/test/merge-map.js @@ -0,0 +1,28 @@ +import _mergeSourceMap from "../lib/transformation/file/merge-map.js"; +const mergeSourceMap = _mergeSourceMap.default; + +const inputMap = { + file: "file.js", + mappings: [], + names: [], + sources: ["file.ts"], + version: 3, +}; + +const outputMap = { + file: "file.transpiled.js", + mappings: [], + names: [], + sources: ["file.js"], + version: 3, +}; + +describe("merge-map", () => { + it("returns a plain js object", () => { + const map = mergeSourceMap(inputMap, outputMap, "file.transpiled.js"); + const proto = Object.getPrototypeOf(map) || Object.getPrototypeOf({}); + expect(typeof map).toBe("object"); + expect(Object.prototype.toString.call(map)).toBe("[object Object]"); + expect(Object.getPrototypeOf(proto)).toBeNull(); + }); +});