mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Extend StaticComparison
w/ support of ===
operator
This commit is contained in:
parent
849be52618
commit
ea42911c3c
2 changed files with 17 additions and 9 deletions
|
@ -6,13 +6,21 @@ module Ameba::Rule::Lint
|
||||||
describe StaticComparison do
|
describe StaticComparison do
|
||||||
it "passes for valid cases" do
|
it "passes for valid cases" do
|
||||||
expect_no_issues subject, <<-CRYSTAL
|
expect_no_issues subject, <<-CRYSTAL
|
||||||
/foo/ === "foo"
|
|
||||||
"foo" == foo
|
"foo" == foo
|
||||||
|
"foo" != foo
|
||||||
foo == "foo"
|
foo == "foo"
|
||||||
|
foo != "foo"
|
||||||
CRYSTAL
|
CRYSTAL
|
||||||
end
|
end
|
||||||
|
|
||||||
it "reports if there is a static comparison evaluating to true" do
|
it "reports if there is a static comparison evaluating to the same" do
|
||||||
|
expect_issue subject, <<-CRYSTAL
|
||||||
|
"foo" === "foo"
|
||||||
|
# ^^^^^^^^^^^^^ error: Comparison always evaluates to the same
|
||||||
|
CRYSTAL
|
||||||
|
end
|
||||||
|
|
||||||
|
it "reports if there is a static comparison evaluating to true (2)" do
|
||||||
expect_issue subject, <<-CRYSTAL
|
expect_issue subject, <<-CRYSTAL
|
||||||
"foo" == "foo"
|
"foo" == "foo"
|
||||||
# ^^^^^^^^^^^^ error: Comparison always evaluates to true
|
# ^^^^^^^^^^^^ error: Comparison always evaluates to true
|
||||||
|
|
|
@ -21,7 +21,7 @@ module Ameba::Rule::Lint
|
||||||
description "Identifies static comparisons"
|
description "Identifies static comparisons"
|
||||||
end
|
end
|
||||||
|
|
||||||
OP_NAMES = %w(== !=)
|
OP_NAMES = %w(=== == !=)
|
||||||
MSG = "Comparison always evaluates to %s"
|
MSG = "Comparison always evaluates to %s"
|
||||||
|
|
||||||
PRIMITIVES = {
|
PRIMITIVES = {
|
||||||
|
@ -45,12 +45,12 @@ module Ameba::Rule::Lint
|
||||||
return unless (obj = node.obj) && (arg = node.args.first?)
|
return unless (obj = node.obj) && (arg = node.args.first?)
|
||||||
return unless obj.class.in?(PRIMITIVES) && arg.class.in?(PRIMITIVES)
|
return unless obj.class.in?(PRIMITIVES) && arg.class.in?(PRIMITIVES)
|
||||||
|
|
||||||
case node.name
|
what =
|
||||||
when "=="
|
case node.name
|
||||||
what = (obj.to_s == arg.to_s).to_s
|
when "===" then "the same"
|
||||||
when "!="
|
when "==" then (obj.to_s == arg.to_s).to_s
|
||||||
what = (obj.to_s != arg.to_s).to_s
|
when "!=" then (obj.to_s != arg.to_s).to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
issue_for node, MSG % what
|
issue_for node, MSG % what
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue