diff --git a/CHANGELOG.md b/CHANGELOG.md index f5a31e3..ecd8ce3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,9 @@ # Next +- Fixes [#1773](https://github.com/Glavin001/atom-beautify/issues/1773) and [#793](https://github.com/Glavin001/atom-beautify/issues/793). ruby-beautify with Rubocop now respects `Exclude` settings, and properly loads inherited config files (like `~/.rubocop.yml`) - Fix [#1862](https://github.com/Glavin001/atom-beautify/issues/1862) Add support for ocp-indent as an executable - See [#601](https://github.com/Glavin001/atom-beautify/issues/1862) Add support for tsx files - See [#1497](https://github.com/Glavin001/atom-beautify/issues/1497) and [#802](https://github.com/Glavin001/atom-beautify/issues/802) +- ... # v0.30.9 (2017-11-22) - Fix [#1949](https://github.com/Glavin001/atom-beautify/issues/1949): Fix beautify on save when text has not changed. diff --git a/src/beautifiers/rubocop.coffee b/src/beautifiers/rubocop.coffee index deafffa..138dfb5 100644 --- a/src/beautifiers/rubocop.coffee +++ b/src/beautifiers/rubocop.coffee @@ -4,6 +4,7 @@ Requires https://github.com/bbatsov/rubocop "use strict" Beautifier = require('./beautifier') +path = require('path') module.exports = class Rubocop extends Beautifier name: "Rubocop" @@ -16,53 +17,50 @@ module.exports = class Rubocop extends Beautifier rubocop_path: true } - beautify: (text, language, options) -> + beautify: (text, language, options, context) -> + fullPath = context.filePath or "" + [projectPath, _relativePath] = atom.project.relativizePath(fullPath) + + # Find the rubocop path @Promise.all([ @which(options.rubocop_path) if options.rubocop_path @which('rubocop') - ]).then((paths) => + ]) + .then((paths) => @debug('rubocop paths', paths) - _ = require 'lodash' - path = require 'path' # Get first valid, absolute path - rubocopPath = _.find(paths, (p) -> p and path.isAbsolute(p) ) + rubocopPath = paths.find((p) -> p and path.isAbsolute(p)) or "rubocop" @verbose('rubocopPath', rubocopPath) @debug('rubocopPath', rubocopPath, paths) - configFile = path.join(atom.project.getPaths()[0], ".rubocop.yml") - - fs = require 'fs' - - if fs.existsSync(configFile) - @debug("rubocop", config, fs.readFileSync(configFile, 'utf8')) - else + # Find or generate a config file if non exists + configFile = @findFile(path.dirname(fullPath), ".rubocop.yml") + if !configFile? yaml = require("yaml-front-matter") - # Generate config file config = { "Style/IndentationWidth": "Width": options.indent_size } + tempConfig = @tempFile("rubocop-config", yaml.safeDump(config)) - configFile = @tempFile("rubocop-config", yaml.safeDump(config)) - @debug("rubocop", config, configFile) + rubocopArguments = [ + "--auto-correct" + "--force-exclusion" + "--stdin", "atom-beautify.rb" # filename is required but not used + ] + rubocopArguments.push("--config", tempConfig) if tempConfig? + @debug("rubocop arguments", rubocopArguments) - # Check if PHP-CS-Fixer path was found - if rubocopPath? - @run(rubocopPath, [ - "--auto-correct" - "--config", configFile - tempFile = @tempFile("temp", text, '.rb') - ], {ignoreReturnCode: true}) - .then(=> - @readFile(tempFile) - ) - else - @run("rubocop", [ - "--auto-correct" - "--config", configFile - tempFile = @tempFile("temp", text, '.rb') - ], {ignoreReturnCode: true}) - .then(=> - @readFile(tempFile) - ) -) + @run(rubocopPath, rubocopArguments, { + ignoreReturnCode: true, + cwd: projectPath, + onStdin: (stdin) -> stdin.end text + }).then((stdout) => + @debug("rubocop output", stdout) + # Rubocop output an error if stdout is empty + return text if stdout.length == 0 + + result = stdout.split("====================\n") + result[result.length - 1] + ) + )