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 autocorrect #248

Merged
merged 23 commits into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c1b4add
Add `Source::Corrector` and `Source::Rewriter`
FnControlOption Oct 24, 2021
d3d3ccd
Add `expect_correction` and `expect_no_corrections`
FnControlOption Oct 24, 2021
99e7ccd
Add `--autocorrect` CLI option
FnControlOption Oct 24, 2021
e5fb052
Autocorrect `Style/LargeNumbers`
FnControlOption Oct 23, 2021
573881c
Autocorrect `Layout/TrailingBlankLines` partially
FnControlOption Oct 24, 2021
f39a7a4
Re-run autocorrect until all correctable issues have been corrected
FnControlOption Oct 25, 2021
1d5f554
Apply suggestions from code review
FnControlOption Oct 26, 2021
f87d99a
Add `Corrector` method overloads that accept `ASTNode`
FnControlOption Oct 26, 2021
e8c0f49
Delete redundant alias `SourceLocation`
FnControlOption Oct 27, 2021
3b11491
Add `insert_*` overloads that accept both location and end_location
FnControlOption Oct 27, 2021
1660896
Allowed named arguments with `Rule::Base.issue_for`
FnControlOption Oct 26, 2021
437584f
Raise error if infinite correction loop
FnControlOption Oct 26, 2021
749c535
Add documentation
FnControlOption Oct 27, 2021
8d3b760
Add autocorrect checks to Flycheck, JSON, and TODO formatters
FnControlOption Oct 27, 2021
1b6fe40
Add autocorrect checks to `ExplainFormatter`
FnControlOption Oct 27, 2021
d51ef27
Add `remove_preceding`, `remove_leading`, `remove_trailing`
FnControlOption Oct 27, 2021
b7bb282
Apply suggestions from code review
FnControlOption Oct 27, 2021
73e97ac
Avoid using iterators and throw-away heap allocations
FnControlOption Oct 27, 2021
61fc99e
Inline the `do_combine` method
FnControlOption Oct 27, 2021
a40fdee
Revert "Add autocorrect checks to `ExplainFormatter`"
FnControlOption Nov 1, 2021
470e41c
Raise error if attempting to both explain issue and autocorrect
FnControlOption Nov 1, 2021
c2aa2fe
Return `source` from `expect_issue`
FnControlOption Nov 1, 2021
7807172
Rename `s` to `source`
FnControlOption Nov 1, 2021
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
16 changes: 8 additions & 8 deletions spec/ameba/formatter/util_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,24 @@ module Ameba::Formatter

describe "#affected_code" do
it "returns nil if there is no such a line number" do
source = Source.new %(
code = <<-EOF
a = 1
)
EOF
location = Crystal::Location.new("filename", 2, 1)
subject.affected_code(source, location).should be_nil
subject.affected_code(code, location).should be_nil
end

it "returns correct line if it is found" do
source = Source.new %(
code = <<-EOF
a = 1
)
EOF
location = Crystal::Location.new("filename", 1, 1)
subject.deansify(subject.affected_code(source, location))
subject.deansify(subject.affected_code(code, location))
.should eq "> a = 1\n ^\n"
end

it "returns correct line if it is found" do
source = Source.new <<-EOF
code = <<-EOF
# pre:1
# pre:2
# pre:3
Expand All @@ -101,7 +101,7 @@ module Ameba::Formatter
EOF

location = Crystal::Location.new("filename", 6, 1)
subject.deansify(subject.affected_code(source, location, context_lines: 3))
subject.deansify(subject.affected_code(code, location, context_lines: 3))
.should eq <<-STR
> # pre:3
> # pre:4
Expand Down
15 changes: 10 additions & 5 deletions spec/ameba/issue_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ require "../spec_helper"
module Ameba
describe Issue do
it "accepts rule and message" do
issue = Issue.new rule: DummyRule.new,
issue = Issue.new code: "",
rule: DummyRule.new,
location: nil,
end_location: nil,
message: "Blah",
Expand All @@ -15,7 +16,8 @@ module Ameba

it "accepts location" do
location = Crystal::Location.new("path", 3, 2)
issue = Issue.new rule: DummyRule.new,
issue = Issue.new code: "",
rule: DummyRule.new,
location: location,
end_location: nil,
message: "Blah",
Expand All @@ -27,7 +29,8 @@ module Ameba

it "accepts end_location" do
location = Crystal::Location.new("path", 3, 2)
issue = Issue.new rule: DummyRule.new,
issue = Issue.new code: "",
rule: DummyRule.new,
location: nil,
end_location: location,
message: "Blah",
Expand All @@ -38,7 +41,8 @@ module Ameba
end

it "accepts status" do
issue = Issue.new rule: DummyRule.new,
issue = Issue.new code: "",
rule: DummyRule.new,
location: nil,
end_location: nil,
message: "",
Expand All @@ -50,7 +54,8 @@ module Ameba
end

it "sets status to :enabled by default" do
issue = Issue.new rule: DummyRule.new,
issue = Issue.new code: "",
rule: DummyRule.new,
location: nil,
end_location: nil,
message: ""
Expand Down
22 changes: 10 additions & 12 deletions spec/ameba/rule/layout/trailing_blank_lines_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,26 @@ module Ameba::Rule::Layout

describe TrailingBlankLines do
it "passes if there is a blank line at the end of a source" do
source = Source.new "a = 1\n", normalize: false
subject.catch(source).should be_valid
expect_no_issues subject, "a = 1\n", normalize: false
end

it "passes if source is empty" do
source = Source.new ""
subject.catch(source).should be_valid
expect_no_issues subject, ""
end

it "fails if there is no blank lines at the end" do
source = Source.new "no-blankline"
subject.catch(source).should_not be_valid
source = expect_issue subject, "no-blankline # error: Trailing newline missing"
expect_correction source, "no-blankline\n"
end

it "fails if there more then one blank line at the end of a source" do
source = Source.new "a = 1\n \n", normalize: false
subject.catch(source).should_not be_valid
source = expect_issue subject, "a = 1\n \n # error: Excessive trailing newline detected", normalize: false
expect_no_corrections source
end

it "fails if last line is not blank" do
source = Source.new "\n\n\n puts 22", normalize: false
subject.catch(source).should_not be_valid
source = expect_issue subject, "\n\n\n puts 22 # error: Trailing newline missing", normalize: false
expect_correction source, "\n\n\n puts 22\n"
end

context "when unnecessary blank line has been detected" do
Expand All @@ -36,7 +34,7 @@ module Ameba::Rule::Layout

issue = source.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:1"
issue.location.to_s.should eq "source.cr:3:1"
issue.end_location.should be_nil
issue.message.should eq "Excessive trailing newline detected"
end
Expand All @@ -49,7 +47,7 @@ module Ameba::Rule::Layout

issue = source.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:0:1"
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.should be_nil
issue.message.should eq "Trailing newline missing"
end
Expand Down
6 changes: 5 additions & 1 deletion spec/ameba/rule/style/large_numbers_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ module Ameba
it "transforms large number #{number}" do
rule = Rule::Style::LargeNumbers.new

expect_issue rule, <<-CRYSTAL, number: number
source = expect_issue rule, <<-CRYSTAL, number: number
number = %{number}
# ^{number} error: Large numbers should be written with underscores: #{expected}
CRYSTAL

expect_correction source, <<-CRYSTAL
number = #{expected}
CRYSTAL
end
end

Expand Down
67 changes: 67 additions & 0 deletions spec/ameba/runner_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ module Ameba

config.update_rule ErrorRule.rule_name, enabled: false
config.update_rule PerfRule.rule_name, enabled: false
config.update_rule AtoAA.rule_name, enabled: false
config.update_rule AtoB.rule_name, enabled: false
config.update_rule BtoA.rule_name, enabled: false
config.update_rule BtoC.rule_name, enabled: false
config.update_rule CtoA.rule_name, enabled: false
config.update_rule ClassToModule.rule_name, enabled: false
config.update_rule ModuleToClass.rule_name, enabled: false

Runner.new(config)
end
Expand Down Expand Up @@ -54,6 +61,15 @@ module Ameba
Runner.new(all_rules, [source], formatter, default_severity).run.success?.should be_true
end

it "aborts because of an infinite loop" do
rules = [AtoAA.new] of Rule::Base
source = Source.new "class A; end", "source.cr"
message = "Infinite loop in source.cr caused by Ameba/AtoAA"
expect_raises(Runner::InfiniteCorrectionLoopError, message) do
Runner.new(rules, [source], formatter, default_severity, autocorrect: true).run
end
end

context "exception in rule" do
it "raises an exception raised in fiber while running a rule" do
rule = RaiseRule.new
Expand Down Expand Up @@ -180,5 +196,56 @@ module Ameba
.run.success?.should be_true
end
end

describe "#run with rules autocorrecting each other" do
context "with two conflicting rules" do
context "if there is an offense in an inspected file" do
it "aborts because of an infinite loop" do
rules = [AtoB.new, BtoA.new]
source = Source.new "class A; end", "source.cr"
message = "Infinite loop in source.cr caused by Ameba/AtoB -> Ameba/BtoA"
expect_raises(Runner::InfiniteCorrectionLoopError, message) do
Runner.new(rules, [source], formatter, default_severity, autocorrect: true).run
end
end
end

context "if there are multiple offenses in an inspected file" do
it "aborts because of an infinite loop" do
rules = [AtoB.new, BtoA.new]
source = Source.new %(
class A; end
class A_A; end
), "source.cr"
message = "Infinite loop in source.cr caused by Ameba/AtoB -> Ameba/BtoA"
expect_raises(Runner::InfiniteCorrectionLoopError, message) do
Runner.new(rules, [source], formatter, default_severity, autocorrect: true).run
end
end
end
end

context "with two pairs of conflicting rules" do
it "aborts because of an infinite loop" do
rules = [ClassToModule.new, ModuleToClass.new, AtoB.new, BtoA.new]
source = Source.new "class A_A; end", "source.cr"
message = "Infinite loop in source.cr caused by Ameba/ClassToModule, Ameba/AtoB -> Ameba/ModuleToClass, Ameba/BtoA"
expect_raises(Runner::InfiniteCorrectionLoopError, message) do
Runner.new(rules, [source], formatter, default_severity, autocorrect: true).run
end
end
end

context "with three rule cycle" do
it "aborts because of an infinite loop" do
rules = [AtoB.new, BtoC.new, CtoA.new]
source = Source.new "class A; end", "source.cr"
message = "Infinite loop in source.cr caused by Ameba/AtoB -> Ameba/BtoC -> Ameba/CtoA"
expect_raises(Runner::InfiniteCorrectionLoopError, message) do
Runner.new(rules, [source], formatter, default_severity, autocorrect: true).run
end
end
end
end
end
end
110 changes: 110 additions & 0 deletions spec/ameba/source/rewriter_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
require "../../spec_helper"

class Ameba::Source
describe Rewriter do
code = "puts(:hello, :world)"
hello = {5, 11}
comma_space = {11, 13}
world = {13, 19}

it "can remove" do
rewriter = Rewriter.new(code)
rewriter.remove(*hello)
rewriter.process.should eq "puts(, :world)"
end

it "can insert before" do
rewriter = Rewriter.new(code)
rewriter.insert_before(*world, "42, ")
rewriter.process.should eq "puts(:hello, 42, :world)"
end

it "can insert after" do
rewriter = Rewriter.new(code)
rewriter.insert_after(*hello, ", 42")
rewriter.process.should eq "puts(:hello, 42, :world)"
end

it "can wrap" do
rewriter = Rewriter.new(code)
rewriter.wrap(*hello, '[', ']')
rewriter.process.should eq "puts([:hello], :world)"
end

it "can replace" do
rewriter = Rewriter.new(code)
rewriter.replace(*hello, ":hi")
rewriter.process.should eq "puts(:hi, :world)"
end

it "accepts crossing deletions" do
rewriter = Rewriter.new(code)
rewriter.remove(hello[0], comma_space[1])
rewriter.remove(comma_space[0], world[1])
rewriter.process.should eq "puts()"
end

it "accepts multiple actions" do
rewriter = Rewriter.new(code)
rewriter.replace(*comma_space, " => ")
rewriter.wrap(hello[0], world[1], '{', '}')
rewriter.replace(*world, ":everybody")
rewriter.wrap(*world, '[', ']')
rewriter.process.should eq "puts({:hello => [:everybody]})"
end

it "can wrap the same range" do
rewriter = Rewriter.new(code)
rewriter.wrap(*hello, '(', ')')
rewriter.wrap(*hello, '[', ']')
rewriter.process.should eq "puts([(:hello)], :world)"
end

it "can insert on empty ranges" do
rewriter = Rewriter.new(code)
rewriter.insert_before(hello[0], '{')
rewriter.replace(hello[0], hello[0], 'x')
rewriter.insert_after(hello[0], '}')
rewriter.insert_before(hello[1], '[')
rewriter.replace(hello[1], hello[1], 'y')
rewriter.insert_after(hello[1], ']')
rewriter.process.should eq "puts({x}:hello[y], :world)"
end

it "can replace the same range" do
rewriter = Rewriter.new(code)
rewriter.replace(*hello, ":hi")
rewriter.replace(*hello, ":hey")
rewriter.process.should eq "puts(:hey, :world)"
end

it "can swallow insertions" do
rewriter = Rewriter.new(code)
rewriter.wrap(hello[0] + 1, hello[1], "__", "__")
rewriter.replace(world[0], world[1] - 2, "xx")
rewriter.replace(hello[0], world[1], ":hi")
rewriter.process.should eq "puts(:hi)"
end

it "rejects out-of-range ranges" do
rewriter = Rewriter.new(code)
expect_raises(IndexError) { rewriter.insert_before(0, 100, "hola") }
end

it "ignores trivial actions" do
rewriter = Rewriter.new(code)
rewriter.empty?.should be_true

# This is a trivial wrap
rewriter.wrap(2, 5, "", "")
rewriter.empty?.should be_true

# This is a trivial deletion
rewriter.remove(2, 2)
rewriter.empty?.should be_true

rewriter.remove(2, 5)
rewriter.empty?.should be_false
end
end
end
6 changes: 4 additions & 2 deletions spec/ameba/spec/annotated_source_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "../../spec_helper"

private def dummy_issue(message,
private def dummy_issue(code,
message,
position : {Int32, Int32}?,
end_position : {Int32, Int32}?,
path = "")
Expand All @@ -9,6 +10,7 @@ private def dummy_issue(message,
end_location = Crystal::Location.new(path, *end_position) if end_position

Ameba::Issue.new(
code: code,
rule: Ameba::DummyRule.new,
location: location,
end_location: end_location,
Expand All @@ -25,7 +27,7 @@ private def expect_invalid_location(code,
expect_raises Exception, exception_message, file, line do
Ameba::Spec::AnnotatedSource.new(
lines: code.lines,
issues: [dummy_issue("Message", position, end_position, "path")]
issues: [dummy_issue(code, "Message", position, end_position, "path")]
)
end
end
Expand Down
Loading