Skip to content

Commit

Permalink
Make source maps plain objects for use with t.valueToNode (#14283)
Browse files Browse the repository at this point in the history
Co-authored-by: Adam J. Hines <ahines@factset.com>
Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
3 people committed Feb 17, 2022
1 parent ea1c44e commit b7631ff
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/babel-core/src/transformation/file/merge-map.ts
Expand Up @@ -38,7 +38,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
27 changes: 27 additions & 0 deletions packages/babel-core/test/merge-map.js
@@ -0,0 +1,27 @@
import _mergeSourceMap from "../lib/transformation/file/merge-map.js";
const mergeSourceMap = _mergeSourceMap.default;

describe("merge-map", () => {
it("returns a plain js object", () => {
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,
};

const map = mergeSourceMap(inputMap, outputMap, "file.transpiled.js");
expect(typeof map).toBe("object");
expect(Object.prototype.toString.call(map)).toBe("[object Object]");
expect(Object.getPrototypeOf(map)).toBe(Object.prototype);
});
});

0 comments on commit b7631ff

Please sign in to comment.