Skip to content

Commit

Permalink
Lint/LiteralInInterpolation: properly report node position
Browse files Browse the repository at this point in the history
  • Loading branch information
veelenga committed Mar 27, 2020
1 parent 441e7fa commit 04497fe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
15 changes: 12 additions & 3 deletions spec/ameba/rule/lint/literal_in_interpolation_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/ameba/rule/lint/literal_in_interpolation.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 04497fe

Please sign in to comment.