Skip to content

Commit a050534

Browse files
Brian Vaughnzhengjitf
Brian Vaughn
authored andcommitted
Adjust Error stack columns numbers by 1 (facebook#21865)
To account for differences between error stacks (1-based) and ASTs (0-based). In practice this change should not make an observable difference.
1 parent b5e8b89 commit a050534

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

packages/react-devtools-extensions/src/astUtils.js

+11
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ function checkNodeLocation(
4040
return false;
4141
}
4242

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+
4354
if (
4455
(line === start.line && column < start.column) ||
4556
(line === end.line && column > end.column)

packages/react-devtools-extensions/src/parseHookNames.js

+2
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ function findHookNames(
378378
line: lineNumber,
379379

380380
// Column numbers are representated differently between tools/engines.
381+
// Error.prototype.stack columns are 1-based (like most IDEs) but ASTs are 0-based.
381382
// For more info see https://github.com/facebook/react/issues/21792#issuecomment-873171991
382383
column: columnNumber - 1,
383384
});
@@ -476,6 +477,7 @@ async function parseSourceAST(
476477
line: lineNumber,
477478

478479
// Column numbers are representated differently between tools/engines.
480+
// Error.prototype.stack columns are 1-based (like most IDEs) but ASTs are 0-based.
479481
// For more info see https://github.com/facebook/react/issues/21792#issuecomment-873171991
480482
column: columnNumber - 1,
481483
});

0 commit comments

Comments
 (0)