mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Rework breaking specs, break backward compatibility
This commit is contained in:
parent
1f4dda1c4a
commit
8d4730182f
8 changed files with 94 additions and 197 deletions
|
@ -15,6 +15,6 @@ scripts:
|
||||||
executables:
|
executables:
|
||||||
- ameba
|
- ameba
|
||||||
|
|
||||||
crystal: ">= 0.36.0"
|
crystal: ">= 1.4.0"
|
||||||
|
|
||||||
license: MIT
|
license: MIT
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
require "../../../spec_helper"
|
require "../../../spec_helper"
|
||||||
require "semantic_version"
|
|
||||||
|
|
||||||
module Ameba::Rule::Lint
|
module Ameba::Rule::Lint
|
||||||
describe EmptyEnsure do
|
describe EmptyEnsure do
|
||||||
subject = EmptyEnsure.new
|
subject = EmptyEnsure.new
|
||||||
|
|
||||||
it "passes if there is no empty ensure blocks" do
|
it "passes if there is no empty ensure blocks" do
|
||||||
s = Source.new %(
|
expect_no_issues subject, <<-CRYSTAL
|
||||||
def some_method
|
def some_method
|
||||||
do_some_stuff
|
do_some_stuff
|
||||||
ensure
|
ensure
|
||||||
|
@ -24,53 +23,30 @@ module Ameba::Rule::Lint
|
||||||
ensure
|
ensure
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
)
|
CRYSTAL
|
||||||
subject.catch(s).should be_valid
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails if there is an empty ensure in method" do
|
it "fails if there is an empty ensure in method" do
|
||||||
s = Source.new %(
|
expect_issue subject, <<-CRYSTAL
|
||||||
def method
|
def method
|
||||||
do_some_stuff
|
do_some_stuff
|
||||||
ensure
|
ensure
|
||||||
|
# ^^^^^^ error: Empty `ensure` block detected
|
||||||
end
|
end
|
||||||
)
|
CRYSTAL
|
||||||
subject.catch(s).should_not be_valid
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails if there is an empty ensure in a block" do
|
it "fails if there is an empty ensure in a block" do
|
||||||
s = Source.new %(
|
expect_issue subject, <<-CRYSTAL
|
||||||
begin
|
|
||||||
do_some_stuff
|
|
||||||
ensure
|
|
||||||
# nothing here
|
|
||||||
end
|
|
||||||
)
|
|
||||||
subject.catch(s).should_not be_valid
|
|
||||||
end
|
|
||||||
|
|
||||||
it "reports rule, pos and message" do
|
|
||||||
s = Source.new %(
|
|
||||||
begin
|
begin
|
||||||
do_some_stuff
|
do_some_stuff
|
||||||
rescue
|
rescue
|
||||||
do_some_other_stuff
|
do_some_other_stuff
|
||||||
ensure
|
ensure
|
||||||
|
# ^^^^^^ error: Empty `ensure` block detected
|
||||||
|
# nothing here
|
||||||
end
|
end
|
||||||
), "source.cr"
|
CRYSTAL
|
||||||
subject.catch(s).should_not be_valid
|
|
||||||
issue = s.issues.first
|
|
||||||
|
|
||||||
issue.rule.should_not be_nil
|
|
||||||
# TODO: refactor this in next release
|
|
||||||
# Crystal 1.4 changes the start location of Crystal::ExceptionHandler
|
|
||||||
if SemanticVersion.parse(Crystal::VERSION) <= SemanticVersion.parse("1.3.2")
|
|
||||||
issue.location.to_s.should eq "source.cr:2:3"
|
|
||||||
else
|
|
||||||
issue.location.to_s.should eq "source.cr:1:1"
|
|
||||||
end
|
|
||||||
issue.end_location.to_s.should eq "source.cr:6:3"
|
|
||||||
issue.message.should eq "Empty `ensure` block detected"
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,18 +1,11 @@
|
||||||
require "../../../spec_helper"
|
require "../../../spec_helper"
|
||||||
require "semantic_version"
|
|
||||||
|
|
||||||
private def check_shadowed(source, exceptions)
|
|
||||||
s = Ameba::Source.new source
|
|
||||||
Ameba::Rule::Lint::ShadowedException.new.catch(s).should_not be_valid
|
|
||||||
s.issues.first.message.should contain exceptions.join(", ")
|
|
||||||
end
|
|
||||||
|
|
||||||
module Ameba::Rule::Lint
|
module Ameba::Rule::Lint
|
||||||
describe ShadowedException do
|
describe ShadowedException do
|
||||||
subject = ShadowedException.new
|
subject = ShadowedException.new
|
||||||
|
|
||||||
it "passes if there isn't shadowed exception" do
|
it "passes if there isn't shadowed exception" do
|
||||||
s = Source.new %(
|
expect_no_issues subject, <<-CRYSTAL
|
||||||
def method
|
def method
|
||||||
do_something
|
do_something
|
||||||
rescue ArgumentError
|
rescue ArgumentError
|
||||||
|
@ -32,152 +25,158 @@ module Ameba::Rule::Lint
|
||||||
rescue e : Exception
|
rescue e : Exception
|
||||||
handle_exception
|
handle_exception
|
||||||
end
|
end
|
||||||
)
|
CRYSTAL
|
||||||
subject.catch(s).should be_valid
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails if there is a shadowed exception" do
|
it "fails if there is a shadowed exception" do
|
||||||
check_shadowed %(
|
expect_issue subject, <<-CRYSTAL
|
||||||
begin
|
begin
|
||||||
do_something
|
do_something
|
||||||
rescue Exception
|
rescue Exception
|
||||||
handle_exception
|
handle_exception
|
||||||
rescue ArgumentError
|
rescue ArgumentError
|
||||||
|
# ^^^^^^^^^^^^^ error: Shadowed exception found: ArgumentError
|
||||||
handle_argument_error_exception
|
handle_argument_error_exception
|
||||||
end
|
end
|
||||||
), %w(ArgumentError)
|
CRYSTAL
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails if there is a custom shadowed exceptions" do
|
it "fails if there is a custom shadowed exceptions" do
|
||||||
check_shadowed %(
|
expect_issue subject, <<-CRYSTAL
|
||||||
begin
|
begin
|
||||||
1
|
1
|
||||||
rescue Exception
|
rescue Exception
|
||||||
2
|
2
|
||||||
rescue MySuperException
|
rescue MySuperException
|
||||||
|
# ^^^^^^^^^^^^^^^^ error: Shadowed exception found: MySuperException
|
||||||
3
|
3
|
||||||
end
|
end
|
||||||
), %w(MySuperException)
|
CRYSTAL
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails if there is a shadowed exception in a type list" do
|
it "fails if there is a shadowed exception in a type list" do
|
||||||
check_shadowed %(
|
expect_issue subject, <<-CRYSTAL
|
||||||
begin
|
begin
|
||||||
rescue Exception | IndexError
|
rescue Exception | IndexError
|
||||||
|
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
|
||||||
end
|
end
|
||||||
), %w(IndexError)
|
CRYSTAL
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails if there is a first shadowed exception in a type list" do
|
it "fails if there is a first shadowed exception in a type list" do
|
||||||
check_shadowed %(
|
expect_issue subject, <<-CRYSTAL
|
||||||
begin
|
begin
|
||||||
rescue IndexError | Exception
|
rescue IndexError | Exception
|
||||||
|
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
|
||||||
rescue Exception
|
rescue Exception
|
||||||
|
# ^^^^^^^^^ error: Shadowed exception found: Exception
|
||||||
rescue
|
rescue
|
||||||
end
|
end
|
||||||
), %w(IndexError)
|
CRYSTAL
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails if there is a shadowed duplicated exception" do
|
it "fails if there is a shadowed duplicated exception" do
|
||||||
check_shadowed %(
|
expect_issue subject, <<-CRYSTAL
|
||||||
begin
|
begin
|
||||||
rescue IndexError
|
rescue IndexError
|
||||||
rescue ArgumentError
|
rescue ArgumentError
|
||||||
rescue IndexError
|
rescue IndexError
|
||||||
|
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
|
||||||
end
|
end
|
||||||
), %w(IndexError)
|
CRYSTAL
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails if there is a shadowed duplicated exception in a type list" do
|
it "fails if there is a shadowed duplicated exception in a type list" do
|
||||||
check_shadowed %(
|
expect_issue subject, <<-CRYSTAL
|
||||||
begin
|
begin
|
||||||
rescue IndexError
|
rescue IndexError
|
||||||
rescue ArgumentError | IndexError
|
rescue ArgumentError | IndexError
|
||||||
|
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
|
||||||
end
|
end
|
||||||
), %w(IndexError)
|
CRYSTAL
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails if there is only shadowed duplicated exceptions" do
|
it "fails if there is only shadowed duplicated exceptions" do
|
||||||
check_shadowed %(
|
expect_issue subject, <<-CRYSTAL
|
||||||
begin
|
begin
|
||||||
rescue IndexError
|
rescue IndexError
|
||||||
rescue IndexError
|
rescue IndexError
|
||||||
|
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
|
||||||
|
rescue Exception
|
||||||
end
|
end
|
||||||
), %w(IndexError)
|
CRYSTAL
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails if there is only shadowed duplicated exceptions in a type list" do
|
it "fails if there is only shadowed duplicated exceptions in a type list" do
|
||||||
check_shadowed %(
|
expect_issue subject, <<-CRYSTAL
|
||||||
begin
|
begin
|
||||||
rescue IndexError | IndexError
|
rescue IndexError | IndexError
|
||||||
|
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
|
||||||
end
|
end
|
||||||
), %w(IndexError)
|
CRYSTAL
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails if all rescues are shadowed and there is a catch-all rescue" do
|
it "fails if all rescues are shadowed and there is a catch-all rescue" do
|
||||||
check_shadowed %(
|
expect_issue subject, <<-CRYSTAL
|
||||||
begin
|
begin
|
||||||
rescue Exception
|
rescue Exception
|
||||||
rescue ArgumentError
|
rescue ArgumentError
|
||||||
|
# ^^^^^^^^^^^^^ error: Shadowed exception found: ArgumentError
|
||||||
rescue IndexError
|
rescue IndexError
|
||||||
|
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
|
||||||
rescue KeyError | IO::Error
|
rescue KeyError | IO::Error
|
||||||
|
# ^^^^^^^^^ error: Shadowed exception found: IO::Error
|
||||||
|
# ^^^^^^^^ error: Shadowed exception found: KeyError
|
||||||
rescue
|
rescue
|
||||||
end
|
end
|
||||||
), %w(IndexError KeyError IO::Error)
|
CRYSTAL
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails if there are shadowed exception with args" do
|
it "fails if there are shadowed exception with args" do
|
||||||
check_shadowed %(
|
expect_issue subject, <<-CRYSTAL
|
||||||
begin
|
begin
|
||||||
rescue Exception
|
rescue Exception
|
||||||
rescue ex : IndexError
|
rescue ex : IndexError
|
||||||
|
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
|
||||||
rescue
|
rescue
|
||||||
end
|
end
|
||||||
), %w(IndexError)
|
CRYSTAL
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails if there are multiple shadowed exceptions" do
|
it "fails if there are multiple shadowed exceptions" do
|
||||||
check_shadowed %(
|
expect_issue subject, <<-CRYSTAL
|
||||||
begin
|
begin
|
||||||
rescue Exception
|
rescue Exception
|
||||||
rescue ArgumentError
|
rescue ArgumentError
|
||||||
|
# ^^^^^^^^^^^^^ error: Shadowed exception found: ArgumentError
|
||||||
rescue IndexError
|
rescue IndexError
|
||||||
|
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
|
||||||
end
|
end
|
||||||
), %w(ArgumentError IndexError)
|
CRYSTAL
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails if there are multiple shadowed exceptions in a type list" do
|
it "fails if there are multiple shadowed exceptions in a type list" do
|
||||||
check_shadowed %(
|
expect_issue subject, <<-CRYSTAL
|
||||||
begin
|
begin
|
||||||
rescue Exception
|
rescue Exception
|
||||||
rescue ArgumentError | IndexError
|
rescue ArgumentError | IndexError
|
||||||
|
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
|
||||||
|
# ^^^^^^^^^^^^^ error: Shadowed exception found: ArgumentError
|
||||||
rescue IO::Error
|
rescue IO::Error
|
||||||
|
# ^^^^^^^^^ error: Shadowed exception found: IO::Error
|
||||||
end
|
end
|
||||||
), %w(ArgumentError IndexError IO::Error)
|
CRYSTAL
|
||||||
end
|
end
|
||||||
|
|
||||||
it "reports rule, location and a message" do
|
it "fails if there are multiple shadowed exceptions in a single rescue" do
|
||||||
s = Source.new %q(
|
expect_issue subject, <<-CRYSTAL
|
||||||
begin
|
begin
|
||||||
do_something
|
do_something
|
||||||
rescue Exception | IndexError
|
rescue Exception | IndexError | ArgumentError
|
||||||
|
# ^^^^^^^^^^^^^ error: Shadowed exception found: ArgumentError
|
||||||
|
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
|
||||||
end
|
end
|
||||||
), "source.cr"
|
CRYSTAL
|
||||||
subject.catch(s).should_not be_valid
|
|
||||||
issue = s.issues.first
|
|
||||||
|
|
||||||
issue.rule.should_not be_nil
|
|
||||||
# TODO: refactor this in next release
|
|
||||||
# Crystal 1.4 changes the start location of Crystal::ExceptionHandler
|
|
||||||
if SemanticVersion.parse(Crystal::VERSION) <= SemanticVersion.parse("1.3.2")
|
|
||||||
issue.location.to_s.should eq "source.cr:2:3"
|
|
||||||
else
|
|
||||||
issue.location.to_s.should eq "source.cr:1:1"
|
|
||||||
end
|
|
||||||
issue.end_location.to_s.should eq "source.cr:4:3"
|
|
||||||
issue.message.should eq(
|
|
||||||
"Exception handler has shadowed exceptions: IndexError"
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -194,10 +194,7 @@ module Ameba::AST::Util
|
||||||
return 0 unless node.responds_to?(:name) && (name = node.name)
|
return 0 unless node.responds_to?(:name) && (name = node.name)
|
||||||
|
|
||||||
case name
|
case name
|
||||||
when Crystal::ASTNode then name.name_size
|
when Crystal::ASTNode then name.name_size
|
||||||
# TODO: remove in a next release
|
|
||||||
# (preserves backward compatibility of crystal <= 1.3.2 )
|
|
||||||
when Symbol then name.to_s.size # Crystal::MagicConstant
|
|
||||||
when Crystal::Token::Kind then name.to_s.size # Crystal::MagicConstant
|
when Crystal::Token::Kind then name.to_s.size # Crystal::MagicConstant
|
||||||
else name.size
|
else name.size
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,81 +0,0 @@
|
||||||
# TODO: remove in a next release
|
|
||||||
# (preserves backward compatibility of crystal <= 1.3.2 )
|
|
||||||
{% if compare_versions(Crystal::VERSION, "1.3.2") < 1 %}
|
|
||||||
struct Symbol
|
|
||||||
def comment?
|
|
||||||
self == :COMMENT
|
|
||||||
end
|
|
||||||
|
|
||||||
def delimiter_start?
|
|
||||||
self == :DELIMITER_START
|
|
||||||
end
|
|
||||||
|
|
||||||
def delimiter_end?
|
|
||||||
self == :DELIMITER_END
|
|
||||||
end
|
|
||||||
|
|
||||||
def interpolation_start?
|
|
||||||
self == :INTERPOLATION_START
|
|
||||||
end
|
|
||||||
|
|
||||||
def string_array_start?
|
|
||||||
self == :STRING_ARRAY_START
|
|
||||||
end
|
|
||||||
|
|
||||||
def string_array_end?
|
|
||||||
self == :STRING_ARRAY_END
|
|
||||||
end
|
|
||||||
|
|
||||||
def symbol_array_start?
|
|
||||||
self == :SYMBOL_ARRAY_START
|
|
||||||
end
|
|
||||||
|
|
||||||
def eof?
|
|
||||||
self == :EOF
|
|
||||||
end
|
|
||||||
|
|
||||||
def op_rcurly?
|
|
||||||
self == :"}"
|
|
||||||
end
|
|
||||||
|
|
||||||
def begin?
|
|
||||||
self == :begin
|
|
||||||
end
|
|
||||||
|
|
||||||
def op_minus_gt?
|
|
||||||
self == :"->"
|
|
||||||
end
|
|
||||||
|
|
||||||
def ident?
|
|
||||||
self == :IDENT
|
|
||||||
end
|
|
||||||
|
|
||||||
def op_lparen?
|
|
||||||
self == :"("
|
|
||||||
end
|
|
||||||
|
|
||||||
def op_rparen?
|
|
||||||
self == :")"
|
|
||||||
end
|
|
||||||
|
|
||||||
def newline?
|
|
||||||
self == :NEWLINE
|
|
||||||
end
|
|
||||||
|
|
||||||
def space?
|
|
||||||
self == :SPACE
|
|
||||||
end
|
|
||||||
|
|
||||||
def number?
|
|
||||||
self == :NUMBER
|
|
||||||
end
|
|
||||||
|
|
||||||
def string?
|
|
||||||
self == :STRING
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
struct Crystal::Token::Kind
|
|
||||||
#
|
|
||||||
end
|
|
||||||
{% end %}
|
|
|
@ -49,7 +49,7 @@ module Ameba::Rule::Lint
|
||||||
node_ensure = node.ensure
|
node_ensure = node.ensure
|
||||||
return if node_ensure.nil? || !node_ensure.nop?
|
return if node_ensure.nil? || !node_ensure.nop?
|
||||||
|
|
||||||
issue_for node, MSG
|
issue_for node.ensure_location, node.end_location, MSG
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -38,38 +38,44 @@ module Ameba::Rule::Lint
|
||||||
description "Disallows rescued exception that get shadowed"
|
description "Disallows rescued exception that get shadowed"
|
||||||
end
|
end
|
||||||
|
|
||||||
MSG = "Exception handler has shadowed exceptions: %s"
|
MSG = "Shadowed exception found: %s"
|
||||||
|
|
||||||
def test(source, node : Crystal::ExceptionHandler)
|
def test(source, node : Crystal::ExceptionHandler)
|
||||||
return unless excs = node.rescues.try &.map(&.types)
|
rescues = node.rescues
|
||||||
return if (excs = shadowed excs).empty?
|
|
||||||
|
|
||||||
issue_for node, MSG % excs.join(", ")
|
return if rescues.nil?
|
||||||
|
|
||||||
|
shadowed(rescues).each { |tp| issue_for tp, MSG % tp.names.join("::") }
|
||||||
end
|
end
|
||||||
|
|
||||||
private def shadowed(exceptions, exception_found = false)
|
private def shadowed(rescues, catch_all = false)
|
||||||
previous_exceptions = [] of String
|
traversed_types = Set(String).new
|
||||||
|
|
||||||
exceptions.reduce([] of String) do |shadowed, excs|
|
filter_rescues(rescues).each_with_object([] of Crystal::Path) do |types, shadowed|
|
||||||
excs = excs.try(&.map(&.to_s)) || %w[Exception]
|
case
|
||||||
|
when catch_all
|
||||||
if exception_found
|
shadowed.concat(types)
|
||||||
shadowed.concat excs
|
next
|
||||||
previous_exceptions.concat excs
|
when types.any?(&.single?("Exception"))
|
||||||
|
nodes = types.reject(&.single?("Exception"))
|
||||||
|
shadowed.concat(nodes) unless nodes.empty?
|
||||||
|
catch_all = true
|
||||||
|
next
|
||||||
else
|
else
|
||||||
exception_found ||= excs.any? &.== "Exception"
|
nodes = types.select { |tp| traverse(tp.to_s, traversed_types) }
|
||||||
excs.each do |exc|
|
shadowed.concat(nodes) unless nodes.empty?
|
||||||
if exception_found && exc != "Exception"
|
|
||||||
shadowed << exc
|
|
||||||
else
|
|
||||||
shadowed << exc if previous_exceptions.any? &.== exc
|
|
||||||
end
|
|
||||||
previous_exceptions << exc
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
shadowed
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private def filter_rescues(rescues)
|
||||||
|
rescues.compact_map(&.types.try &.select(Crystal::Path))
|
||||||
|
end
|
||||||
|
|
||||||
|
private def traverse(path, traversed_types)
|
||||||
|
dup = traversed_types.includes?(path)
|
||||||
|
dup || (traversed_types << path)
|
||||||
|
dup
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -90,7 +90,7 @@ module Ameba::Rule::Style
|
||||||
end
|
end
|
||||||
|
|
||||||
private def redundant_begin_in_expressions?(node)
|
private def redundant_begin_in_expressions?(node)
|
||||||
!!(node.keyword.try &.begin?)
|
!!node.keyword.try(&.begin?)
|
||||||
end
|
end
|
||||||
|
|
||||||
private def inner_handler?(handler)
|
private def inner_handler?(handler)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue