Skip to content

Commit

Permalink
Fix AtomLinter#44, AtomLinter#51, others? Use provider API
Browse files Browse the repository at this point in the history
  • Loading branch information
caseywebdev committed Jul 25, 2015
1 parent 7c48607 commit d5ed9a1
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 39 deletions.
10 changes: 10 additions & 0 deletions lib/find-file.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{exists} = require 'fs'
{join, resolve} = require 'path'

module.exports = (dir, file, cb) ->
absolute = join dir, file
exists absolute, (doesExist) ->
return cb absolute if doesExist
parent = resolve dir, '..'
return cb() if dir is parent
module.exports parent, file, cb
6 changes: 6 additions & 0 deletions lib/hash.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{createHash} = require 'crypto'

module.exports = (buffer) ->
hash = createHash 'md5'
hash.end buffer
hash.read().toString 'hex'
26 changes: 23 additions & 3 deletions lib/init.coffee
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
lint = require './lint'
{CompositeDisposable} = require 'atom'

module.exports =
config:
executablePath:
title: 'Rubocop Executable Path'
description: 'The path where rubocop is located'
type: 'string'
title: 'Executable Path'
default: 'rubocop'
additionalArguments:
title: 'Additional Arguments'
type: 'string'
default: ''

activate: ->
console.log 'activate linter-rubocop'
@subscriptions = new CompositeDisposable
@subscriptions.add atom.config.observe 'linter-rubocop.additionalArguments',
(args) => @additionalArguments = if args then args.split ' ' else []
@subscriptions.add atom.config.observe 'linter-rubocop.executablePath',
(executablePath) => @executablePath = executablePath

deactivate: ->
@subscriptions.dispose()

provideLinter: ->
provider =
grammarScopes: ['source.ruby', 'source.ruby.rails', 'source.ruby.rspec']
scope: 'file'
lintOnFly: true
lint: (editor) => lint editor, @executablePath, @additionalArguments
49 changes: 49 additions & 0 deletions lib/lint.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
findFile = require './find-file'
hash = require './hash'
{BufferedProcess} = require 'atom'
{join} = require 'path'
{tmpdir} = require 'os'
{unlink, writeFile} = require 'fs'

module.exports = (editor, command, args) ->
filePath = editor.getPath()
rails = editor.getGrammar().scopeName is 'source.ruby.rails'
tmpPath = join tmpdir(), hash filePath
out = ''

appendToOut = (data) -> out += data
getConfig = (cb) -> findFile filePath, '.rubocop.yml', cb
writeTmp = (cb) -> writeFile tmpPath, editor.getText(), cb
cleanup = (cb) -> unlink tmpPath, cb

new Promise (resolve, reject) -> getConfig (config) -> writeTmp (er) ->
return reject er if er
new BufferedProcess
command: command
args: [
'-f'
'json'
(if config then ['-c', config] else [])...
(if rails then ['-R'] else [])...
args...
tmpPath
]
stdout: appendToOut
stderr: appendToOut
exit: -> cleanup ->
try {offenses} = JSON.parse(out).files[0]
return reject new Error out unless offenses
resolve offenses.map (offense) ->
line = (offense.location?.line or 1) - 1
col = (offense.location?.column or 1) - 1
type:
switch offense.severity
when 'refactor', 'convention', 'warning' then 'warning'
else 'error'
text: (offense.message or 'Unknown Error') +
(if offense.cop_name then " (#{offense.cop_name})" else ''),
filePath: filePath,
range: [
[line, col],
[line, col + (offense.location?.length or 0)]
]
34 changes: 0 additions & 34 deletions lib/linter-rubocop.coffee

This file was deleted.

10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "linter-rubocop",
"linter-package": true,
"activationCommands": [],
"main": "./lib/init",
"version": "0.2.7",
Expand All @@ -10,5 +9,12 @@
"engines": {
"atom": ">0.50.0"
},
"dependencies": {}
"dependencies": {},
"providedServices": {
"linter": {
"versions": {
"1.0.0": "provideLinter"
}
}
}
}

0 comments on commit d5ed9a1

Please sign in to comment.