Add MaxLineLength option to Style/VerboseBlock rule

This commit is contained in:
Sijawusz Pur Rahnama 2021-01-30 18:22:10 +01:00
parent a53d44617d
commit 4b7f3ba6ee
2 changed files with 28 additions and 0 deletions

View file

@ -90,6 +90,21 @@ module Ameba::Rule::Style
.catch(source).should_not be_valid .catch(source).should_not be_valid
end end
it "#max_line_length" do
source = Source.new %(
(1..3).tap &.tap &.tap &.tap &.tap &.tap &.tap do |i|
i.to_s.reverse.strip.blank?
end
)
rule = VerboseBlock.new
rule
.tap(&.max_line_length = 60)
.catch(source).should be_valid
rule
.tap(&.max_line_length = nil)
.catch(source).should_not be_valid
end
it "#max_length" do it "#max_length" do
source = Source.new %( source = Source.new %(
(1..3).tap { |i| i.to_s.split.reverse.join.strip.blank? } (1..3).tap { |i| i.to_s.split.reverse.join.strip.blank? }

View file

@ -23,6 +23,7 @@ module Ameba::Rule::Style
# ExcludeCallsWithBlocks: false # ExcludeCallsWithBlocks: false
# ExcludeOperators: false # ExcludeOperators: false
# ExcludeSetters: false # ExcludeSetters: false
# MaxLineLength: ~
# MaxLength: 50 # use ~ to disable # MaxLength: 50 # use ~ to disable
# ``` # ```
class VerboseBlock < Base class VerboseBlock < Base
@ -34,6 +35,7 @@ module Ameba::Rule::Style
exclude_operators false exclude_operators false
exclude_setters false exclude_setters false
max_line_length : Int32? = nil # 100
max_length : Int32? = 50 max_length : Int32? = 50
end end
@ -68,6 +70,16 @@ module Ameba::Rule::Style
true true
end end
protected def valid_line_length?(node, code)
if max_line_length = self.max_line_length
if location = node.name_location
final_line_length = location.column_number + code.size
return final_line_length <= max_line_length
end
end
true
end
protected def node_to_s(node : Crystal::Call) protected def node_to_s(node : Crystal::Call)
case name = node.name case name = node.name
when "[]" when "[]"
@ -141,6 +153,7 @@ module Ameba::Rule::Style
call_code = call_code =
call_code(node, body) call_code(node, body)
return unless valid_line_length?(node, call_code)
return unless valid_length?(call_code) return unless valid_length?(call_code)
issue_for node.name_location, node.end_location, issue_for node.name_location, node.end_location,