Skip to content

Commit

Permalink
[Fix #8673] Fix the JSON parse error when specifying --format=json
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
koic authored and bbatsov committed Sep 14, 2020
1 parent 4b6b6c8 commit 3282218
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions lib/rubocop/cli/command/execute_runner.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions spec/rubocop/cli/cli_options_spec.rb
Expand Up @@ -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")
Expand Down

0 comments on commit 3282218

Please sign in to comment.