Skip to content

Commit 99f498a

Browse files
JLHwungCopilot
andauthored
[7.x packport]Improve input source map handling (#18001)
* fix: improve inputSourceMap URL handling * refactor: extract transformation/read-input-source-map-file * chore: keep sub in git * Improve relative upward check Co-authored-by: Copilot Autofix powered by AI <[email protected]> * address review comments * fix: replace obug usage * Polyfill find-up-simple#findUpSync for Node.js 6 * yarn dedupe * Also polyfill dirTreeDisplay test helper * unused ts-expect-error due to yarn-dedupe * fix linting errors --------- Co-authored-by: Copilot Autofix powered by AI <[email protected]>
1 parent feba0a3 commit 99f498a

41 files changed

Lines changed: 1872 additions & 698 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎eslint.config.mts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import babelParser from "@babel/eslint-parser/experimental-worker";
22
import globals from "globals";
33
import js from "@eslint/js";
44
import { defineConfig } from "eslint/config";
5-
// @ts-expect-error no types
65
import pluginImport from "eslint-plugin-import";
76
import pluginJest from "eslint-plugin-jest";
87
import pluginN from "eslint-plugin-n";

‎packages/babel-core/package.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@
4141
"./lib/config/files/index.js": "./lib/config/files/index-browser.js",
4242
"./lib/config/resolve-targets.js": "./lib/config/resolve-targets-browser.js",
4343
"./lib/transform-file.js": "./lib/transform-file-browser.js",
44+
"./lib/transformation/read-input-source-map-file.js": "./lib/transformation/read-input-source-map-file-browser.js",
4445
"./src/config/files/index.ts": "./src/config/files/index-browser.ts",
4546
"./src/config/resolve-targets.ts": "./src/config/resolve-targets-browser.ts",
46-
"./src/transform-file.ts": "./src/transform-file-browser.ts"
47+
"./src/transform-file.ts": "./src/transform-file-browser.ts",
48+
"./src/transformation/read-input-source-map-file.ts": "./src/transformation/read-input-source-map-file-browser.ts"
4749
},
4850
"dependencies": {
4951
"@babel/code-frame": "workspace:^",

‎packages/babel-core/src/transformation/normalize-file.ts‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import fs from "node:fs";
2-
import path from "node:path";
31
import buildDebug from "debug";
42
import type { Handler } from "gensync";
53
import { file, traverseFast } from "@babel/types";
64
import type * as t from "@babel/types";
75
import type { PluginPasses } from "../config/index.ts";
86
import convertSourceMap from "convert-source-map";
97
import type { SourceMapConverter as Converter } from "convert-source-map";
8+
import readInputSourceMapFile from "./read-input-source-map-file.ts";
109
import File from "./file/file.ts";
1110
import parser from "../parser/index.ts";
1211
import cloneDeep from "./util/clone-deep.ts";
@@ -82,14 +81,13 @@ export default function* normalizeFile(
8281
if (typeof options.filename === "string" && lastComment) {
8382
try {
8483
// when `lastComment` is non-null, EXTERNAL_SOURCEMAP_REGEX must have matches
85-
const match: [string, string] = EXTERNAL_SOURCEMAP_REGEX.exec(
86-
lastComment,
87-
) as any;
88-
const inputMapContent = fs.readFileSync(
89-
path.resolve(path.dirname(options.filename), match[1]),
90-
"utf8",
84+
const inputMapURL: string =
85+
EXTERNAL_SOURCEMAP_REGEX.exec(lastComment)[1];
86+
inputMap = readInputSourceMapFile(
87+
options.filename,
88+
options.root,
89+
inputMapURL,
9190
);
92-
inputMap = convertSourceMap.fromJSON(inputMapContent);
9391
} catch (err) {
9492
debug("discarding unknown file input sourcemap", err);
9593
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default function readInputSourceMapFile(): never {
2+
throw new Error(
3+
"Reading input source map files is not supported in browsers",
4+
);
5+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
import buildDebug from "debug";
4+
import convertSourceMap from "convert-source-map";
5+
import type { SourceMapConverter } from "convert-source-map";
6+
7+
const debug = buildDebug("babel:transform:file");
8+
9+
// Revised from https://github.com/sindresorhus/find-up-simple/blob/f10133c55dcbf36f84a246c6f1bbfed178dcb774/index.js#L36
10+
// for Node.js 6 compatibility
11+
function findUpSync(
12+
name: string,
13+
{
14+
cwd,
15+
stopAt,
16+
}: {
17+
cwd?: string;
18+
stopAt?: string;
19+
} = {},
20+
) {
21+
let directory = path.resolve(cwd || "");
22+
const { root } = path.parse(directory);
23+
stopAt = path.resolve(directory, stopAt || root);
24+
const isAbsoluteName = path.isAbsolute(name);
25+
26+
while (directory) {
27+
const filePath = isAbsoluteName ? name : path.join(directory, name);
28+
29+
try {
30+
const stats = fs.statSync(filePath);
31+
if (stats.isFile()) {
32+
return filePath;
33+
}
34+
} catch (_) {}
35+
36+
if (directory === stopAt || directory === root) {
37+
break;
38+
}
39+
40+
directory = path.dirname(directory);
41+
}
42+
}
43+
44+
function getInputMapPath(
45+
filename: string,
46+
root: string,
47+
inputMapURL: string,
48+
): string | null {
49+
const inputFileDir = path.dirname(filename);
50+
const inputMapPath = path.resolve(inputFileDir, inputMapURL);
51+
const relativeToInputFileDir = path.relative(inputFileDir, inputMapPath);
52+
53+
if (
54+
relativeToInputFileDir.startsWith("..") ||
55+
path.isAbsolute(relativeToInputFileDir)
56+
) {
57+
const inputPackageJSONPath = findUpSync("package.json", {
58+
cwd: inputFileDir,
59+
stopAt: root,
60+
});
61+
const inputFileRoot = inputPackageJSONPath
62+
? path.dirname(inputPackageJSONPath)
63+
: root;
64+
const relativeInputMapPath = path.relative(inputFileRoot, inputMapPath);
65+
if (
66+
relativeInputMapPath.startsWith("..") ||
67+
path.isAbsolute(relativeInputMapPath)
68+
) {
69+
debug(
70+
`discarding input sourcemap "${inputMapPath}" outside of package root "${inputFileRoot}"`,
71+
);
72+
return null;
73+
}
74+
}
75+
return inputMapPath;
76+
}
77+
78+
export default function readInputSourceMapFile(
79+
filename: string,
80+
root: string,
81+
inputMapURL: string,
82+
): SourceMapConverter | null {
83+
const inputMapPath = getInputMapPath(filename, root, inputMapURL);
84+
if (inputMapPath) {
85+
const inputMapContent = fs.readFileSync(inputMapPath, "utf8");
86+
return convertSourceMap.fromJSON(inputMapContent);
87+
}
88+
return null;
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"sourceMaps": true,
3+
"inputSourceMap": true,
4+
"generatorOpts": {
5+
"compact": true
6+
}
7+
}

‎packages/babel-core/test/fixtures/input-source-map-resolution/invalid-relative-up-across-node-modules-url-not-start-with-double-dot/input.js.map‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/babel-core/test/fixtures/input-source-map-resolution/invalid-relative-up-across-node-modules-url-not-start-with-double-dot/node_modules/corp-lib/input.js‎

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/babel-core/test/fixtures/input-source-map-resolution/invalid-relative-up-across-node-modules-url-not-start-with-double-dot/node_modules/corp-lib/package.json‎

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/babel-core/test/fixtures/input-source-map-resolution/invalid-relative-up-across-node-modules-url-not-start-with-double-dot/node_modules/corp-lib/sub/mod.js‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)