mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
New rule: UnlessElse
This commit is contained in:
parent
93dd7d446f
commit
11005930f6
7 changed files with 134 additions and 20 deletions
14
README.md
14
README.md
|
@ -40,18 +40,18 @@ Ameba.run
|
||||||
```
|
```
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
Inspecting 7 files.
|
Inspecting 18 files.
|
||||||
|
|
||||||
|
|
||||||
..F...F
|
...............F.F
|
||||||
|
|
||||||
7 inspected, 2 failures.
|
18 inspected, 2 failures.
|
||||||
|
|
||||||
src/ameba/formatter.cr:47
|
src/ameba/source.cr:26
|
||||||
LineLength: Line too long [122]
|
LineLength: Line too long (82 symbols)
|
||||||
|
|
||||||
src/ameba.cr:18
|
src/ameba.cr:12
|
||||||
LineLength: Line too long [81]
|
UnlessElse: Favour if over unless with else
|
||||||
```
|
```
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
44
spec/ameba/rules/unless_else_spec.cr
Normal file
44
spec/ameba/rules/unless_else_spec.cr
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
require "../../spec_helper"
|
||||||
|
|
||||||
|
module Ameba::Rules
|
||||||
|
subject = UnlessElse.new
|
||||||
|
|
||||||
|
describe UnlessElse do
|
||||||
|
it "passes if unless hasn't else" do
|
||||||
|
s = Source.new %(
|
||||||
|
unless something
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
)
|
||||||
|
subject.catch(s).valid?.should be_true
|
||||||
|
end
|
||||||
|
|
||||||
|
it "fails if unless has else" do
|
||||||
|
s = Source.new %(
|
||||||
|
unless something
|
||||||
|
:one
|
||||||
|
else
|
||||||
|
:two
|
||||||
|
end
|
||||||
|
)
|
||||||
|
subject.catch(s).valid?.should be_false
|
||||||
|
end
|
||||||
|
|
||||||
|
it "reports rule, pos and message" do
|
||||||
|
s = Source.new %(
|
||||||
|
unless something
|
||||||
|
:one
|
||||||
|
else
|
||||||
|
:two
|
||||||
|
end
|
||||||
|
)
|
||||||
|
subject.catch(s)
|
||||||
|
|
||||||
|
error = s.errors.first
|
||||||
|
error.should_not be_nil
|
||||||
|
error.rule.should_not be_nil
|
||||||
|
error.pos.should eq 2
|
||||||
|
error.message.should eq "Favour if over unless with else"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -13,7 +13,7 @@ module Ameba
|
||||||
end
|
end
|
||||||
|
|
||||||
def run(files, formatter : Formatter)
|
def run(files, formatter : Formatter)
|
||||||
sources = files.map { |path| Source.new(File.read path) }
|
sources = files.map { |path| Source.new(File.read(path), path) }
|
||||||
|
|
||||||
reporter = Reporter.new formatter
|
reporter = Reporter.new formatter
|
||||||
reporter.start sources
|
reporter.start sources
|
||||||
|
|
32
src/ameba/dsl.cr
Normal file
32
src/ameba/dsl.cr
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
module Ameba
|
||||||
|
macro rule(name, &block)
|
||||||
|
module Ameba::Rules
|
||||||
|
struct {{name.id}} < Rule
|
||||||
|
def test(source)
|
||||||
|
{{block.body}}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
macro visitor(name, node, &block)
|
||||||
|
module Ameba::Rules
|
||||||
|
class {{name.id}}Visitor < Crystal::Visitor
|
||||||
|
@rule : Rule
|
||||||
|
@source : Source
|
||||||
|
|
||||||
|
def initialize(@rule, @source)
|
||||||
|
@source.ast.accept self
|
||||||
|
end
|
||||||
|
|
||||||
|
def visit(node : Crystal::ASTNode)
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def visit(node : {{node.id}})
|
||||||
|
{{block.body}}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -3,18 +3,9 @@ module Ameba
|
||||||
Rules::LineLength,
|
Rules::LineLength,
|
||||||
Rules::TrailingBlankLines,
|
Rules::TrailingBlankLines,
|
||||||
Rules::TrailingWhitespace,
|
Rules::TrailingWhitespace,
|
||||||
|
Rules::UnlessElse,
|
||||||
]
|
]
|
||||||
|
|
||||||
macro rule(name, &block)
|
|
||||||
module Ameba::Rules
|
|
||||||
struct {{name.id}} < Rule
|
|
||||||
def test(source)
|
|
||||||
{{block.body}}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
abstract struct Rule
|
abstract struct Rule
|
||||||
abstract def test(source : Source)
|
abstract def test(source : Source)
|
||||||
|
|
||||||
|
|
47
src/ameba/rules/unless_else.cr
Normal file
47
src/ameba/rules/unless_else.cr
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
# A rule that disallows the use of an `else` block with the `unless`.
|
||||||
|
#
|
||||||
|
# For example, the rule considers these valid:
|
||||||
|
#
|
||||||
|
# ```crystal
|
||||||
|
# unless something
|
||||||
|
# :ok
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# if something
|
||||||
|
# :one
|
||||||
|
# else
|
||||||
|
# :two
|
||||||
|
# end
|
||||||
|
# ```
|
||||||
|
#
|
||||||
|
# But it considers this one invalid as it is an `unless` with an `else`:
|
||||||
|
#
|
||||||
|
# ```crystal
|
||||||
|
# unless something
|
||||||
|
# :one
|
||||||
|
# else
|
||||||
|
# :two
|
||||||
|
# end
|
||||||
|
# ```
|
||||||
|
#
|
||||||
|
# The solution is to swap the order of the blocks, and change the `unless` to
|
||||||
|
# an `if`, so the previous invalid example would become this:
|
||||||
|
#
|
||||||
|
# ```crystal
|
||||||
|
# if something
|
||||||
|
# :two
|
||||||
|
# else
|
||||||
|
# :one
|
||||||
|
# end
|
||||||
|
# ```
|
||||||
|
|
||||||
|
Ameba.rule UnlessElse do |source|
|
||||||
|
UnlessElseVisitor.new self, source
|
||||||
|
end
|
||||||
|
|
||||||
|
Ameba.visitor UnlessElse, Crystal::Unless do |node|
|
||||||
|
unless node.else.is_a?(Crystal::Nop)
|
||||||
|
@source.error @rule, node.location.try &.line_number,
|
||||||
|
"Favour if over unless with else"
|
||||||
|
end
|
||||||
|
end
|
|
@ -10,7 +10,7 @@ module Ameba
|
||||||
# position of the error and a message.
|
# position of the error and a message.
|
||||||
record Error,
|
record Error,
|
||||||
rule : Rule,
|
rule : Rule,
|
||||||
pos : Int32,
|
pos : Int32?,
|
||||||
message : String
|
message : String
|
||||||
|
|
||||||
getter lines : Array(String)
|
getter lines : Array(String)
|
||||||
|
@ -22,7 +22,7 @@ module Ameba
|
||||||
@lines = @content.split("\n")
|
@lines = @content.split("\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
def error(rule : Rule, line_number : Int32, message : String)
|
def error(rule : Rule, line_number : Int32?, message : String)
|
||||||
errors << Error.new rule, line_number, message
|
errors << Error.new rule, line_number, message
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue