eslint: Vscode and atom highlight locations are unexpected when end location is not provided
Summary Many rules reports only start location and no end location. For these rules, editors often renders tildes in unexpected places.
Examples
In these examples, the rules only reports start location but no end locations. Instead of highlighting tokens like \n
, (
, or {
, the token before the said token is highlighted.
/* eslint semi: [2, 'always'] */
const a = Math.random
// ~~~~~~ actual highlight in vscode
// ~ expected highlight
// ^ reported start location
/* eslint object-curly-spacing: [2, 'always'] */
const b = {b: Math.random}
// ~~~~~~ actual highlight in vscode
// ~ expected highlight
// ^ reported start location
/* eslint space-in-parens: [2, 'never'] */
function ffff( a ) {}
// ~~~~ actual highlight in vscode
// ~ expected highlight
// ^ reported start location
There are many other rules suffering from this problem, most of them are named *space*
or *spacing*
.
How to fix The problem disappear if the both start location and end location are reported and start location is different to end location. That is, the location range is not zero-width-ed.
There are several places this can be done:
-
Modify each rules to report end location. Pros: explicit. Cons: lots of place needs change.
-
Modify ESLint core, automatically add end location when end location is missing. Cons: not explicit.
-
Modify vscode-eslint plugin to automatically add end location when end location is missing.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 3
- Comments: 28 (27 by maintainers)
Links to this issue
Commits related to this issue
- Fix: add end location to report in no-extra-bind (refs #12334) — committed to eslint/eslint by mdjermanovic 4 years ago
- Fix: getNameLocationInGlobalDirectiveComment end location (refs #12334) — committed to eslint/eslint by mdjermanovic 4 years ago
- Fix: add end location to report in no-prototype-builtins (refs #12334) — committed to eslint/eslint by mdjermanovic 4 years ago
- Fix: add end location to report in no-prototype-builtins (refs #12334) (#13087) — committed to eslint/eslint by mdjermanovic 4 years ago
- Update: report operator location in operator-linebreak (refs #12334) — committed to eslint/eslint by mdjermanovic 4 years ago
- Update: report operator location in operator-linebreak (refs #12334) (#13102) — committed to eslint/eslint by mdjermanovic 4 years ago
- Fix: add end location to report in no-extra-bind (refs #12334) (#13083) — committed to eslint/eslint by mdjermanovic 4 years ago
- Update: Improve report location for array-callback-return (refs #12334) — committed to eslint/eslint by mdjermanovic 4 years ago
- Fix: getNameLocationInGlobalDirectiveComment end location (refs #12334) (#13086) — committed to eslint/eslint by mdjermanovic 4 years ago
- Fix: add end location to report in no-useless-concat (refs #12334) — committed to eslint/eslint by mdjermanovic 4 years ago
- Update: Improve report location newline-per-chained-call (refs #12334) — committed to eslint/eslint by mdjermanovic 4 years ago
- Update: Improve report location for no-empty-function (refs #12334) — committed to eslint/eslint by mdjermanovic 4 years ago
- Update: Improve report location for no-empty-function (refs #12334) (#13121) — committed to eslint/eslint by mdjermanovic 4 years ago
- Update: Improve report location for comma-style (refs #12334) (#13111) — committed to eslint/eslint by golopot 4 years ago
- Fix: add end location to report in no-useless-concat (refs #12334) (#13110) — committed to eslint/eslint by mdjermanovic 4 years ago
- Update: Improve report location for new-cap (refs #12334) — committed to eslint/eslint by mdjermanovic 4 years ago
- Update: Improve report location newline-per-chained-call (refs #12334) (#13116) * Update: Improve report location newline-per-chained-call (refs #12334) * Set loc.end to callee.loc.end — committed to eslint/eslint by mdjermanovic 4 years ago
- Update: report backtick loc in no-unexpected-multiline (refs #12334) — committed to eslint/eslint by mdjermanovic 4 years ago
- Update: Improve report location for new-cap (refs #12334) (#13136) — committed to eslint/eslint by mdjermanovic 4 years ago
- Update: Improve report location for getter-return (refs #12334) — committed to eslint/eslint by mdjermanovic 4 years ago
This is a list of rules that reports start column but misses end column. The list is acquired by patching rule tester in a dirty way.
This is all done 🎉 .
Awesome! Thanks to everyone who helped push through this. 🙏
@fisker I’d suggest opening a separate issue to discuss your idea.
I think for the harder cases we can either make a best guess at a change or just leave it as-is. We’ve already made so many fixes that I think we can be forgiven if a few aren’t quite as accurate.
unicode-bom
andeol-last
in particular, dealing with invisible characters, is something that is probably not going to make much sense so maybe just set the end location to one character after the start location?The remaining four rules may be problematic:
unicode-bom
- currently reports the very first location in the source code as the error’s start loc. What could be the end loc?consistent-return
(partially) - there’s a similar problem as withunicode-bom
, in the case where there’s missing “global” return at the end of the program. Other cases can be fixed.eol-last
- currently reports the very last location in the source code as the error’s start loc. There are no valid locations after that location, so what could be the end loc?semi
(partially) - there’s a similar problem as witheol-last
, in the case where the missing semi should be inserted after the EOF (example in this comment). All other cases are already fixed.@anikethsaha updated, thanks!
I will champion this. Given @btmills’s comment here, I’m going to mark this as accepted. @golopot Thanks for spearheading this!
@btmills I think that makes the most sense? Ideally, it would be nice to audit the rules and make a list we can keep track of here.
I have tested that case, report the location of the linefeed (with the end location at the next line) will do well in vscode.