Skip to content

Commit

Permalink
Fix infinite recursion when merging sourcemaps (#14274)
Browse files Browse the repository at this point in the history
* Fix infinite recursion when merge sourcemaps

* Add test case

* Externalize the sourcemap
  • Loading branch information
jridgewell committed Feb 15, 2022
1 parent dc28575 commit 42c765b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/babel-core/src/transformation/file/merge-map.ts
Expand Up @@ -6,8 +6,14 @@ export default function mergeSourceMap(
map: SourceMap,
source: string,
): SourceMap {
// Prevent an infinite recursion if one of the input map's sources has the
// same resolved path as the input map. In the case, it would keep find the
// input map, then get it's sources which will include a path like the input
// map, on and on.
let found = false;
const result = remapping(rootless(map), (s, ctx) => {
if (s === source) {
if (s === source && !found) {
found = true;
// We empty the source location, which will prevent the sourcemap from
// becoming relative to the input's location. Eg, if we're transforming a
// file 'foo/bar.js', and it is a transformation of a `baz.js` file in the
Expand Down
@@ -0,0 +1,6 @@
var foo = function () {
return 4;
};

//# sourceMappingURL=input.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -0,0 +1,3 @@
{
"inputSourceMap": true
}
@@ -0,0 +1,3 @@
var foo = function () {
return 4;
};
@@ -0,0 +1,7 @@
{
"mappings": "AAAA,UAAU,Y;SAAM;AAAC,CAAjB",
"names": [],
"sources": ["source-maps/input-source-map-same-location/input.js"],
"sourcesContent": ["var foo = () => 4;"],
"version": 3
}

0 comments on commit 42c765b

Please sign in to comment.