mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
parent
b225b17b5b
commit
9e2ab9c002
2 changed files with 16 additions and 5 deletions
|
@ -35,7 +35,16 @@ module Ameba::Rule
|
||||||
error = s.errors.first
|
error = s.errors.first
|
||||||
error.rule.should_not be_nil
|
error.rule.should_not be_nil
|
||||||
error.location.to_s.should eq "source.cr:2:13"
|
error.location.to_s.should eq "source.cr:2:13"
|
||||||
error.message.should eq "Duplicated keys in hash literal."
|
error.message.should eq %(Duplicated keys in hash literal: "a")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "reports multiple duplicated keys" do
|
||||||
|
s = Source.new %q(
|
||||||
|
h = {"key1" => 1, "key1" => 2, "key2" => 3, "key2" => 4}
|
||||||
|
)
|
||||||
|
subject.catch(s).should_not be_valid
|
||||||
|
error = s.errors.first
|
||||||
|
error.message.should eq %(Duplicated keys in hash literal: "key1","key2")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -30,15 +30,17 @@ module Ameba::Rule
|
||||||
end
|
end
|
||||||
|
|
||||||
def test(source, node : Crystal::HashLiteral)
|
def test(source, node : Crystal::HashLiteral)
|
||||||
return unless duplicated_keys?(node.entries)
|
return unless (keys = duplicated_keys(node.entries)).any?
|
||||||
|
|
||||||
source.error self, node.location, "Duplicated keys in hash literal."
|
source.error self, node.location,
|
||||||
|
"Duplicated keys in hash literal: #{keys.join(",")}"
|
||||||
end
|
end
|
||||||
|
|
||||||
private def duplicated_keys?(entries)
|
private def duplicated_keys(entries)
|
||||||
entries.map(&.key)
|
entries.map(&.key)
|
||||||
.group_by(&.itself)
|
.group_by(&.itself)
|
||||||
.any? { |_, v| v.size > 1 }
|
.select { |_, v| v.size > 1 }
|
||||||
|
.map { |k, _| k }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue