Skip to content

Commit

Permalink
Make sourcemap plainobject for use w / valueToNode
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Adam J. Hines committed Feb 17, 2022
1 parent 5c2fcad commit 6dbb4ff
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/babel-core/src/transformation/file/merge-map.ts
Expand Up @@ -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 {
Expand Down
28 changes: 28 additions & 0 deletions 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();
});
});

0 comments on commit 6dbb4ff

Please sign in to comment.