Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Diagnostic Line Number #112

Closed
suptejas opened this issue Jul 4, 2021 · 15 comments
Closed

Get Diagnostic Line Number #112

suptejas opened this issue Jul 4, 2021 · 15 comments

Comments

@suptejas
Copy link

suptejas commented Jul 4, 2021

So I was wondering if there way any way to get the line number of the error (which line the error occured in).

Nodejs shows you the error like so:
image

So i was wondering if there was any field in Diagnostic that gives you the line number.

@Stupremee
Copy link
Member

Stupremee commented Jul 4, 2021

The structure of a Diagnostic is really different. Not just a message, line combination. It consists of multiple SubDiagnostics, which are each rendered separately to allow such nice error messages. The best way to get the line is using the primary field

@suptejas
Copy link
Author

suptejas commented Jul 4, 2021

@Stupremee so what field should I access? primary.?

@suptejas
Copy link
Author

suptejas commented Jul 4, 2021

Or do I have to manually calculate line number?

@Stupremee
Copy link
Member

Yes most likely. Depending on the suggestion and what line number do you want.

@Stupremee
Copy link
Member

If you see an error message with an error, and help / info stuff, then there are multiple child diagnostics

@suptejas
Copy link
Author

suptejas commented Jul 4, 2021

@Stupremee isn't this an essential diagnostic field however? Wouldn't you think it's hard to manually calculate?

@suptejas
Copy link
Author

suptejas commented Jul 4, 2021

Also - not to forget, it's expensive to do this every time you need a line number - it's so much easier if we could get a line_no field so that it's less expensive

@Stupremee
Copy link
Member

Stupremee commented Jul 4, 2021

You don't need to calculate it manually. diag.primary.unwrap().span

Wait nvm. Yea you need to calculate the line number

@suptejas
Copy link
Author

suptejas commented Jul 4, 2021

Yes, that gives me character range, not line number though 😅

@suptejas
Copy link
Author

suptejas commented Jul 4, 2021

Also 1 more question - the primary error message i get for the first post here is correct (something along the lines of expected )), however it's not able to suggest fixes

@Stupremee
Copy link
Member

Stupremee commented Jul 4, 2021

You can take a look into rslint_errors. It's not that expensive.
First, you collect all line starts using rslint_errors::line_starts and then you can do a simple binary search on the returned list:

line_starts
  .binary_search(&byte_index)
  .unwrap_or_else(|next_line| next_line - 1),

You can even do this automatically using rslint_error::SimpleFile and the Files trait

@Stupremee
Copy link
Member

Stupremee commented Jul 4, 2021

Also 1 more question - the primary error message i get for the first post here is correct (something along the lines of expected )), however it's not able to suggest fixes

IIRC we don't suggest any fixes for any parsing errors. Might be a good addition, but is also very hard to do correctly

@suptejas
Copy link
Author

suptejas commented Jul 4, 2021

agreed

@Stupremee
Copy link
Member

Do you have discord or something else? Using github issues for stuff like this is annoying 😅

@suptejas
Copy link
Author

suptejas commented Jul 4, 2021

Yes - XtremeDevX#0007

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants