mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
parent
7c586eb9e5
commit
9e2d4f1856
22 changed files with 134 additions and 1 deletions
|
@ -8,6 +8,17 @@ module Ameba::Rule
|
||||||
rules.should_not be_nil
|
rules.should_not be_nil
|
||||||
rules.should contain DummyRule
|
rules.should contain DummyRule
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "contains rules across all the available groups" do
|
||||||
|
Rule.rules.map(&.group_name).uniq!.reject!(&.empty?).sort.should eq %w(
|
||||||
|
Ameba
|
||||||
|
Layout
|
||||||
|
Lint
|
||||||
|
Metrics
|
||||||
|
Performance
|
||||||
|
Style
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "properties" do
|
context "properties" do
|
||||||
|
|
|
@ -22,6 +22,13 @@ module Ameba::Rule::Performance
|
||||||
subject.catch(source).should_not be_valid
|
subject.catch(source).should_not be_valid
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "does not report if source is a spec" do
|
||||||
|
source = Source.new %(
|
||||||
|
[1, 2, 3].select { |e| e > 2 }.any?
|
||||||
|
), "source_spec.cr"
|
||||||
|
subject.catch(source).should be_valid
|
||||||
|
end
|
||||||
|
|
||||||
it "reports if there is reject followed by any? without a block" do
|
it "reports if there is reject followed by any? without a block" do
|
||||||
source = Source.new %(
|
source = Source.new %(
|
||||||
[1, 2, 3].reject { |e| e > 2 }.any?
|
[1, 2, 3].reject { |e| e > 2 }.any?
|
||||||
|
|
|
@ -21,6 +21,13 @@ module Ameba::Rule::Performance
|
||||||
subject.catch(source).should_not be_valid
|
subject.catch(source).should_not be_valid
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "does not report if source is a spec" do
|
||||||
|
source = Source.new %(
|
||||||
|
[1, 2, 3].any?
|
||||||
|
), "source_spec.cr"
|
||||||
|
subject.catch(source).should be_valid
|
||||||
|
end
|
||||||
|
|
||||||
context "macro" do
|
context "macro" do
|
||||||
it "reports in macro scope" do
|
it "reports in macro scope" do
|
||||||
source = Source.new %(
|
source = Source.new %(
|
||||||
|
|
19
spec/ameba/rule/performance/base_spec.cr
Normal file
19
spec/ameba/rule/performance/base_spec.cr
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
require "../../../spec_helper"
|
||||||
|
|
||||||
|
module Ameba::Rule::Performance
|
||||||
|
describe Base do
|
||||||
|
subject = PerfRule.new
|
||||||
|
|
||||||
|
describe "#catch" do
|
||||||
|
it "ignores spec files" do
|
||||||
|
source = Source.new("", "source_spec.cr")
|
||||||
|
subject.catch(source).should be_valid
|
||||||
|
end
|
||||||
|
|
||||||
|
it "reports perf issues for non-spec files" do
|
||||||
|
source = Source.new("", "source.cr")
|
||||||
|
subject.catch(source).should_not be_valid
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -23,6 +23,13 @@ module Ameba::Rule::Performance
|
||||||
subject.catch(source).should_not be_valid
|
subject.catch(source).should_not be_valid
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "does not report if source is a spec" do
|
||||||
|
source = Source.new %(
|
||||||
|
[1, 2, 3].select { |e| e > 1 }.reverse
|
||||||
|
), "source_spec.cr"
|
||||||
|
subject.catch(source).should be_valid
|
||||||
|
end
|
||||||
|
|
||||||
it "reports if there is select followed by reverse followed by other call" do
|
it "reports if there is select followed by reverse followed by other call" do
|
||||||
source = Source.new %(
|
source = Source.new %(
|
||||||
[1, 2, 3].select { |e| e > 2 }.reverse.size
|
[1, 2, 3].select { |e| e > 2 }.reverse.size
|
||||||
|
|
|
@ -25,6 +25,13 @@ module Ameba::Rule::Performance
|
||||||
subject.catch(source).should_not be_valid
|
subject.catch(source).should_not be_valid
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "does not report if source is a spec" do
|
||||||
|
source = Source.new %(
|
||||||
|
(1..3).map(&.itself).compact
|
||||||
|
), "source_spec.cr"
|
||||||
|
subject.catch(source).should be_valid
|
||||||
|
end
|
||||||
|
|
||||||
context "macro" do
|
context "macro" do
|
||||||
it "doesn't report in macro scope" do
|
it "doesn't report in macro scope" do
|
||||||
source = Source.new %(
|
source = Source.new %(
|
||||||
|
|
|
@ -22,6 +22,13 @@ module Ameba::Rule::Performance
|
||||||
subject.catch(source).should_not be_valid
|
subject.catch(source).should_not be_valid
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "does not report if source is a spec" do
|
||||||
|
source = Source.new %(
|
||||||
|
[1, 2, 3].select { |e| e > 2 }.last
|
||||||
|
), "source_spec.cr"
|
||||||
|
subject.catch(source).should be_valid
|
||||||
|
end
|
||||||
|
|
||||||
it "reports if there is select followed by last?" do
|
it "reports if there is select followed by last?" do
|
||||||
source = Source.new %(
|
source = Source.new %(
|
||||||
[1, 2, 3].select { |e| e > 2 }.last?
|
[1, 2, 3].select { |e| e > 2 }.last?
|
||||||
|
|
|
@ -18,6 +18,13 @@ module Ameba::Rule::Performance
|
||||||
subject.catch(source).should_not be_valid
|
subject.catch(source).should_not be_valid
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "does not report is source is a spec" do
|
||||||
|
source = Source.new %(
|
||||||
|
%w[Alice Bob].map(&.chars).flatten
|
||||||
|
), "source_spec.cr"
|
||||||
|
subject.catch(source).should be_valid
|
||||||
|
end
|
||||||
|
|
||||||
context "macro" do
|
context "macro" do
|
||||||
it "doesn't report in macro scope" do
|
it "doesn't report in macro scope" do
|
||||||
source = Source.new %(
|
source = Source.new %(
|
||||||
|
|
|
@ -19,6 +19,13 @@ module Ameba::Rule::Performance
|
||||||
subject.catch(source).should_not be_valid
|
subject.catch(source).should_not be_valid
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "does not report if source is a spec" do
|
||||||
|
source = Source.new %(
|
||||||
|
(1..3).map(&.to_s).join
|
||||||
|
), "source_spec.cr"
|
||||||
|
subject.catch(source).should be_valid
|
||||||
|
end
|
||||||
|
|
||||||
it "reports if there is map followed by sum without a block (with argument)" do
|
it "reports if there is map followed by sum without a block (with argument)" do
|
||||||
source = Source.new %(
|
source = Source.new %(
|
||||||
(1..3).map(&.to_u64).sum(0)
|
(1..3).map(&.to_u64).sum(0)
|
||||||
|
|
|
@ -24,6 +24,13 @@ module Ameba::Rule::Performance
|
||||||
subject.catch(source).should_not be_valid
|
subject.catch(source).should_not be_valid
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "does not report if source is a spec" do
|
||||||
|
source = Source.new %(
|
||||||
|
[1, 2, 3].select { |e| e > 2 }.size
|
||||||
|
), "source_spec.cr"
|
||||||
|
subject.catch(source).should be_valid
|
||||||
|
end
|
||||||
|
|
||||||
it "reports if there is a reject followed by size" do
|
it "reports if there is a reject followed by size" do
|
||||||
source = Source.new %(
|
source = Source.new %(
|
||||||
[1, 2, 3].reject { |e| e < 2 }.size
|
[1, 2, 3].reject { |e| e < 2 }.size
|
||||||
|
|
|
@ -7,6 +7,7 @@ module Ameba
|
||||||
config.globs = files
|
config.globs = files
|
||||||
|
|
||||||
config.update_rule ErrorRule.rule_name, enabled: false
|
config.update_rule ErrorRule.rule_name, enabled: false
|
||||||
|
config.update_rule PerfRule.rule_name, enabled: false
|
||||||
|
|
||||||
Runner.new(config)
|
Runner.new(config)
|
||||||
end
|
end
|
||||||
|
|
|
@ -107,6 +107,16 @@ module Ameba
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class PerfRule < Rule::Performance::Base
|
||||||
|
properties do
|
||||||
|
description : String = "Sample performance rule"
|
||||||
|
end
|
||||||
|
|
||||||
|
def test(source)
|
||||||
|
issue_for({1, 1}, "Poor performance")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class DummyFormatter < Formatter::BaseFormatter
|
class DummyFormatter < Formatter::BaseFormatter
|
||||||
property started_sources : Array(Source)?
|
property started_sources : Array(Source)?
|
||||||
property finished_sources : Array(Source)?
|
property finished_sources : Array(Source)?
|
||||||
|
|
|
@ -128,6 +128,16 @@ module Ameba::Rule
|
||||||
{{ @type.subclasses }}
|
{{ @type.subclasses }}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
protected def self.abstract?
|
||||||
|
{{ @type.abstract? }}
|
||||||
|
end
|
||||||
|
|
||||||
|
protected def self.inherited_rules
|
||||||
|
subclasses.each_with_object([] of Base.class) do |klass, obj|
|
||||||
|
klass.abstract? ? obj.concat(klass.inherited_rules) : (obj << klass)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
macro inherited
|
macro inherited
|
||||||
protected def self.path_to_source_file
|
protected def self.path_to_source_file
|
||||||
__FILE__
|
__FILE__
|
||||||
|
@ -188,6 +198,6 @@ module Ameba::Rule
|
||||||
# Ameba::Rule.rules # => [Rule1, Rule2, ....]
|
# Ameba::Rule.rules # => [Rule1, Rule2, ....]
|
||||||
# ```
|
# ```
|
||||||
def self.rules
|
def self.rules
|
||||||
Base.subclasses
|
Base.inherited_rules
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
require "./base"
|
||||||
|
|
||||||
module Ameba::Rule::Performance
|
module Ameba::Rule::Performance
|
||||||
# This rule is used to identify usage of `any?` calls that follow filters.
|
# This rule is used to identify usage of `any?` calls that follow filters.
|
||||||
#
|
#
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
require "./base"
|
||||||
|
|
||||||
module Ameba::Rule::Performance
|
module Ameba::Rule::Performance
|
||||||
# This rule is used to identify usage of arg-less `Enumerable#any?` calls.
|
# This rule is used to identify usage of arg-less `Enumerable#any?` calls.
|
||||||
#
|
#
|
||||||
|
|
10
src/ameba/rule/performance/base.cr
Normal file
10
src/ameba/rule/performance/base.cr
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
require "../base"
|
||||||
|
|
||||||
|
module Ameba::Rule::Performance
|
||||||
|
# A general base class for performance rules.
|
||||||
|
abstract class Base < Ameba::Rule::Base
|
||||||
|
def catch(source : Source)
|
||||||
|
source.spec? ? source : super
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,3 +1,5 @@
|
||||||
|
require "./base"
|
||||||
|
|
||||||
module Ameba::Rule::Performance
|
module Ameba::Rule::Performance
|
||||||
# This rule is used to identify usage of chained calls not utilizing
|
# This rule is used to identify usage of chained calls not utilizing
|
||||||
# the bang method variants.
|
# the bang method variants.
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
require "./base"
|
||||||
|
|
||||||
module Ameba::Rule::Performance
|
module Ameba::Rule::Performance
|
||||||
# This rule is used to identify usage of `compact` calls that follow `map`.
|
# This rule is used to identify usage of `compact` calls that follow `map`.
|
||||||
#
|
#
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
require "./base"
|
||||||
|
|
||||||
module Ameba::Rule::Performance
|
module Ameba::Rule::Performance
|
||||||
# This rule is used to identify usage of `first/last/first?/last?` calls that follow filters.
|
# This rule is used to identify usage of `first/last/first?/last?` calls that follow filters.
|
||||||
#
|
#
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
require "./base"
|
||||||
|
|
||||||
module Ameba::Rule::Performance
|
module Ameba::Rule::Performance
|
||||||
# This rule is used to identify usage of `flatten` calls that follow `map`.
|
# This rule is used to identify usage of `flatten` calls that follow `map`.
|
||||||
#
|
#
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
require "./base"
|
||||||
|
|
||||||
module Ameba::Rule::Performance
|
module Ameba::Rule::Performance
|
||||||
# This rule is used to identify usage of `sum/product` calls
|
# This rule is used to identify usage of `sum/product` calls
|
||||||
# that follow `map`.
|
# that follow `map`.
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
require "./base"
|
||||||
|
|
||||||
module Ameba::Rule::Performance
|
module Ameba::Rule::Performance
|
||||||
# This rule is used to identify usage of `size` calls that follow filter.
|
# This rule is used to identify usage of `size` calls that follow filter.
|
||||||
#
|
#
|
||||||
|
|
Loading…
Reference in a new issue