Skip to content

Commit

Permalink
Merge pull request #102 from AtomLinter/istexteditor
Browse files Browse the repository at this point in the history
Use the Atom API to validate TextEditor
  • Loading branch information
steelbrain committed Feb 3, 2016
2 parents 3ae4cab + 8208b67 commit 366a5d3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
17 changes: 14 additions & 3 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ function _validateFind(directory, name) {
}
}

function _validateEditor(editor) {
let isEditor;
if (typeof atom.workspace.isTextEditor === 'function') {
// Added in Atom v1.4.0
isEditor = atom.workspace.isTextEditor(editor);
} else {
isEditor = typeof editor.getText !== 'function';
}
if (!isEditor) {
throw new Error('Invalid TextEditor provided');
}
}

function exec(command) {
let args = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1];
let options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
Expand All @@ -148,9 +161,7 @@ function execNode(command) {
}

function rangeFromLineNumber(textEditor, line, column) {
if (typeof textEditor.getText !== 'function') {
throw new Error('Invalid textEditor provided');
}
_validateEditor(textEditor);
let lineNumber = line;

if (!Number.isFinite(lineNumber) || Number.isNaN(lineNumber) || lineNumber < 0) {
Expand Down
17 changes: 14 additions & 3 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ function _validateFind(directory, name) {
}
}

function _validateEditor(editor) {
let isEditor
if (typeof atom.workspace.isTextEditor === 'function') {
// Added in Atom v1.4.0
isEditor = atom.workspace.isTextEditor(editor)
} else {
isEditor = typeof editor.getText !== 'function'
}
if (!isEditor) {
throw new Error('Invalid TextEditor provided')
}
}

export function exec(command, args = [], options = {}) {
_validateExec(command, args, options)
return _exec(command, args, options, false)
Expand All @@ -117,9 +130,7 @@ export function execNode(command, args = [], options = {}) {
}

export function rangeFromLineNumber(textEditor, line, column) {
if (typeof textEditor.getText !== 'function') {
throw new Error('Invalid textEditor provided')
}
_validateEditor(textEditor)
let lineNumber = line

if (!Number.isFinite(lineNumber) || Number.isNaN(lineNumber) || lineNumber < 0) {
Expand Down

0 comments on commit 366a5d3

Please sign in to comment.