feat: Allow specifying startLine in code frame#17070
feat: Allow specifying startLine in code frame#17070nicolo-ribaudo merged 5 commits intobabel:mainfrom
startLine in code frame#17070Conversation
liuxingbaoyu
commented
Jan 20, 2025
| 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 |
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/60729 |
| } | ||
|
|
||
| const { loc, missingPlugin } = err; | ||
| if (loc) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
That would display a start line and start column that would be confusing for the user.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Yes, I'm fine with both approaches.
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
Sounds good, I'll get it done.
d218f20 to
77e677a
Compare
|
commit: |
Did we end up doing this? We have a bunch of "new feature" PRs, they could go in a last Babel 7 minor. |
|
It looks like we've added |
77e677a to
4f2a995
Compare
| /** The number of lines to show below the error. default: 3 */ | ||
| linesBelow?: number; | ||
|
|
||
| startLine?: number; |
There was a problem hiding this comment.
This should go in a minor, right?
There was a problem hiding this comment.
I think we have enough stuff to do a last Babel 7 minor soon :)
startLinestartLine in code frame
| ): string { | ||
| const shouldHighlight = | ||
| opts.forceColor || (isColorSupported() && opts.highlightCode); | ||
| const startLineBaseZero = (opts.startLine || 1) - 1; |
There was a problem hiding this comment.
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.