From b616e09b784610a402386f61ccc461670e0dceb9 Mon Sep 17 00:00:00 2001 From: Landon Abney Date: Fri, 16 Sep 2016 15:48:20 -0700 Subject: [PATCH] Add support for endLine and endColumn 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`. --- lib/helpers.js | 9 +++++++++ lib/main.js | 12 +++++++++++- src/helpers.js | 8 ++++++++ src/main.js | 18 +++++++++++++----- 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/lib/helpers.js b/lib/helpers.js index 6cb50a6d..39aec3ad 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -7,6 +7,7 @@ Object.defineProperty(exports, "__esModule", { exports.spawnWorker = spawnWorker; exports.showError = showError; exports.idsToIgnoredRules = idsToIgnoredRules; +exports.validatePoint = validatePoint; var _child_process = require('child_process'); @@ -74,3 +75,11 @@ function idsToIgnoredRules() { return ids; }, {}); } + +function validatePoint(textEditor, line, col) { + var 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!'); + } +} diff --git a/lib/main.js b/lib/main.js index 4d047634..d2e9e80d 100644 --- a/lib/main.js +++ b/lib/main.js @@ -167,6 +167,8 @@ module.exports = { var ruleId = _ref.ruleId; var column = _ref.column; var fix = _ref.fix; + var endLine = _ref.endLine; + var endColumn = _ref.endColumn; var textBuffer = textEditor.getBuffer(); var linterFix = null; @@ -178,8 +180,16 @@ module.exports = { }; } var range = void 0; + var msgLine = line - 1; + var msgCol = column ? column - 1 : column; try { - range = Helpers.rangeFromLineNumber(textEditor, line - 1, column ? column - 1 : column); + if (endColumn && endLine) { + (0, _helpers.validatePoint)(textEditor, msgLine, msgCol); + (0, _helpers.validatePoint)(textEditor, endLine - 1, endColumn - 1); + range = [[msgLine, msgCol], [endLine - 1, endColumn - 1]]; + } else { + range = Helpers.rangeFromLineNumber(textEditor, msgLine, msgCol); + } } catch (err) { throw new Error('Cannot mark location in editor for (' + ruleId + ') - (' + message + ')' + (' at line (' + line + ') column (' + column + ')')); } diff --git a/src/helpers.js b/src/helpers.js index c6e26ec7..c3e89b50 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -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!`) + } +} diff --git a/src/main.js b/src/main.js index bdee035d..ec0bb64d 100644 --- a/src/main.js +++ b/src/main.js @@ -6,7 +6,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 = [] @@ -150,7 +150,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) { @@ -164,10 +166,16 @@ module.exports = { } } let range + const msgLine = line - 1 + const msgCol = column ? column - 1 : column try { - range = Helpers.rangeFromLineNumber( - textEditor, line - 1, column ? column - 1 : column - ) + if (endColumn && endLine) { + validatePoint(textEditor, msgLine, msgCol) + validatePoint(textEditor, endLine - 1, endColumn - 1) + range = [[msgLine, msgCol], [endLine - 1, endColumn - 1]] + } else { + range = Helpers.rangeFromLineNumber(textEditor, msgLine, msgCol) + } } catch (err) { throw new Error( `Cannot mark location in editor for (${ruleId}) - (${message})` +