Skip to content

Commit

Permalink
Adjust Error stack columns numbers by 1
Browse files Browse the repository at this point in the history
To account for differences between error stacks (1-based) and ASTs (0-based). In practice this change should not make an observable difference.
  • Loading branch information
Brian Vaughn committed Jul 13, 2021
1 parent a4ecd85 commit ebb5480
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/react-devtools-extensions/src/astUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ function checkNodeLocation(
): boolean {
const {start, end} = path.node.loc;

// Column numbers are representated differently between tools/engines.
// Error.prototype.stack columns are 1-based (like most IDEs) but ASTs are 0-based.
//
// In practice this will probably never matter,
// because this code matches the 1-based Error stack location for the hook Identifier (e.g. useState)
// with the larger 0-based VariableDeclarator (e.g. [foo, setFoo] = useState())
// so the ranges should always overlap.
//
// For more info see https://github.com/facebook/react/pull/21833#discussion_r666831276
column -= 1;

if (line < start.line || line > end.line) {
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/react-devtools-extensions/src/parseHookNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ function findHookNames(
line: lineNumber,

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

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

0 comments on commit ebb5480

Please sign in to comment.