From 648c7a3292959ef552f0ab81958d161e1f5d4abb Mon Sep 17 00:00:00 2001 From: "Adam J. Hines" Date: Wed, 16 Feb 2022 22:06:34 -0700 Subject: [PATCH] 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: https://github.com/vuejs/vue-jest/issues/450 before deciding to try to track the error down myself. --- .../src/transformation/file/merge-map.ts | 8 +++++- packages/babel-core/test/merge-map.js | 28 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 packages/babel-core/test/merge-map.js 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(); + }); +});