提交 5ac6ec54 编写于 作者: K Koichi ITO

Enable autocorrect for `Lint/EndAlignment` cop

### Summary

This PR changes .rubocop.yml.

Regarding the code using `if ... else ... end`, I think the coding style
that Rails expects is as follows.

```ruby
var = if cond
  a
else
  b
end
```

However, the current .rubocop.yml setting does not offense for the
following code.

```ruby
var = if cond
        a
      else
        b
      end
```

I think that the above code expects offense to be warned.
Moreover, the layout by autocorrect is unnatural.

```ruby
var = if cond
  a
      else
        b
      end
```

This PR adds a setting to .rubocop.yml to make an offense warning and
autocorrect as expected by the coding style.
And this change also fixes `case ... when ... end` together.

Also this PR itself is an example that arranges the layout using
`rubocop -a`.

### Other Information

Autocorrect of `Lint/EndAlignment` cop is `false` by default.
https://github.com/bbatsov/rubocop/blob/v0.51.0/config/default.yml#L1443

This PR changes this value to `true`.

Also this PR has changed it together as it is necessary to enable
`Layout/ElseAlignment` cop to make this behavior.
上级 ffd9902b
...@@ -26,6 +26,9 @@ Layout/CaseIndentation: ...@@ -26,6 +26,9 @@ Layout/CaseIndentation:
Layout/CommentIndentation: Layout/CommentIndentation:
Enabled: true Enabled: true
Layout/ElseAlignment:
Enabled: true
Layout/EmptyLineAfterMagicComment: Layout/EmptyLineAfterMagicComment:
Enabled: true Enabled: true
...@@ -140,6 +143,7 @@ Style/UnneededPercentQ: ...@@ -140,6 +143,7 @@ Style/UnneededPercentQ:
Lint/EndAlignment: Lint/EndAlignment:
Enabled: true Enabled: true
EnforcedStyleAlignWith: variable EnforcedStyleAlignWith: variable
AutoCorrect: true
# Use my_method(my_arg) not my_method( my_arg ) or my_method my_arg. # Use my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
Lint/RequireParentheses: Lint/RequireParentheses:
......
...@@ -83,7 +83,7 @@ def transmit(message) ...@@ -83,7 +83,7 @@ def transmit(message)
when Numeric then @driver.text(message.to_s) when Numeric then @driver.text(message.to_s)
when String then @driver.text(message) when String then @driver.text(message)
when Array then @driver.binary(message) when Array then @driver.binary(message)
else false else false
end end
end end
......
...@@ -274,7 +274,7 @@ def port ...@@ -274,7 +274,7 @@ def port
def standard_port def standard_port
case protocol case protocol
when "https://" then 443 when "https://" then 443
else 80 else 80
end end
end end
......
...@@ -116,7 +116,7 @@ def distance_of_time_in_words(from_time, to_time = 0, options = {}) ...@@ -116,7 +116,7 @@ def distance_of_time_in_words(from_time, to_time = 0, options = {})
when 10..19 then locale.t :less_than_x_seconds, count: 20 when 10..19 then locale.t :less_than_x_seconds, count: 20
when 20..39 then locale.t :half_a_minute when 20..39 then locale.t :half_a_minute
when 40..59 then locale.t :less_than_x_minutes, count: 1 when 40..59 then locale.t :less_than_x_minutes, count: 1
else locale.t :x_minutes, count: 1 else locale.t :x_minutes, count: 1
end end
when 2...45 then locale.t :x_minutes, count: distance_in_minutes when 2...45 then locale.t :x_minutes, count: distance_in_minutes
...@@ -131,7 +131,7 @@ def distance_of_time_in_words(from_time, to_time = 0, options = {}) ...@@ -131,7 +131,7 @@ def distance_of_time_in_words(from_time, to_time = 0, options = {})
when 43200...86400 then locale.t :about_x_months, count: (distance_in_minutes.to_f / 43200.0).round when 43200...86400 then locale.t :about_x_months, count: (distance_in_minutes.to_f / 43200.0).round
# 60 days up to 365 days # 60 days up to 365 days
when 86400...525600 then locale.t :x_months, count: (distance_in_minutes.to_f / 43200.0).round when 86400...525600 then locale.t :x_months, count: (distance_in_minutes.to_f / 43200.0).round
else else
from_year = from_time.year from_year = from_time.year
from_year += 1 if from_time.month >= 3 from_year += 1 if from_time.month >= 3
to_year = to_time.year to_year = to_time.year
......
...@@ -38,7 +38,7 @@ def create_database(name, options = {}) ...@@ -38,7 +38,7 @@ def create_database(name, options = {})
" TABLESPACE = \"#{value}\"" " TABLESPACE = \"#{value}\""
when :connection_limit when :connection_limit
" CONNECTION LIMIT = #{value}" " CONNECTION LIMIT = #{value}"
else else
"" ""
end end
end end
......
...@@ -165,7 +165,7 @@ def normalize_keys(params) ...@@ -165,7 +165,7 @@ def normalize_keys(params)
Hash[params.map { |k, v| [k.to_s.tr("-", "_"), normalize_keys(v)] } ] Hash[params.map { |k, v| [k.to_s.tr("-", "_"), normalize_keys(v)] } ]
when Array when Array
params.map { |v| normalize_keys(v) } params.map { |v| normalize_keys(v) }
else else
params params
end end
end end
...@@ -178,7 +178,7 @@ def deep_to_h(value) ...@@ -178,7 +178,7 @@ def deep_to_h(value)
process_array(value) process_array(value)
when String when String
value value
else else
raise "can't typecast #{value.class.name} - #{value.inspect}" raise "can't typecast #{value.class.name} - #{value.inspect}"
end end
end end
......
...@@ -670,7 +670,7 @@ def to_constant_name(desc) #:nodoc: ...@@ -670,7 +670,7 @@ def to_constant_name(desc) #:nodoc:
when Module when Module
desc.name || desc.name ||
raise(ArgumentError, "Anonymous modules have no name to be referenced by") raise(ArgumentError, "Anonymous modules have no name to be referenced by")
else raise TypeError, "Not a valid constant descriptor: #{desc.inspect}" else raise TypeError, "Not a valid constant descriptor: #{desc.inspect}"
end end
end end
......
...@@ -61,7 +61,7 @@ def deprecated_method_warning(method_name, message = nil) ...@@ -61,7 +61,7 @@ def deprecated_method_warning(method_name, message = nil)
case message case message
when Symbol then "#{warning} (use #{message} instead)" when Symbol then "#{warning} (use #{message} instead)"
when String then "#{warning} (#{message})" when String then "#{warning} (#{message})"
else warning else warning
end end
end end
......
...@@ -227,7 +227,7 @@ def clear(scope = :all) ...@@ -227,7 +227,7 @@ def clear(scope = :all)
case scope case scope
when :all when :all
@plurals, @singulars, @uncountables, @humans = [], [], Uncountables.new, [] @plurals, @singulars, @uncountables, @humans = [], [], Uncountables.new, []
else else
instance_variable_set "@#{scope}", [] instance_variable_set "@#{scope}", []
end end
end end
......
...@@ -350,7 +350,7 @@ def ordinal(number) ...@@ -350,7 +350,7 @@ def ordinal(number)
when 1; "st" when 1; "st"
when 2; "nd" when 2; "nd"
when 3; "rd" when 3; "rd"
else "th" else "th"
end end
end end
end end
......
...@@ -276,7 +276,7 @@ def normalize(string, form = nil) ...@@ -276,7 +276,7 @@ def normalize(string, form = nil)
reorder_characters(decompose(:compatibility, codepoints)) reorder_characters(decompose(:compatibility, codepoints))
when :kc when :kc
compose(reorder_characters(decompose(:compatibility, codepoints))) compose(reorder_characters(decompose(:compatibility, codepoints)))
else else
raise ArgumentError, "#{form} is not a valid normalization variant", caller raise ArgumentError, "#{form} is not a valid normalization variant", caller
end.pack("U*".freeze) end.pack("U*".freeze)
end end
......
...@@ -238,7 +238,7 @@ def [](arg) ...@@ -238,7 +238,7 @@ def [](arg)
when Numeric, ActiveSupport::Duration when Numeric, ActiveSupport::Duration
arg *= 3600 if arg.abs <= 13 arg *= 3600 if arg.abs <= 13
all.find { |z| z.utc_offset == arg.to_i } all.find { |z| z.utc_offset == arg.to_i }
else else
raise ArgumentError, "invalid argument to TimeZone[]: #{arg.inspect}" raise ArgumentError, "invalid argument to TimeZone[]: #{arg.inspect}"
end end
end end
......
...@@ -75,7 +75,7 @@ def field_type ...@@ -75,7 +75,7 @@ def field_type
when :date then :date_select when :date then :date_select
when :text then :text_area when :text then :text_area
when :boolean then :check_box when :boolean then :check_box
else else
:text_field :text_field
end end
end end
...@@ -91,7 +91,7 @@ def default ...@@ -91,7 +91,7 @@ def default
when :text then "MyText" when :text then "MyText"
when :boolean then false when :boolean then false
when :references, :belongs_to then nil when :references, :belongs_to then nil
else else
"" ""
end end
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册