-
Notifications
You must be signed in to change notification settings - Fork 141
Add support for endLine and endColumn #709
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
root: true, | ||
rules: { | ||
'no-unreachable': 'error' | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict'; | ||
|
||
function foo() { | ||
return 42; | ||
|
||
var a = 4; | ||
return a + 2; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,3 +56,11 @@ export function idsToIgnoredRules(ruleIds = []) { | |
return ids | ||
}, {}) | ||
} | ||
|
||
export function validatePoint(textEditor, line, col) { | ||
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!`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be validating the point in a similar manner to how |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ import ruleURI from 'eslint-rule-documentation' | |
// eslint-disable-next-line import/no-extraneous-dependencies, import/extensions | ||
import { CompositeDisposable, Range } from 'atom' | ||
|
||
import { spawnWorker, showError, idsToIgnoredRules } from './helpers' | ||
import { spawnWorker, showError, idsToIgnoredRules, validatePoint } from './helpers' | ||
|
||
// Configuration | ||
const scopes = [] | ||
|
@@ -65,6 +65,7 @@ module.exports = { | |
'linter-eslint:debug': () => { | ||
const textEditor = atom.workspace.getActiveTextEditor() | ||
const filePath = textEditor.getPath() | ||
// eslint-disable-next-line import/no-dynamic-require | ||
const linterEslintMeta = require(Path.join(atom.packages.resolvePackagePath('linter-eslint'), 'package.json')) | ||
const config = atom.config.get('linter-eslint') | ||
const configString = JSON.stringify(config, null, 2) | ||
|
@@ -77,6 +78,7 @@ module.exports = { | |
const detail = [ | ||
`atom version: ${atom.getVersion()}`, | ||
`linter-eslint version: ${linterEslintMeta.version}`, | ||
// eslint-disable-next-line import/no-dynamic-require | ||
`eslint version: ${require(Path.join(response.path, 'package.json')).version}`, | ||
`hours since last atom restart: ${Math.round(hoursSinceRestart * 10) / 10}`, | ||
`platform: ${process.platform}`, | ||
|
@@ -181,7 +183,9 @@ module.exports = { | |
*/ | ||
return null | ||
} | ||
return response.map(({ message, line, severity, ruleId, column, fix }) => { | ||
return response.map(({ | ||
message, line, severity, ruleId, column, fix, endLine, endColumn } | ||
) => { | ||
const textBuffer = textEditor.getBuffer() | ||
let linterFix = null | ||
if (fix) { | ||
|
@@ -195,10 +199,20 @@ module.exports = { | |
} | ||
} | ||
let range | ||
const msgLine = line - 1 | ||
try { | ||
range = Helpers.rangeFromLineNumber( | ||
textEditor, line - 1, column ? column - 1 : column | ||
) | ||
if (typeof endColumn !== 'undefined' && typeof endLine !== 'undefined') { | ||
// Here we always want the column to be a number | ||
const msgCol = Math.max(0, column - 1) | ||
validatePoint(textEditor, msgLine, msgCol) | ||
validatePoint(textEditor, endLine - 1, endColumn - 1) | ||
range = [[msgLine, msgCol], [endLine - 1, endColumn - 1]] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: why are you subtracting 1 from the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assumed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. column numbers are 0-based, line numbers are 1-based. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} else { | ||
// We want msgCol to remain undefined if it was initially so | ||
// `rangeFromLineNumber` will give us a range over the entire line | ||
const msgCol = typeof column !== 'undefined' ? column - 1 : column | ||
range = Helpers.rangeFromLineNumber(textEditor, msgLine, msgCol) | ||
} | ||
} catch (err) { | ||
throw new Error( | ||
`Cannot mark location in editor for (${ruleId}) - (${message})` + | ||
|
There was a problem hiding this comment.
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 useTextBuffer::clipRange
? It would save calling it twice...