Lint/LiteralInInterpolation: properly report node position

This commit is contained in:
Vitalii Elenhaupt 2020-03-27 19:44:04 +02:00
parent 441e7fa2bb
commit 04497feeed
No known key found for this signature in database
GPG key ID: CD0BF17825928BC0
2 changed files with 15 additions and 6 deletions

View file

@ -29,13 +29,22 @@ module Ameba::Rule::Lint
end
it "reports rule, pos and message" do
s = Source.new %q("#{4}"), "source.cr"
s = Source.new %q(
"Hello, #{:world} from #{:ameba}"
), "source.cr"
subject.catch(s).should_not be_valid
s.issues.size.should eq 2
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:1:6"
issue.location.to_s.should eq "source.cr:1:11"
issue.end_location.to_s.should eq "source.cr:1:16"
issue.message.should eq "Literal value found in interpolation"
issue = s.issues.last
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:26"
issue.end_location.to_s.should eq "source.cr:1:31"
issue.message.should eq "Literal value found in interpolation"
end
end

View file

@ -26,9 +26,9 @@ module Ameba::Rule::Lint
MSG = "Literal value found in interpolation"
def test(source, node : Crystal::StringInterpolation)
found = node.expressions.any? { |e| !e.is_a?(Crystal::StringLiteral) && literal?(e) }
return unless found
issue_for node, MSG
node.expressions
.select { |e| !e.is_a?(Crystal::StringLiteral) && literal?(e) }
.each { |n| issue_for n, MSG }
end
end
end