Skip to content

Commit 904bef1

Browse files
committed
[compiler] Improve display of errors on multi-line expressions (#34963)
When a longer function or expression is identified as the source of an error, we currently print the entire expression in our error message. This is because we delegate to a Babel helper to print codeframes. Here, we add some checking and abbreviate the result if it spans too many lines. DiffTrain build for [408b38e](408b38e)
1 parent bdfee6a commit 904bef1

35 files changed

+103
-87
lines changed

compiled/eslint-plugin-react-hooks/index.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17654,6 +17654,10 @@ function hasOwnProperty$1(obj, key) {
1765417654
return Object.prototype.hasOwnProperty.call(obj, key);
1765517655
}
1765617656

17657+
const CODEFRAME_LINES_ABOVE = 2;
17658+
const CODEFRAME_LINES_BELOW = 3;
17659+
const CODEFRAME_MAX_LINES = 10;
17660+
const CODEFRAME_ABBREVIATED_SOURCE_LINES = 5;
1765717661
var ErrorSeverity;
1765817662
(function (ErrorSeverity) {
1765917663
ErrorSeverity["Error"] = "Error";
@@ -17970,7 +17974,7 @@ class CompilerError extends Error {
1797017974
}
1797117975
}
1797217976
function printCodeFrame(source, loc, message) {
17973-
return libExports.codeFrameColumns(source, {
17977+
const printed = libExports.codeFrameColumns(source, {
1797417978
start: {
1797517979
line: loc.start.line,
1797617980
column: loc.start.column + 1,
@@ -17981,7 +17985,19 @@ function printCodeFrame(source, loc, message) {
1798117985
},
1798217986
}, {
1798317987
message,
17988+
linesAbove: CODEFRAME_LINES_ABOVE,
17989+
linesBelow: CODEFRAME_LINES_BELOW,
1798417990
});
17991+
const lines = printed.split(/\r?\n/);
17992+
if (loc.end.line - loc.start.line < CODEFRAME_MAX_LINES) {
17993+
return printed;
17994+
}
17995+
const pipeIndex = lines[0].indexOf('|');
17996+
return [
17997+
...lines.slice(0, CODEFRAME_LINES_ABOVE + CODEFRAME_ABBREVIATED_SOURCE_LINES),
17998+
' '.repeat(pipeIndex) + '…',
17999+
...lines.slice(-(CODEFRAME_LINES_BELOW + CODEFRAME_ABBREVIATED_SOURCE_LINES)),
18000+
].join('\n');
1798518001
}
1798618002
function printErrorSummary(category, message) {
1798718003
let heading;

compiled/facebook-www/REVISION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
09056abde76c464f4632f322a0ac30cd3984cee6
1+
408b38ef7304faf022d2a37110c57efce12c6bad
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
09056abde76c464f4632f322a0ac30cd3984cee6
1+
408b38ef7304faf022d2a37110c57efce12c6bad

compiled/facebook-www/React-dev.classic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1499,7 +1499,7 @@ __DEV__ &&
14991499
exports.useTransition = function () {
15001500
return resolveDispatcher().useTransition();
15011501
};
1502-
exports.version = "19.3.0-www-classic-09056abd-20251023";
1502+
exports.version = "19.3.0-www-classic-408b38ef-20251023";
15031503
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
15041504
"function" ===
15051505
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/React-dev.modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1499,7 +1499,7 @@ __DEV__ &&
14991499
exports.useTransition = function () {
15001500
return resolveDispatcher().useTransition();
15011501
};
1502-
exports.version = "19.3.0-www-modern-09056abd-20251023";
1502+
exports.version = "19.3.0-www-modern-408b38ef-20251023";
15031503
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
15041504
"function" ===
15051505
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/React-prod.classic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,4 +606,4 @@ exports.useSyncExternalStore = function (
606606
exports.useTransition = function () {
607607
return ReactSharedInternals.H.useTransition();
608608
};
609-
exports.version = "19.3.0-www-classic-09056abd-20251023";
609+
exports.version = "19.3.0-www-classic-408b38ef-20251023";

compiled/facebook-www/React-prod.modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,4 +606,4 @@ exports.useSyncExternalStore = function (
606606
exports.useTransition = function () {
607607
return ReactSharedInternals.H.useTransition();
608608
};
609-
exports.version = "19.3.0-www-modern-09056abd-20251023";
609+
exports.version = "19.3.0-www-modern-408b38ef-20251023";

compiled/facebook-www/React-profiling.classic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ exports.useSyncExternalStore = function (
610610
exports.useTransition = function () {
611611
return ReactSharedInternals.H.useTransition();
612612
};
613-
exports.version = "19.3.0-www-classic-09056abd-20251023";
613+
exports.version = "19.3.0-www-classic-408b38ef-20251023";
614614
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
615615
"function" ===
616616
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/React-profiling.modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ exports.useSyncExternalStore = function (
610610
exports.useTransition = function () {
611611
return ReactSharedInternals.H.useTransition();
612612
};
613-
exports.version = "19.3.0-www-modern-09056abd-20251023";
613+
exports.version = "19.3.0-www-modern-408b38ef-20251023";
614614
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
615615
"function" ===
616616
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/ReactART-dev.classic.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20371,10 +20371,10 @@ __DEV__ &&
2037120371
(function () {
2037220372
var internals = {
2037320373
bundleType: 1,
20374-
version: "19.3.0-www-classic-09056abd-20251023",
20374+
version: "19.3.0-www-classic-408b38ef-20251023",
2037520375
rendererPackageName: "react-art",
2037620376
currentDispatcherRef: ReactSharedInternals,
20377-
reconcilerVersion: "19.3.0-www-classic-09056abd-20251023"
20377+
reconcilerVersion: "19.3.0-www-classic-408b38ef-20251023"
2037820378
};
2037920379
internals.overrideHookState = overrideHookState;
2038020380
internals.overrideHookStateDeletePath = overrideHookStateDeletePath;
@@ -20409,7 +20409,7 @@ __DEV__ &&
2040920409
exports.Shape = Shape;
2041020410
exports.Surface = Surface;
2041120411
exports.Text = Text;
20412-
exports.version = "19.3.0-www-classic-09056abd-20251023";
20412+
exports.version = "19.3.0-www-classic-408b38ef-20251023";
2041320413
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
2041420414
"function" ===
2041520415
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

0 commit comments

Comments
 (0)