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 context "when problems found" do
it "reports an error" do it "reports an error" do
s = Source.new "a = 1", "source.cr" 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 = flycheck
subject.source_finished s subject.source_finished s
subject.output.to_s.should eq "source.cr:1:2: E: message\n" 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 it "properly reports multi-line message" do
s = Source.new "a = 1", "source.cr" 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 = flycheck
subject.source_finished s subject.source_finished s
subject.output.to_s.should eq "source.cr:1:2: E: multi line\n" 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 formatter = Formatter::TODOFormatter.new IO::Memory.new, file
s = Source.new "a = 1", "source.cr" 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] formatter.finished [s]
file.to_s file.to_s
@ -57,7 +57,7 @@ module Ameba
formatter = Formatter::TODOFormatter.new IO::Memory.new, file formatter = Formatter::TODOFormatter.new IO::Memory.new, file
s = Source.new "def invalid_syntax" 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] formatter.finished [s]
content = file.to_s content = file.to_s

View file

@ -14,7 +14,7 @@ module Ameba
describe "#error" do describe "#error" do
it "adds and error" do it "adds and error" do
s = Source.new "", "source.cr" 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 s.should_not be_valid
error = s.errors.first error = s.errors.first

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -36,7 +36,7 @@ module Ameba
def initialize(@code : String, @path = "") def initialize(@code : String, @path = "")
end end
# Add new error to the list of errors. # Adds new error to the list of errors.
# #
# ``` # ```
# source.error rule, location, "Line too long" # source.error rule, location, "Line too long"
@ -46,6 +46,17 @@ module Ameba
errors << Error.new rule, location, message errors << Error.new rule, location, message
end 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. # Indicates whether source is valid or not.
# Returns true if the list or errors empty, false otherwise. # Returns true if the list or errors empty, false otherwise.
# #
@ -89,18 +100,6 @@ module Ameba
.parse .parse
end 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 def fullpath
@fullpath ||= File.expand_path @path @fullpath ||= File.expand_path @path
end end