diff --git a/package.json b/package.json index 691f44d..6900257 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,10 @@ { "name": "Ramón Cahenzli", "url": "https://github.com/psy-q" + }, + { + "name": "Clemens Damke", + "url": "https://github.com/Cortys" } ], "engines": { @@ -69,6 +73,7 @@ "extend": "^2.0.1", "handlebars": "^3.0.3", "js-beautify": "^1.5.6", + "jscs": "^1.13.1", "lodash": "3.9.3", "loophole": "^1.0.0", "node-dir": "^0.1.8", diff --git a/src/beautifiers/index.coffee b/src/beautifiers/index.coffee index 5d1219a..610c95a 100644 --- a/src/beautifiers/index.coffee +++ b/src/beautifiers/index.coffee @@ -42,6 +42,7 @@ module.exports = class Beautifiers extends EventEmitter 'gofmt' 'fortran-beautifier' 'js-beautify' + 'jscs' 'perltidy' 'php-cs-fixer' 'prettydiff' diff --git a/src/beautifiers/jscs.coffee b/src/beautifiers/jscs.coffee new file mode 100644 index 0000000..92ad828 --- /dev/null +++ b/src/beautifiers/jscs.coffee @@ -0,0 +1,40 @@ +"use strict" +Beautifier = require('./beautifier') +Checker = require 'jscs' +cliConfig = require 'jscs/lib/cli-config' + +checker = null + +module.exports = class JSCSFixer extends Beautifier + name: "JSCS Fixer" + + options: { + JavaScript: false + } + + beautify: (text, language, options) -> + @verbose("JSCS Fixer language #{language}") + return new @Promise((resolve, reject) => + try + editor = atom.workspace.getActiveTextEditor() + path = if editor? then editor.getPath() else undefined + config = if path? then cliConfig.load(undefined, atom.project.relativizePath(path)[0]) else undefined + if !checker? + checker = new Checker() + checker.registerDefaultRules() + if !config? + throw new Error("No JSCS config found.") + checker.configure(config) + result = checker.fixString(text, path) + if result.errors.getErrorCount() > 0 + @error(result.errors.getErrorList().reduce((res, err) => + "#{res}
Line #{err.line}: #{err.message}" + , "JSCS Fixer error:")) + + resolve result.output + + catch err + @error("JSCS Fixer error: #{err}") + reject(err) + + )