Skip to content

Commit

Permalink
Merge pull request #207 from crystal-ameba/release/0.14.0
Browse files Browse the repository at this point in the history
Release 0.14.0
  • Loading branch information
veelenga authored Feb 24, 2021
2 parents 51b0a07 + 3a1ee7f commit d8c32f0
Show file tree
Hide file tree
Showing 106 changed files with 2,045 additions and 448 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<p align="center">Code style linter for Crystal<p>
<p align="center">
<sup>
<i> (a single-celled animal that catches food and moves about by extending fingerlike projections of protoplasm) </i>
<i>(a single-celled animal that catches food and moves about by extending fingerlike projections of protoplasm)</i>
</sup>
</p>
<p align="center">
Expand Down Expand Up @@ -35,7 +35,7 @@
## About

Ameba is a static code analysis tool for the Crystal language.
It enforces a consistent [Crystal code style](https://crystal-lang.org/docs/conventions/coding_style.html),
It enforces a consistent [Crystal code style](https://crystal-lang.org/reference/conventions/coding_style.html),
also catches code smells and wrong code constructions.

See also [Roadmap](https://github.com/crystal-ameba/ameba/wiki).
Expand All @@ -46,7 +46,7 @@ Run `ameba` binary within your project directory to catch code issues:

```sh
$ ameba
Inspecting 107 files.
Inspecting 107 files

...............F.....................F....................................................................

Expand All @@ -61,9 +61,7 @@ src/ameba/formatter/base_formatter.cr:12:7
^

Finished in 542.64 milliseconds

129 inspected, 2 failures.

129 inspected, 2 failures
```

### Run in parallel
Expand Down
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: ameba
version: 0.13.4
version: 0.14.0

authors:
- Vitalii Elenhaupt <[email protected]>
Expand Down
2 changes: 1 addition & 1 deletion spec/ameba/ast/scope_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ module Ameba::AST
it "adds a new variable to the scope" do
scope = Scope.new as_node("")
scope.add_variable(Crystal::Var.new "foo")
scope.variables.any?.should be_true
scope.variables.empty?.should be_false
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/ameba/ast/variabling/variable_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ module Ameba::AST
it "assigns the variable (creates a new assignment)" do
variable = Variable.new(var_node, scope)
variable.assign(assign_node, scope)
variable.assignments.any?.should be_true
variable.assignments.empty?.should be_false
end

it "can create multiple assignments" do
Expand All @@ -64,7 +64,7 @@ module Ameba::AST
variable = Variable.new(var_node, scope)
variable.assign(as_node("foo=1"), scope)
variable.reference(var_node, scope)
variable.references.any?.should be_true
variable.references.empty?.should be_false
end

it "adds a reference to the scope" do
Expand Down
17 changes: 17 additions & 0 deletions spec/ameba/ast/visitors/top_level_nodes_visitor_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require "../../../spec_helper"

module Ameba::AST
describe TopLevelNodesVisitor do
describe "#require_nodes" do
it "returns require node" do
source = Source.new %(
require "foo"
def bar; end
)
visitor = TopLevelNodesVisitor.new(source.ast)
visitor.require_nodes.size.should eq 1
visitor.require_nodes.first.to_s.should eq %q(require "foo")
end
end
end
end
10 changes: 10 additions & 0 deletions spec/ameba/cli/cmd_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ module Ameba::Cli
c.except.should eq %w(RULE1 RULE2)
end

it "defaults rules? flag to false" do
c = Cli.parse_args %w(file.cr)
c.rules?.should eq false
end

it "accepts --rules flag" do
c = Cli.parse_args %w(--rules)
c.rules?.should eq true
end

it "defaults all? flag to false" do
c = Cli.parse_args %w(file.cr)
c.all?.should eq false
Expand Down
4 changes: 2 additions & 2 deletions spec/ameba/config_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ module Ameba
it "returns list of sources" do
config.sources.size.should be > 0
config.sources.first.should be_a Source
config.sources.any? { |s| s.fullpath == __FILE__ }.should be_true
config.sources.any?(&.fullpath.==(__FILE__)).should be_true
end

it "returns a list of sources mathing globs" do
Expand All @@ -130,7 +130,7 @@ module Ameba

it "returns a lisf of sources excluding 'Excluded'" do
config.excluded = %w(**/config_spec.cr)
config.sources.any? { |s| s.fullpath == __FILE__ }.should be_false
config.sources.any?(&.fullpath.==(__FILE__)).should be_false
end
end

Expand Down
10 changes: 5 additions & 5 deletions spec/ameba/formatter/dot_formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Ameba::Formatter
describe "#started" do
it "writes started message" do
subject.started [Source.new ""]
output.to_s.should eq "Inspecting 1 file.\n\n"
output.to_s.should eq "Inspecting 1 file\n\n"
end
end

Expand All @@ -29,7 +29,7 @@ module Ameba::Formatter
describe "#finished" do
it "writes a final message" do
subject.finished [Source.new ""]
output.to_s.should contain "1 inspected, 0 failures."
output.to_s.should contain "1 inspected, 0 failures"
end

it "writes the elapsed time" do
Expand All @@ -45,7 +45,7 @@ module Ameba::Formatter
end
subject.finished [s]
log = output.to_s
log.should contain "1 inspected, 2 failures."
log.should contain "1 inspected, 2 failures"
log.should contain "DummyRuleError"
log.should contain "NamedRuleError"
end
Expand All @@ -60,7 +60,7 @@ module Ameba::Formatter
end
subject.finished [s]
log = output.to_s
log.should contain "> a = 22"
log.should contain "> \e[97ma = 22"
log.should contain " \e[33m^\e[0m"
end

Expand Down Expand Up @@ -99,7 +99,7 @@ module Ameba::Formatter
s.add_issue(DummyRule.new, location: {1, 1},
message: "DummyRuleError", status: :disabled)
subject.finished [s]
output.to_s.should contain "1 inspected, 0 failures."
output.to_s.should contain "1 inspected, 0 failures"
end
end
end
Expand Down
92 changes: 91 additions & 1 deletion spec/ameba/formatter/util_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,65 @@ module Ameba::Formatter
subject = Subject.new

describe Util do
describe "#deansify" do
it "returns given string without ANSI codes" do
str = String.build do |io|
io << "foo".colorize.green.underline
io << '-'
io << "bar".colorize.red.underline
end
subject.deansify("foo-bar").should eq "foo-bar"
subject.deansify(str).should eq "foo-bar"
end
end

describe "#trim" do
it "trims string longer than :max_length" do
subject.trim(("+" * 300), 1).should eq "+"
subject.trim(("+" * 300), 3).should eq "+++"
subject.trim(("+" * 300), 5).should eq "+ ..."
subject.trim(("+" * 300), 7).should eq "+++ ..."
end

it "leaves intact string shorter than :max_length" do
subject.trim(("+" * 3), 100).should eq "+++"
end

it "allows to use custom ellipsis" do
subject.trim(("+" * 300), 3, "").should eq "++…"
end
end

describe "#context" do
it "returns correct pre/post context lines" do
source = Source.new <<-EOF
# pre:1
# pre:2
# pre:3
# pre:4
# pre:5
a = 1
# post:1
# post:2
# post:3
# post:4
# post:5
EOF

subject.context(source.lines, lineno: 6, context_lines: 3)
.should eq({<<-PRE.lines, <<-POST.lines
# pre:3
# pre:4
# pre:5
PRE
# post:1
# post:2
# post:3
POST
})
end
end

describe "#affected_code" do
it "returns nil if there is no such a line number" do
source = Source.new %(
Expand All @@ -22,7 +81,38 @@ module Ameba::Formatter
a = 1
)
location = Crystal::Location.new("filename", 1, 1)
subject.affected_code(source, location).should eq "> a = 1\n \e[33m^\e[0m"
subject.deansify(subject.affected_code(source, location))
.should eq "> a = 1\n ^\n"
end

it "returns correct line if it is found" do
source = Source.new <<-EOF
# pre:1
# pre:2
# pre:3
# pre:4
# pre:5
a = 1
# post:1
# post:2
# post:3
# post:4
# post:5
EOF

location = Crystal::Location.new("filename", 6, 1)
subject.deansify(subject.affected_code(source, location, context_lines: 3))
.should eq <<-STR
> # pre:3
> # pre:4
> # pre:5
> a = 1
^
> # post:1
> # post:2
> # post:3
STR
end
end
end
Expand Down
17 changes: 15 additions & 2 deletions spec/ameba/issue_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,22 @@ module Ameba
location: nil,
end_location: nil,
message: "",
status: :enabled
status: :disabled

issue.status.should eq :enabled
issue.status.should eq Issue::Status::Disabled
issue.disabled?.should be_true
issue.enabled?.should be_false
end

it "sets status to :enabled by default" do
issue = Issue.new rule: DummyRule.new,
location: nil,
end_location: nil,
message: ""

issue.status.should eq Issue::Status::Enabled
issue.enabled?.should be_true
issue.disabled?.should be_false
end
end
end
48 changes: 48 additions & 0 deletions spec/ameba/rule/lint/duplicated_require_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require "../../../spec_helper"

module Ameba::Rule::Lint
subject = DuplicatedRequire.new

describe DuplicatedRequire do
it "passes if there are no duplicated requires" do
source = Source.new %(
require "math"
require "big"
require "big/big_decimal"
)
subject.catch(source).should be_valid
end

it "reports if there are a duplicated requires" do
source = Source.new %(
require "big"
require "math"
require "big"
)
subject.catch(source).should_not be_valid
end

it "reports rule, pos and message" do
source = Source.new %(
require "./thing"
require "./thing"
require "./another_thing"
require "./another_thing"
), "source.cr"

subject.catch(source).should_not be_valid

issue = source.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:1"
issue.end_location.to_s.should eq ""
issue.message.should eq "Duplicated require of `./thing`"

issue = source.issues.last
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:4:1"
issue.end_location.to_s.should eq ""
issue.message.should eq "Duplicated require of `./another_thing`"
end
end
end
2 changes: 1 addition & 1 deletion spec/ameba/rule/lint/redundant_with_object_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ module Ameba::Rule::Lint
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:14"
issue.end_location.to_s.should eq "source.cr:2:30"
issue.message.should eq "Use each instead of each_with_object"
issue.message.should eq "Use `each` instead of `each_with_object`"
end
end
end
Loading

0 comments on commit d8c32f0

Please sign in to comment.