Skip to content

Commit 57a123f

Browse files
author
Adam J. Hines
committed
Make sourcemap plainobject for use w / valueToNode
When the sourcemaps were switched to use "@ampproject/remapping", the type of the inputSourceMap that gets passed around changed from being a plain js object to a `class SourceMap` fromthe remapping package. This causes issues with the @babel/types valueToNode function because that function is defined to throw for objects that aren't plain-objects. In practice I was seeing this error after upgrading babel when running code- coverage, and I saw a similar error reported here: vuejs/vue-jest#450 before deciding to try to track the error down myself.
1 parent 5c2fcad commit 57a123f

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-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
@@ -30,7 +30,13 @@ export default function mergeSourceMap(
3030
if (typeof inputMap.sourceRoot === "string") {
3131
result.sourceRoot = inputMap.sourceRoot;
3232
}
33-
return result;
33+
34+
// remapping returns a SourceMap class type, but this breaks code downstream in
35+
// @babel/traverse and @babel/types that relies on data being plain objects.
36+
// When it encounters the sourcemap type it outputs a "don't know how to turn
37+
// this value into a node" error. As a result, we are converting the merged
38+
// sourcemap to a plain js object.
39+
return { ...result };
3440
}
3541

3642
function rootless(map: SourceMap): SourceMap {

packages/babel-core/test/merge-map.js

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

0 commit comments

Comments
 (0)