Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make source maps plain objects for use with t.valueToNode #14283

Merged
merged 2 commits into from Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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);
});
});