Skip to content

Commit b7631ff

Browse files
thebanjomaticAdam J. Hinesnicolo-ribaudo
authored
Make source maps plain objects for use with t.valueToNode (#14283)
Co-authored-by: Adam J. Hines <[email protected]> Co-authored-by: Nicolò Ribaudo <[email protected]>
1 parent ea1c44e commit b7631ff

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

packages/babel-core/src/transformation/file/merge-map.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ export default function mergeSourceMap(
3838
if (typeof inputMap.sourceRoot === "string") {
3939
result.sourceRoot = inputMap.sourceRoot;
4040
}
41-
return result;
41+
42+
// remapping returns a SourceMap class type, but this breaks code downstream in
43+
// @babel/traverse and @babel/types that relies on data being plain objects.
44+
// When it encounters the sourcemap type it outputs a "don't know how to turn
45+
// this value into a node" error. As a result, we are converting the merged
46+
// sourcemap to a plain js object.
47+
return { ...result };
4248
}
4349

4450
function rootless(map: SourceMap): SourceMap {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import _mergeSourceMap from "../lib/transformation/file/merge-map.js";
2+
const mergeSourceMap = _mergeSourceMap.default;
3+
4+
describe("merge-map", () => {
5+
it("returns a plain js object", () => {
6+
const inputMap = {
7+
file: "file.js",
8+
mappings: [],
9+
names: [],
10+
sources: ["file.ts"],
11+
version: 3,
12+
};
13+
14+
const outputMap = {
15+
file: "file.transpiled.js",
16+
mappings: [],
17+
names: [],
18+
sources: ["file.js"],
19+
version: 3,
20+
};
21+
22+
const map = mergeSourceMap(inputMap, outputMap, "file.transpiled.js");
23+
expect(typeof map).toBe("object");
24+
expect(Object.prototype.toString.call(map)).toBe("[object Object]");
25+
expect(Object.getPrototypeOf(map)).toBe(Object.prototype);
26+
});
27+
});

0 commit comments

Comments
 (0)