Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #8673] Fix the JSON parse error when specifying --format=json and --stdin #8694

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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