fix(code-frame): use 0-based columns to match Babel AST locations (#1…#17849
Conversation
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/61475 |
0a17403 to
4b94a8d
Compare
|
commit: |
4b94a8d to
166282f
Compare
There was a problem hiding this comment.
Pull request overview
This PR aligns @babel/code-frame column handling with Babel AST locations by treating columns as 0-based, and updates internal Babel callers/tests to remove prior + 1 compensations.
Changes:
- Update
@babel/code-framemarker calculations to interpretcolumnas 0-based (including allowing column0as a valid value). - Remove
loc.column + 1adjustments in@babel/coreand@babel/traversecall sites. - Update and extend
@babel/code-frametests to use 0-based columns (including new coverage for column0).
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| packages/babel-traverse/src/path/replacement.ts | Removes + 1 adjustment when passing parser error locations into codeFrameColumns. |
| packages/babel-core/src/transformation/file/file.ts | Removes + 1 adjustments for node locs passed into codeFrameColumns. |
| packages/babel-core/src/parser/index.ts | Removes + 1 adjustment for parser error locations used in code frames. |
| packages/babel-code-frame/test/index.js | Updates existing tests to 0-based columns and adds new tests for AST-style locations including column 0. |
| packages/babel-code-frame/test/color-detection.js | Updates highlight tests to use 0-based columns and column 0. |
| packages/babel-code-frame/src/common.ts | Switches missing-column sentinel to null and updates spacing/length calculations for 0-based columns. |
Comments suppressed due to low confidence (2)
packages/babel-code-frame/src/common.ts:65
getMarkerLinesnow usescolumn: nullas a sentinel and tests passcolumn: null, but the exportedLocation/NodeLocationtypes still requirecolumn: number. This makes the public TS types inconsistent with the runtime API and forces@ts-expect-errorworkarounds. Consider updatingLocationsocolumnis optional ornumber | null(and similarly forloc.end) so consumers can pass column 0 and also omit columns without type casts.
const startLoc: Location = {
// @ts-expect-error default value
column: null,
// @ts-expect-error default value
line: -1,
...loc.start,
};
packages/babel-code-frame/src/common.ts:106
- In the multi-line case, middle-line marker lengths are computed using
source[lineNumber - i].length. SincelineNumberisstartLine + i,lineNumber - ialways equalsstartLine, so for spans of more than 3 lines every middle line will incorrectly use the first line’s length. This should use the current line’s length (e.g. the same indexing approach as the first-line branch) so markers cover the full middle line correctly.
} else {
const sourceLength = source[lineNumber - i].length;
markerLines[lineNumber] = [0, sourceLength];
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
166282f to
3817e92
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
3817e92 to
36bb3f0
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 7 out of 7 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (2)
packages/babel-code-frame/src/common.ts:65
getMarkerLinesnow usescolumn: nullas the sentinel for “no column”, and tests/usage in this repo passcolumn: null, but the exportedLocation/NodeLocationtypes still requirecolumn: number. This forces@ts-expect-errorinternally and means TS consumers can’t represent the supportednull/omitted-column cases without casting. Consider updating the public types (e.g.,column?: number | null) so the runtime API and typings stay in sync, and so column0remains a valid numeric position.
const startLoc: Location = {
// @ts-expect-error default value
column: null,
// @ts-expect-error default value
line: -1,
...loc.start,
};
packages/babel-code-frame/src/common.ts:117
- In the single-line range case,
markerLines[startLine] = [startColumn, endColumn - startColumn]will produceNaN(and later break marker rendering) if eitherstartColumnorendColumnisnull/undefined. Since this PR explicitly introducesnullas a valid sentinel for missing columns, it would be safer to guard here (e.g., treat missing columns as “no column”/whole-line highlight, or normalize missingstartColumnto0whenendColumnis provided).
if (startColumn === endColumn) {
if (startColumn != null) {
markerLines[startLine] = [startColumn, 0];
} else {
markerLines[startLine] = true;
}
} else {
markerLines[startLine] = [startColumn, endColumn - startColumn];
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…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.
36bb3f0 to
455553e
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 7 out of 7 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
packages/babel-code-frame/src/common.ts:66
getMarkerLinesnow relies oncolumn: nullas the sentinel to represent “no column” (and the tests also passcolumn: null), but the TypeScriptLocation/NodeLocationtypes still declarecolumn: number, forcing@ts-expect-errorhere. This leaks into the publiccodeFrameColumnssignature and makes it hard for TS consumers to express “no column” orcolumn: nullwithout casts. Consider updating the types (e.g.,column?: number | null) so the implementation and public API are type-safe and you can drop the@ts-expect-errordefaults.
const startLoc: Location = {
// @ts-expect-error default value
column: null,
// @ts-expect-error default value
line: -1,
...loc.start,
};
const endLoc: Location = {
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Thanks for the review. Docs PR opened: babel/website#3200 |
Updates the codeFrameColumns examples to use 0-based column values matching the API change in babel/babel#17849, and adds a Babel 7 to Babel 8 migration note.
Add @babel/code-frame entry under v8-migration-api.md for the 0-based column change in babel/babel#17849.
Add @babel/code-frame entry under v8-migration-api.md for the 0-based column change in babel/babel#17849.
Add @babel/code-frame entry under v8-migration-api.md for the 0-based column change in babel/babel#17849.
|
@SimenB This should be the last breaking change that can affect Jest |
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)