Skip to content

Commit 42c765b

Browse files
authored
Fix infinite recursion when merging sourcemaps (#14274)
* Fix infinite recursion when merge sourcemaps * Add test case * Externalize the sourcemap
1 parent dc28575 commit 42c765b

6 files changed

Lines changed: 37 additions & 1 deletion

File tree

packages/babel-core/src/transformation/file/merge-map.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ export default function mergeSourceMap(
66
map: SourceMap,
77
source: string,
88
): SourceMap {
9+
// Prevent an infinite recursion if one of the input map's sources has the
10+
// same resolved path as the input map. In the case, it would keep find the
11+
// input map, then get it's sources which will include a path like the input
12+
// map, on and on.
13+
let found = false;
914
const result = remapping(rootless(map), (s, ctx) => {
10-
if (s === source) {
15+
if (s === source && !found) {
16+
found = true;
1117
// We empty the source location, which will prevent the sourcemap from
1218
// becoming relative to the input's location. Eg, if we're transforming a
1319
// file 'foo/bar.js', and it is a transformation of a `baz.js` file in the
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var foo = function () {
2+
return 4;
3+
};
4+
5+
//# sourceMappingURL=input.js.map
6+

packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-same-location/input.js.map

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"inputSourceMap": true
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var foo = function () {
2+
return 4;
3+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"mappings": "AAAA,UAAU,Y;SAAM;AAAC,CAAjB",
3+
"names": [],
4+
"sources": ["source-maps/input-source-map-same-location/input.js"],
5+
"sourcesContent": ["var foo = () => 4;"],
6+
"version": 3
7+
}

0 commit comments

Comments
 (0)