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

Add support for endLine and endColumn #709

Merged
merged 3 commits into from
Oct 10, 2016
Merged

Conversation

Arcanemagus
Copy link
Member

@Arcanemagus Arcanemagus commented Sep 21, 2016

In eslint/eslint#6640, released in eslint@3.1.0, ESLint added the ability for a rule to return a full range instead of just a single point. This PR adds support for that.

Merging of this is currently blocked however as no core rule currently actually returns a range (eslint/eslint#3307), so we have nothing to validate against.

@Arcanemagus
Copy link
Member Author

Arcanemagus commented Sep 21, 2016

Note: This is currently based on the branch for #692 and should be rebased on master once that gets merged.

Rebased.

@@ -61,3 +62,11 @@ function showError(givenMessage) {
dismissable: true
});
}

function validatePoint(textEditor, line, col) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Should this be validateRange and do similar logic but just use TextBuffer::clipRange? It would save calling it twice...

@Arcanemagus
Copy link
Member Author

Looks like no-unreachable supports this format, writing a spec now.

@@ -164,10 +166,16 @@ module.exports = {
}
}
let range
const msgLine = line - 1
const msgCol = column ? column - 1 : column
Copy link
Member

Choose a reason for hiding this comment

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

This works fine, but it might be nice to be a little more explicit with something like:

const msgCol = column > 0 ? column - 1 : 0

or even better (I think):

const msgCol = Math.max(0, column - 1)

Copy link
Member Author

Choose a reason for hiding this comment

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

Ooo, I like that idea.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, after looking at the code the reason I was passing it on directly if it was "falsey" was that rangeFromLineNumber will ignore it if it is undefined or null... meaning it will give us the whole line highlight that we want if there is only a line specified.

I'll make that clearer in the code though.

if (endColumn && endLine) {
validatePoint(textEditor, msgLine, msgCol)
validatePoint(textEditor, endLine - 1, endColumn - 1)
range = [[msgLine, msgCol], [endLine - 1, endColumn - 1]]
Copy link
Member

Choose a reason for hiding this comment

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

Question: why are you subtracting 1 from the endColumn? I understand subtracting for endLine, since it is 1-based, but endColumn is 0-based.

Copy link
Member Author

Choose a reason for hiding this comment

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

I assumed endColumn is 1-based like all the other points we get from ESLint... did they really make 1/4 of the points 0-based?!?

Copy link
Member

Choose a reason for hiding this comment

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

column numbers are 0-based, line numbers are 1-based.

Copy link
Member Author

Choose a reason for hiding this comment

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

Looks like rules have mixed 1-based and 0-based coords, but the results are all 1-based.

range = Helpers.rangeFromLineNumber(
textEditor, line - 1, column ? column - 1 : column
)
if (endColumn && endLine) {
Copy link
Member

Choose a reason for hiding this comment

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

Are we sure that a valid endColumn won't be 0? If we get an endColumn of zero, and an endLine of, say, 5, it seems like we should still use them. But I think this check will return false, right? Perhaps a safer thing to do is check typeof endColumn !== 'undefined'?

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's what I get for falling in with the lazy crowd that abuses that hidden check, thanks!

const buffer = textEditor.getBuffer()
// Clip the given point to a valid one, and check if it equals the original
if (!buffer.clipPosition([line, col]).isEqual([line, col])) {
throw new Error(`${line}:${col} isn't a valid point!`)
Copy link
Member

Choose a reason for hiding this comment

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

I'm not convinced that throwing an error is the right thing to do here. We could be more graceful and just use the clipped position. I suppose the positive here is that we might be able to catch ESLint errors, whereas without throwing an error people will just live with the possibly slightly-inaccurate location marking. Pros and cons either way, but personally I think I lean towards not throwing an error. Curious to hear your thoughts.

Copy link
Member Author

@Arcanemagus Arcanemagus Oct 10, 2016

Choose a reason for hiding this comment

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

This should be validating the point in a similar manner to how rangeFromLineNumber() does. Where this is being called over here is wrapped in the same try/catch that the call to rangFromLineNumber() is, so this throw will be caught and handled in the exact same manner as any other invalid point we run into.

Add support for creating a full range from an ESLint message if it
specifies an `endLine` and `endColumn`. Also adds a `validatePoint` helper
function to check that these coordinates given from ESLint are valid since
we no longer have the checking built into `helpers.rangeFromLineNumber`.
Properly check whether the positions are undefined, instead of just "falsey".
Also updates compiled code, since `babel` changed it's mind again.
Copy link
Member

@IanVS IanVS left a comment

Choose a reason for hiding this comment

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

LGTM!

@Arcanemagus Arcanemagus merged commit fcc3863 into master Oct 10, 2016
@Arcanemagus Arcanemagus deleted the eslint-full-range branch October 10, 2016 20:20
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.

2 participants