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 #6722] Fix an error for Style/OneLineConditional #6727

Merged
merged 1 commit into from Feb 4, 2019
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 @@ -16,6 +16,7 @@
* [#6668](https://github.com/rubocop-hq/rubocop/issues/6668): Fix autocorrection for `Style/UnneededCondition` when conditional has the `unless` form. ([@mvz][])
* [#6382](https://github.com/rubocop-hq/rubocop/issues/6382): Fix `Layout/IndentationWidth` with `Layout/EndAlignment` set to start_of_line. ([@dischorde][], [@siegfault][], [@mhelmetag][])
* [#6710](https://github.com/rubocop-hq/rubocop/issues/6710): Fix `Naming/MemoizedInstanceVariableName` on method starts with underscore. ([@pocke][])
* [#6722](https://github.com/rubocop-hq/rubocop/issues/6722): Fix an error for `Style/OneLineConditional` when `then` branch has no body. ([@koic][])

### Changes

Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/style/one_line_conditional.rb
Expand Up @@ -67,6 +67,8 @@ def to_ternary(node)
end

def expr_replacement(node)
return 'nil' if node.nil?

requires_parentheses?(node) ? "(#{node.source})" : node.source
end

Expand Down
7 changes: 7 additions & 0 deletions spec/rubocop/cop/style/one_line_conditional_spec.rb
Expand Up @@ -38,6 +38,13 @@
end
end

context 'one line if/then/else/end when `then` branch has no body' do
let(:source) { 'if cond then else dont end' }

include_examples 'offense', 'if'
include_examples 'autocorrect', 'cond ? nil : dont'
end

context 'one line if/then/end' do
let(:source) { 'if cond then run end' }

Expand Down