Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Use stdin instead of temp file #72

Merged
merged 13 commits into from
Sep 25, 2015
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
npm-debug.log
node_modules
.idea
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# 0.4.0

### New Features

* Use STDIN for linting
[#72](https://github.com/AtomLinter/linter-rubocop/pull/72/files). This
version requires `rubocop >= 0.34.0`.

# 0.2.2

### Bugs fixed
Expand Down
109 changes: 35 additions & 74 deletions index.coffee
Original file line number Diff line number Diff line change
@@ -1,61 +1,37 @@
{BufferedProcess, CompositeDisposable} = require 'atom'
{exists, unlink, writeFile} = require 'fs'
{join, resolve, dirname} = require 'path'
{randomBytes} = require 'crypto'
{tmpdir} = require 'os'

findFile = (dir, file, cb) ->
absolute = join dir, file
exists absolute, (doesExist) ->
return cb absolute if doesExist
parent = resolve dir, '..'
return cb() if dir is parent
findFile parent, file, cb

lint = (editor, command, args) ->
filePath = editor.getPath()
fileDir = dirname(filePath)
tmpPath = join tmpdir(), randomBytes(32).toString 'hex'
out = ''
err = ''

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

new Promise (resolve, reject) -> getConfig (config) -> writeTmp (er) -> getCwd (cwd) ->
return reject er if er
new BufferedProcess
command: command[0]
args: [
command.slice(1)...
'-f'
'json'
(if config then ['-c', config] else [])...
args...
tmpPath
]
options:
cwd: cwd
stdout: appendToOut
stderr: appendToErr
exit: -> cleanup ->
try {offenses: errors} = JSON.parse(out).files[0]
return reject new Error "STDOUT:#{out}\nSTDERR:#{err}" unless errors
resolve errors.map (error) ->
{line, column, length} =
error.location || {line: 1, column: 1, length: 0}
type:
switch error.severity
when 'refactor', 'convention', 'warning' then 'warning'
else 'error'
text: (error.message or 'Unknown Error') +
(if error.cop_name then " (#{error.cop_name})" else ''),
filePath: filePath,
range: [[line - 1, column - 1], [line - 1, column + length - 1]]
helpers = require 'atom-linter'

COMMAND_CONFIG_KEY = 'linter-rubocop.executablePath'
ARGS_CONFIG_KEY = 'linter-rubocop.additionalArguments'
DEFAULT_LOCATION = {line: 1, column: 1, length: 0}
DEFAULT_ARGS = ['--cache', 'false', '--force-exclusion', '-f', 'json', '-s']
DEFAULT_MESSAGE = 'Unknown Error'
WARNINGS = new Set(['refactor', 'convention', 'warning'])

lint = (editor) ->
command = atom.config.get(COMMAND_CONFIG_KEY)
args = atom.config.get(ARGS_CONFIG_KEY).split(/\s+/).filter((i) -> i)
.concat(DEFAULT_ARGS, path = editor.getPath())
options = {stdin: editor.getText()}
helpers.exec(command, args, options).then (result) ->
(JSON.parse(result).files[0]?.offenses || []).map (offense) ->
{cop_name, location, message, severity} = offense
{line, column, length} = location || DEFAULT_LOCATION
type: if WARNINGS.has(severity) then 'Warning' else 'Error'
text: (message || DEFAULT_MESSAGE) +
(if cop_name then " (#{cop_name})" else '')
filePath: path
range: [[line - 1, column - 1], [line - 1, column + length - 1]]

linter =
grammarScopes: [
'source.ruby'
'source.ruby.rails'
'source.ruby.rspec'
'source.ruby.chef'
]
scope: 'file'
lintOnFly: true
lint: lint

module.exports =
config:
Expand All @@ -68,19 +44,4 @@ module.exports =
type: 'string'
default: ''

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

deactivate: ->
@subscriptions.dispose()

provideLinter: ->
grammarScopes: ['source.ruby', 'source.ruby.rails', 'source.ruby.rspec', 'source.ruby.chef'],
scope: 'file'
lintOnFly: true
lint: (editor) => lint editor, @executablePath, @additionalArguments
provideLinter: -> linter
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
"1.1.0": "provideLinter"
}
}
},
"dependencies": {
"atom-linter": "^3.2.0"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have this one which is at 1.5 https://github.com/atom-community/linter

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I get

Cannot find module 'atom-linter'

Error: Cannot find module 'atom-linter'
    at Module._resolveFilename (module.js:328:15)
    at Function.Module._resolveFilename (/Applications/Atom.app/Contents/Resources/app.asar/src/module-cache.js:383:52)
    at Function.Module._load (module.js:270:25)
    at Module.require (module.js:357:17)
    at require (module.js:376:17)
    at Object.module.exports.provideLinter (/Users/mrfoto/.atom/packages/linter-rubocop/index.coffee:27:15)
    at Package.module.exports.Package.activateServices (/Applications/Atom.app/Contents/Resources/app.asar/src/package.js:414:69)
    at Package.module.exports.Package.activateNow (/Applications/Atom.app/Contents/Resources/app.asar/src/package.js:246:16)
    at /Applications/Atom.app/Contents/Resources/app.asar/src/package.js:224:30
    at Package.module.exports.Package.measure (/Applications/Atom.app/Contents/Resources/app.asar/src/package.js:167:15)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, and you have the correct one. This is just an npm helper module we've made. You can find the repo at github.com/AtomLinter/atom-linter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will have to do npm install after you checkout this PR

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, got it now but it does not appear to run 😕

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

umm, does a console.log in the method get triggered?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, nada. I have several offenses on the file (run from the command line), but this does not appear to run. Even if I remove rubocop path I get no error, so maybe it's not hooked up to linter or something

}
}