Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Smarter ranges #43

Merged
merged 4 commits into from
Nov 20, 2016
Merged

Smarter ranges #43

merged 4 commits into from
Nov 20, 2016

Conversation

jonathanrdelgado
Copy link
Contributor

Grabbed the following features from linter-eslint, here. Prior to this PR, this plugin did not have linter underline support; adding the ability to assume ranges when eslint doesn't provide a range fixes that. Additionally, if there is an endColumn and endLine present, it will use that instead of assuming.

  • Add support for endColumn and endLine
  • Add a smart fallback to assume a range
  • Add a dependency to atom-linter for range assumption

Resolves #28

// We want msgCol to remain undefined if it was initially so
// `rangeFromLineNumber` will give us a range over the entire line
const msgCol = typeof x.column === 'undefined' ? x.column : x.column - 1;
range = rangeFromLineNumber(textEditor, msgLine, msgCol);

Choose a reason for hiding this comment

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

Note that if an invalid point is provided this will throw an error, you may want to catch and handle this somehow.

ESLint has had a variety of issues with rules in the past, many fixed due to this catching the bugs and users reporting them 😛.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I may be wrong, but I thought Atom has a general try/catch for each package that will result in an error notification and the ability for the user to submit the error to the package. If this is incorrect, please let me know and I will add some sugar over this; otherwise, I think loudly failing on range errors is better than silently failing as linter-estlint does. As you mentioned, those bugs should be brought to someones attention, after all.

Choose a reason for hiding this comment

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

linter-eslint catches the error and makes it more presentable (showing rule name, originally requested position, and message), Atom will simply show what rangeFromLineNumber throws, which is admittedly somewhat cryptic to users:

Column start (x) greater than line length (y) for line z

As a note, see here for my current plans on making this less of a pain for users, I just haven't had time to implement it yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, gotcha. Yeah, i'll throw in some formatting, can't hurt.

range = rangeFromLineNumber(textEditor, msgLine, msgCol);
}
} catch (err) {
throw new Error(`Cannot mark location in editor for (${x.ruleId}) - (${x.message}) at line (${x.line}) column (${x.column})`);
Copy link
Owner

Choose a reason for hiding this comment

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

This is not very actionable. It would be helpful if it would point out that it's an issue with one of the rules and what the users should do (report it to the specific rule).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This would only be throw if there is a problem here, not with a rule. If that function is called, we have determined eslint did not provide endColumn / endLine and we are assuming a range so we can have some type of underline support.

Edit: Also, since this isn't immediately obvious, I just grabbed that error message from linter-eslint - I can make whatever modification you want in order to get this merged, though.

Choose a reason for hiding this comment

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

@jonathandelgado What @sindresorhus means is that rangeFromLineNumber throws when it is fed an invalid point, the only way it should be throwing is if an ESLint rule has a bug in it. A great example of this is how no-multiple-empty-lines was broken and returning the wrong point between versions v1.8.0 and v3.4.0. It was fixed in v3.5.0 (due to getting reported in linter-eslint).

The problem with a broken rule like that throwing is that it happens on every edit for on the fly linters, hence why we plan to make it less intrusive. I like your idea of pointing users to where to report it as well @sindresorhus!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, gotcha, sorry for the misunderstanding. Any suggestions on that implementation? Not sure how to redirect Atom errors to anything other than the package that threw it, in respect to the automatic reporting feature.

Choose a reason for hiding this comment

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

I would imagine something along the lines of "This is likely an issue with ESLint, please file an issue there", but @sindresorhus would be the one to answer what he wants here.

You've already "redirected" the error by catching it, you just need to change what happens with it from there. My plan is to add a fake "message" to linter with the relevant text.

// taken from linter-eslint
let range;
const msgLine = x.line - 1;
try {
Copy link
Owner

Choose a reason for hiding this comment

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

The try/catch should be around the rangeFromLineNumber call. No point in try/catching more than necessary.

@sindresorhus sindresorhus changed the title Smarter Ranges Smarter ranges Nov 2, 2016
@sindresorhus
Copy link
Owner

@jonathandelgado Mind updating to what @Arcanemagus suggested in #43 (comment) ?

@jonathanrdelgado
Copy link
Contributor Author

@sindresorhus Sorry for the delay, just one of those weeks. Does this look good to you?

@sindresorhus sindresorhus merged commit 001abcd into sindresorhus:master Nov 20, 2016
@sindresorhus
Copy link
Owner

Thank you :)

sindresorhus pushed a commit that referenced this pull request Nov 20, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use the new ESLint endLine and endColumn properties for better error highlighting
3 participants