Use property? for defining Bool-returning Rule properties

This commit is contained in:
Sijawusz Pur Rahnama 2022-11-22 19:49:16 +01:00
parent c7c75ee36a
commit 2af58cabd4
6 changed files with 21 additions and 16 deletions

View file

@ -242,7 +242,11 @@ class Ameba::Config
{% properties[name] = {key: key, default: value, type: type, converter: converter} %}
@[YAML::Field(key: {{ key }}, converter: {{ converter }}, type: {{ type }})]
property {{ name }} : {{ type }} = {{ value }}
{% if type == Bool %}
property? {{ name }} : {{ type }} = {{ value }}
{% else %}
property {{ name }} : {{ type }} = {{ value }}
{% end %}
{% end %}
{% if properties["enabled".id] == nil %}

View file

@ -41,15 +41,15 @@ module Ameba::Rule::Lint
end
def test(source, node : Crystal::ProcLiteral, scope : AST::Scope)
ignore_procs || find_unused_arguments source, scope
ignore_procs? || find_unused_arguments source, scope
end
def test(source, node : Crystal::Block, scope : AST::Scope)
ignore_blocks || find_unused_arguments source, scope
ignore_blocks? || find_unused_arguments source, scope
end
def test(source, node : Crystal::Def, scope : AST::Scope)
ignore_defs || find_unused_arguments source, scope
ignore_defs? || find_unused_arguments source, scope
end
private def find_unused_arguments(source, scope)

View file

@ -46,7 +46,7 @@ module Ameba::Rule::Style
when Crystal::Yield
!in_ternary || node.has_parentheses? || node.exps.empty?
when Crystal::Assign, Crystal::OpAssign, Crystal::MultiAssign
!in_ternary && !allow_safe_assignment
!in_ternary && !allow_safe_assignment?
else
true
end
@ -55,7 +55,7 @@ module Ameba::Rule::Style
def test(source, node : Crystal::If | Crystal::Unless | Crystal::Case | Crystal::While | Crystal::Until)
cond = node.cond
if cond.is_a?(Crystal::Assign) && allow_safe_assignment
if cond.is_a?(Crystal::Assign) && allow_safe_assignment?
issue_for cond, MSG_MISSING do |corrector|
corrector.insert_before(cond, '(')
corrector.insert_after(cond, ')')
@ -65,7 +65,7 @@ module Ameba::Rule::Style
is_ternary = node.is_a?(Crystal::If) && node.ternary?
return if is_ternary && exclude_ternary
return if is_ternary && exclude_ternary?
return unless cond.is_a?(Crystal::Expressions)
return unless cond.keyword.paren?

View file

@ -113,8 +113,8 @@ module Ameba::Rule::Style
end
def test(source, node : Crystal::Next, visitor : AST::RedundantControlExpressionVisitor)
return if allow_multi_next && node.exp.is_a?(Crystal::TupleLiteral)
return if allow_empty_next && (node.exp.nil? || node.exp.try(&.nop?))
return if allow_multi_next? && node.exp.is_a?(Crystal::TupleLiteral)
return if allow_empty_next? && (node.exp.nil? || node.exp.try(&.nop?))
if exp_code = control_exp_code(node, source.lines)
issue_for node, MSG do |corrector|

View file

@ -110,8 +110,8 @@ module Ameba::Rule::Style
end
def test(source, node : Crystal::Return, visitor : AST::RedundantControlExpressionVisitor)
return if allow_multi_return && node.exp.is_a?(Crystal::TupleLiteral)
return if allow_empty_return && (node.exp.nil? || node.exp.try(&.nop?))
return if allow_multi_return? && node.exp.is_a?(Crystal::TupleLiteral)
return if allow_empty_return? && (node.exp.nil? || node.exp.try(&.nop?))
if exp_code = control_exp_code(node, source.lines)
issue_for node, MSG do |corrector|

View file

@ -190,11 +190,12 @@ module Ameba::Rule::Style
protected def issue_for_valid(source, call : Crystal::Call, block : Crystal::Block, body : Crystal::Call)
return unless location = call.name_location
return unless end_location = block.end_location
return if exclude_calls_with_block && body.block
return if exclude_multiple_line_blocks && !same_location_lines?(call, body)
return if exclude_prefix_operators && prefix_operator?(body)
return if exclude_operators && operator?(body.name)
return if exclude_setters && setter?(body.name)
return if exclude_calls_with_block? && body.block
return if exclude_multiple_line_blocks? && !same_location_lines?(call, body)
return if exclude_prefix_operators? && prefix_operator?(body)
return if exclude_operators? && operator?(body.name)
return if exclude_setters? && setter?(body.name)
call_code =
call_code(source, call, body)