Use more natural way to define error with custom loc

This commit is contained in:
Vitalii Elenhaupt 2018-01-28 20:06:04 +02:00 committed by V. Elenhaupt
parent 1d436fbb94
commit 55b66e7975
9 changed files with 22 additions and 27 deletions

View file

@ -18,7 +18,7 @@ module Ameba::Formatter
context "when problems found" do
it "reports an error" do
s = Source.new "a = 1", "source.cr"
s.error DummyRule.new, s.location(1, 2), "message"
s.error DummyRule.new, 1, 2, "message"
subject = flycheck
subject.source_finished s
subject.output.to_s.should eq "source.cr:1:2: E: message\n"
@ -26,7 +26,7 @@ module Ameba::Formatter
it "properly reports multi-line message" do
s = Source.new "a = 1", "source.cr"
s.error DummyRule.new, s.location(1, 2), "multi\nline"
s.error DummyRule.new, 1, 2, "multi\nline"
subject = flycheck
subject.source_finished s
subject.output.to_s.should eq "source.cr:1:2: E: multi line\n"

View file

@ -6,7 +6,7 @@ module Ameba
formatter = Formatter::TODOFormatter.new IO::Memory.new, file
s = Source.new "a = 1", "source.cr"
s.error DummyRule.new, s.location(1, 2), "message"
s.error DummyRule.new, 1, 2, "message"
formatter.finished [s]
file.to_s
@ -57,7 +57,7 @@ module Ameba
formatter = Formatter::TODOFormatter.new IO::Memory.new, file
s = Source.new "def invalid_syntax"
s.error Rule::Syntax.new, s.location(1, 2), "message"
s.error Rule::Syntax.new, 1, 2, "message"
formatter.finished [s]
content = file.to_s

View file

@ -14,7 +14,7 @@ module Ameba
describe "#error" do
it "adds and error" do
s = Source.new "", "source.cr"
s.error(DummyRule.new, s.location(23, 2), "Error!")
s.error(DummyRule.new, 23, 2, "Error!")
s.should_not be_valid
error = s.errors.first

View file

@ -13,8 +13,7 @@ module Ameba
struct ErrorRule < Rule::Base
def test(source)
source.error self, source.location(1, 1),
"This rule always adds an error"
source.error self, 1, 1, "This rule always adds an error"
end
end

View file

@ -20,8 +20,7 @@ module Ameba::Rule
source.lines.each_with_index do |line, index|
next unless line.size > max_length
source.error self, source.location(index + 1, line.size),
"Line too long"
source.error self, index + 1, line.size, "Line too long"
end
end
end

View file

@ -23,8 +23,7 @@ module Ameba::Rule
def test(source)
source.ast
rescue e : Crystal::SyntaxException
location = source.location(e.line_number, e.column_number)
source.error self, location, e.message.to_s
source.error self, e.line_number, e.column_number, e.message.to_s
end
end
end

View file

@ -15,7 +15,7 @@ module Ameba::Rule
def test(source)
if source.lines.size > 1 && source.lines[-2, 2].join.strip.empty?
source.error self, source.location(source.lines.size, 1),
source.error self, source.lines.size, 1,
"Blank lines detected at the end of the file"
end
end

View file

@ -16,8 +16,7 @@ module Ameba::Rule
def test(source)
source.lines.each_with_index do |line, index|
next unless line =~ /\s$/
source.error self, source.location(index + 1, line.size),
"Trailing whitespace detected"
source.error self, index + 1, line.size, "Trailing whitespace detected"
end
end
end

View file

@ -36,7 +36,7 @@ module Ameba
def initialize(@code : String, @path = "")
end
# Add new error to the list of errors.
# Adds new error to the list of errors.
#
# ```
# source.error rule, location, "Line too long"
@ -46,6 +46,17 @@ module Ameba
errors << Error.new rule, location, message
end
# Adds new error to the list of errors using line and column number.
#
# ```
# source.error rule, line_number, column_number, "Bad code"
# ```
#
def error(rule : Rule::Base, l : Int32, c : Int32, message : String)
location = Crystal::Location.new path, l, c
error rule, location, message
end
# Indicates whether source is valid or not.
# Returns true if the list or errors empty, false otherwise.
#
@ -89,18 +100,6 @@ module Ameba
.parse
end
# Returns a new instance of the `Crystal::Location` in current
# source based on the line number `l` and column number `c`.
#
# ```
# s = Ameba::Source.new code, path
# s.location(3, 76)
# ```
#
def location(l, c)
Crystal::Location.new path, l, c
end
def fullpath
@fullpath ||= File.expand_path @path
end