Adjust Error stack columns numbers by 1 (#21865)
To account for differences between error stacks (1-based) and ASTs (0-based). In practice this change should not make an observable difference.
Brian Vaughn committed
Jul 13, 2021 at 13:28 UTC
64bbd7a7f1b7874ffcbfc69fa87be4f606e5adc5
2 files changed
+13
packages/react-devtools-extensions/src/astUtils.js
+11
@@ -40,6 +40,17 @@ function checkNodeLocation(
40
return false;
41
}
42
43
+ // Column numbers are representated differently between tools/engines.
44
+ // Error.prototype.stack columns are 1-based (like most IDEs) but ASTs are 0-based.
45
+ //
46
+ // In practice this will probably never matter,
47
+ // because this code matches the 1-based Error stack location for the hook Identifier (e.g. useState)
48
+ // with the larger 0-based VariableDeclarator (e.g. [foo, setFoo] = useState())
49
+ // so the ranges should always overlap.
50
+ //
51
+ // For more info see https://github.com/facebook/react/pull/21833#discussion_r666831276
52
+ column -= 1;
53
+
54
if (
55
(line === start.line && column < start.column) ||
56
(line === end.line && column > end.column)
packages/react-devtools-extensions/src/parseHookNames.js
+2
@@ -378,6 +378,7 @@ function findHookNames(
378
line: lineNumber,
379
380
// Column numbers are representated differently between tools/engines.
381
+ // Error.prototype.stack columns are 1-based (like most IDEs) but ASTs are 0-based.
382
// For more info see https://github.com/facebook/react/issues/21792#issuecomment-873171991
383
column: columnNumber - 1,
384
});
@@ -476,6 +477,7 @@ async function parseSourceAST(
477
line: lineNumber,
478
479
// Column numbers are representated differently between tools/engines.
480
+ // Error.prototype.stack columns are 1-based (like most IDEs) but ASTs are 0-based.
481
// For more info see https://github.com/facebook/react/issues/21792#issuecomment-873171991
482
column: columnNumber - 1,
483
});