fix(code-frame): use 0-based columns to match Babel AST locations (#1…#17849
Open
nehemiyawicks wants to merge 1 commit intobabel:mainfrom
Open
fix(code-frame): use 0-based columns to match Babel AST locations (#1…#17849nehemiyawicks wants to merge 1 commit intobabel:mainfrom
nehemiyawicks wants to merge 1 commit intobabel:mainfrom
Conversation
Collaborator
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/61092 |
…bel#17316) `codeFrameColumns` treated the `column` property as 1-based, but Babel AST locations use 0-based columns. This caused the caret/underline to be off by one character when passing AST `.loc` objects directly. - Removed the `- 1` offset in marker spacing calculation - Removed the `+ 1` in multiline start marker count - Changed default column sentinel from `0` to `null` so that `column: 0` is treated as a valid position - Updated falsiness checks (`!startColumn`) to null checks - Removed `+ 1` workarounds in babel-core and babel-traverse callers This is a breaking change for external consumers of `@babel/code-frame` who passed 1-based columns manually.
0a17403 to
4b94a8d
Compare
|
commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #17316
codeFrameColumnscurrently treats columns as 1-based, while Babel AST locations are 0-based. This mismatch causes caret/underline markers to be off by one character whencodeFrameColumnsis called with AST locations directly.This PR updates
@babel/code-frameto treat columns as 0-based, and removes the corresponding+ 1compensations in internal callers (@babel/core,@babel/traverse) that were working around the previous behavior.Changes
@babel/code-frame(common.ts)0tonullso that column0is a valid position!startColumn,if (startColumn)) with explicit null checks (startColumn == null,startColumn != null)+ 1compensation in marker length calculation- 1offset in marker spacing calculation@babel/coreand@babel/traversecolumn: loc.column + 1in 3 call sites that were converting 0-based AST columns to 1-based before passing them tocodeFrameColumnsTests
Breaking change note
This may be a breaking change for external consumers who manually pass 1-based column values to
codeFrameColumns.Consumers passing Babel AST
.locobjects (the primary use case) will now receive correct results without manual adjustment.If preferred, this change can be targeted for Babel 8.
Test plan
@babel/code-frametests pass (31 tests)@babel/coretests pass (483 tests)@babel/parsertests pass (16,083 tests)@babel/traversetests pass (650 tests)@babel/parseroutput for both valid code (AST loc) and syntax errors (error loc)