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

Add Rule::Base.autocorrect_incompatible_with #534

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions spec/ameba/runner_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ module Ameba
source.issues.first.rule.name.should eq Rule::Lint::UnneededDisableDirective.rule_name
end
end

it "handles rules with incompatible autocorrect" do
rules = [Rule::Performance::MinMaxAfterMap.new, Rule::Style::VerboseBlock.new]
source = Source.new "list.map { |i| i.size }.max\n", File.tempfile("source", ".cr").path

Runner.new(rules, [source], formatter, default_severity, autocorrect: true).run
source.code.should eq "list.max_of(&.size)\n"
end
end

describe "#explain" do
Expand Down
5 changes: 5 additions & 0 deletions src/ameba/rule/base.cr
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ module Ameba::Rule
abstract class Base
include Config::RuleConfig

def self.autocorrect_incompatible_with
# TODO: can't use Class as generic type argument yet
[] of Base
end

# This method is designed to test the source passed in. If source has issues
# that are tested by this rule, it should add an issue.
#
Expand Down
4 changes: 4 additions & 0 deletions src/ameba/rule/performance/minmax_after_map.cr
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ module Ameba::Rule::Performance
MSG = "Use `%s {...}` instead of `map {...}.%s`."
CALL_NAMES = %w[min min? max max? minmax minmax?]

def self.autocorrect_incompatible_with
[Style::VerboseBlock]
end

def test(source)
AST::NodeVisitor.new self, source, skip: :macro
end
Expand Down
4 changes: 4 additions & 0 deletions src/ameba/rule/style/verbose_block.cr
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ module Ameba::Rule::Style
MSG = "Use short block notation instead: `%s`"
CALL_PATTERN = "%s(%s&.%s)"

def self.autocorrect_incompatible_with
[Performance::MinMaxAfterMap]
end

protected def same_location_lines?(a, b)
return unless a_location = name_location(a)
return unless b_location = b.location
Expand Down
5 changes: 5 additions & 0 deletions src/ameba/runner.cr
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,14 @@ module Ameba
@syntax_rule.test(source)
break unless source.valid?

rules_with_issues = [] of Rule::Base
@rules.each do |rule|
next if autocorrect? && rules_with_issues.any?(&.class.in?(rule.class.autocorrect_incompatible_with))
next if rule.excluded?(source)
size_before = source.issues.size
rule.test(source)
size_after = source.issues.size
rules_with_issues << rule if size_before != size_after
end
check_unneeded_directives(source)
break unless autocorrect? && source.correct?
Expand Down
Loading