From 225c01e2a2a32c9da7683d06ef069439e7b32aed Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Thu, 10 Sep 2020 18:53:25 +0900 Subject: [PATCH] [Fix #8673] Fix the JSON parse error when specifying `--format=json` and `--stdin` Fixes #8673. This PR returns only JSON result when specifying `--format=json` and `--stdin` to fix the JSON parse error. Similar HTML and JUnit formatters are targeted as well. --- CHANGELOG.md | 1 + lib/rubocop/cli/command/execute_runner.rb | 8 ++++++ spec/rubocop/cli/cli_options_spec.rb | 30 +++++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 160acd93bf9..49dc1dff9e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ * [#8704](https://github.com/rubocop-hq/rubocop/issues/8704): Fix an error for `Lint/AmbiguousOperator` when using safe navigation operator with a unary operator. ([@koic][]) * [#8661](https://github.com/rubocop-hq/rubocop/pull/8661): Fix an incorrect auto-correct for `Style/MultilineTernaryOperator` when returning a multiline ternary operator expression. ([@koic][]) * [#8526](https://github.com/rubocop-hq/rubocop/pull/8526): Fix a false positive for `Style/CaseEquality` cop when the receiver is not a camel cased constant. ([@koic][]) +* [#8673](https://github.com/rubocop-hq/rubocop/issues/8673): Fix the JSON parse error when specifying `--format=json` and `--stdin` options. ([@koic][]) ### Changes diff --git a/lib/rubocop/cli/command/execute_runner.rb b/lib/rubocop/cli/command/execute_runner.rb index 9895c1650a9..f336981ef07 100644 --- a/lib/rubocop/cli/command/execute_runner.rb +++ b/lib/rubocop/cli/command/execute_runner.rb @@ -8,6 +8,9 @@ module Command class ExecuteRunner < Base include Formatter::TextUtil + # Combination of short and long formatter names. + INTEGRATION_FORMATTERS = %w[h html j json ju junit].freeze + self.command_name = :execute_runner def run @@ -61,6 +64,11 @@ def display_error_summary(errors) end def maybe_print_corrected_source + # Integration tools (like RubyMine) expect to have only the JSON result + # when specifying JSON format. Similar HTML and JUnit are targeted as well. + # See: https://github.com/rubocop-hq/rubocop/issues/8673 + return if INTEGRATION_FORMATTERS.include?(@options[:format]) + # If we are asked to autocorrect source code read from stdin, the only # reasonable place to write it is to stdout # Unfortunately, we also write other information to stdout diff --git a/spec/rubocop/cli/cli_options_spec.rb b/spec/rubocop/cli/cli_options_spec.rb index 3ed5a4c5fc2..1d544d4344c 100644 --- a/spec/rubocop/cli/cli_options_spec.rb +++ b/spec/rubocop/cli/cli_options_spec.rb @@ -1719,6 +1719,36 @@ def f end end + it 'can parse JSON result when specifying `--format=json` and `--stdin` options' do + begin + $stdin = StringIO.new('p $/') + argv = ['--auto-correct-all', + '--only=Style/SpecialGlobalVars', + '--format=json', + '--stdin', + 'fake.rb'] + expect(cli.run(argv)).to eq(0) + expect { JSON.parse($stdout.string) }.not_to raise_error(JSON::ParserError) + ensure + $stdin = STDIN + end + end + + it 'can parse JSON result when specifying `--format=j` and `--stdin` options' do + begin + $stdin = StringIO.new('p $/') + argv = ['--auto-correct-all', + '--only=Style/SpecialGlobalVars', + '--format=j', + '--stdin', + 'fake.rb'] + expect(cli.run(argv)).to eq(0) + expect { JSON.parse($stdout.string) }.not_to raise_error(JSON::ParserError) + ensure + $stdin = STDIN + end + end + it 'detects CR at end of line' do begin create_file('example.rb', "puts 'hello world'\r")