Skip to content

feat: Allow specifying startLine in code frame#17070

Merged
nicolo-ribaudo merged 5 commits intobabel:mainfrom
liuxingbaoyu:fix-17069
Jan 29, 2026
Merged

feat: Allow specifying startLine in code frame#17070
nicolo-ribaudo merged 5 commits intobabel:mainfrom
liuxingbaoyu:fix-17069

Conversation

@liuxingbaoyu
Copy link
Member

Q                       A
Fixed Issues? Fixes #17069
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

@liuxingbaoyu liuxingbaoyu added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: core labels Jan 20, 2025
@babel-bot
Copy link
Collaborator

babel-bot commented Jan 20, 2025

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/60729

}

const { loc, missingPlugin } = err;
if (loc) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we apply the offset to the error location here? So that we don't have to copy the input code, which could be huge.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would display a start line and start column that would be confusing for the user.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood, in that case can we pass the startLine / startColumn info to babel-code-frame and let it adjust its line markers? It think this approach should be more efficient than padding the input because the startLine / startColumn can be arbitrarily large.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I remember, the reason we recently also introduced .startIndex is because the workaround of adding all the whitespace at the beginning can be very expensive on large files.

However, this shouldn't be too bad, because we are only adding one characcter per line (\n) rather than re-filling all the previous characters. And the + code likely doesn't actually copy the string, since V8 represents strings as trees/ropes of smaller strings? Unless then we do something that causes the string to be "flattened".

Also, this branch is only running in the error case, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'm fine with both approaches.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liuxingbaoyu What about adding startLine/startColumn to code-frame with a PR for the upcoming minor release? Or maybe just startLine, since startColumn's behavior would get weird when there are multiple lines.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I'll get it done.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Nov 28, 2025

Open in StackBlitz

commit: 991ce2a

@nicolo-ribaudo
Copy link
Member

@liuxingbaoyu What about adding startLine/startColumn to code-frame with a PR for the upcoming minor release? Or maybe just startLine, since startColumn's behavior would get weird when there are multiple lines.

Did we end up doing this? We have a bunch of "new feature" PRs, they could go in a last Babel 7 minor.

@liuxingbaoyu
Copy link
Member Author

It looks like we've added startLine.

/** The number of lines to show below the error. default: 3 */
linesBelow?: number;

startLine?: number;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should go in a minor, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, yes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have enough stuff to do a last Babel 7 minor soon :)

@nicolo-ribaudo nicolo-ribaudo added this to the v7.29.0 milestone Jan 23, 2026
@nicolo-ribaudo nicolo-ribaudo changed the title fix: show correct code frame for startLine feat: Allow specifying startLine in code frame Jan 27, 2026
@nicolo-ribaudo nicolo-ribaudo added PR: New Feature (next major) 🚀 A type of pull request used for our changelog categories for next major release 7.x: needs backport and removed PR: Bug Fix 🐛 A type of pull request used for our changelog categories labels Jan 27, 2026
): string {
const shouldHighlight =
opts.forceColor || (isColorSupported() && opts.highlightCode);
const startLineBaseZero = (opts.startLine || 1) - 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I pass startLine: 0 in the options, I get this to be startLineBaseZero: 0. I think it's good, as the first line is line 1 so 0 is likely a mistake that meant "first line", but I wanted to point it out.

@nicolo-ribaudo nicolo-ribaudo merged commit 5b71ddf into babel:main Jan 29, 2026
54 checks passed
@nicolo-ribaudo nicolo-ribaudo added the PR: New Feature 🚀 A type of pull request used for our changelog categories label Mar 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

7.x: needs backport pkg: core PR: New Feature (next major) 🚀 A type of pull request used for our changelog categories for next major release PR: New Feature 🚀 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: message is wrong with parserOpts.startLine

4 participants