diff --git a/index.coffee b/index.coffee new file mode 100644 index 00000000..470e464c --- /dev/null +++ b/index.coffee @@ -0,0 +1,80 @@ +{BufferedProcess, CompositeDisposable} = require 'atom' +{exists, unlink, writeFile} = require 'fs' +{join, resolve} = 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() + tmpPath = join tmpdir(), randomBytes(32).toString 'hex' + 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 [])... + args... + tmpPath + ] + stdout: appendToOut + stderr: appendToOut + exit: -> cleanup -> + try {offenses: errors} = JSON.parse(out).files[0] + return reject new Error out 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]] + +module.exports = + config: + executablePath: + type: 'string' + title: 'Executable Path' + default: 'rubocop' + additionalArguments: + title: 'Additional Arguments' + type: 'string' + default: '' + + activate: -> + prefix = 'linter-rubocop.' + @subscriptions = new CompositeDisposable + @subscriptions.add atom.config.observe "#{prefix}executablePath", + (executablePath) => @executablePath = executablePath + @subscriptions.add atom.config.observe "#{prefix}additionalArguments", + (args) => @additionalArguments = if args then args.split ' ' else [] + + deactivate: -> + @subscriptions.dispose() + + provideLinter: -> + provider = + grammarScopes: ['source.ruby', 'source.ruby.rails', 'source.ruby.rspec'], + scope: 'file' + lintOnFly: true + lint: (editor) => lint editor, @executablePath, @additionalArguments diff --git a/lib/init.coffee b/lib/init.coffee deleted file mode 100644 index 8918b491..00000000 --- a/lib/init.coffee +++ /dev/null @@ -1,10 +0,0 @@ -module.exports = - config: - executablePath: - title: 'Rubocop Executable Path' - description: 'The path where rubocop is located' - type: 'string' - default: '' - - activate: -> - console.log 'activate linter-rubocop' diff --git a/lib/linter-rubocop.coffee b/lib/linter-rubocop.coffee deleted file mode 100644 index 1fdf858c..00000000 --- a/lib/linter-rubocop.coffee +++ /dev/null @@ -1,34 +0,0 @@ -linterPath = atom.packages.getLoadedPackage("linter").path -Linter = require "#{linterPath}/lib/linter" -findFile = require "#{linterPath}/lib/util" - -class LinterRubocop extends Linter - # The syntax that the linter handles. May be a string or - # list/tuple of strings. Names should be all lowercase. - @syntax: ['source.ruby', 'source.ruby.rails', 'source.ruby.rspec'] - - # A string, list, tuple or callable that returns a string, list or tuple, - # containing the command line (with arguments) used to lint. - cmd: 'rubocop --force-exclusion --format emacs' - - linterName: 'rubocop' - - # A regex pattern used to extract information from the executable's output. - regex: - '.+?:(?\\d+):(?\\d+): ' + - '((?[RCW])|(?[EF])): ' + - '(?.+)' - - options: ['executablePath'] - - constructor: (editor)-> - super(editor) - - if editor.getGrammar().scopeName == 'source.ruby.rails' - @cmd += " --rails" - - config = findFile(@cwd, '.rubocop.yml') - if config - @cmd += " --config #{config}" - -module.exports = LinterRubocop diff --git a/package.json b/package.json index 5cfe4951..542e3eec 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,6 @@ { "name": "linter-rubocop", - "linter-package": true, "activationCommands": [], - "main": "./lib/init", "version": "0.2.7", "description": "Lint `Ruby` on the fly, using rubocop", "repository": "https://github.com/AtomLinter/linter-rubocop", @@ -10,5 +8,11 @@ "engines": { "atom": ">0.50.0" }, - "dependencies": {} + "providedServices": { + "linter": { + "versions": { + "1.1.0": "provideLinter" + } + } + } }