[Sass] Fix line numbering for errors.

This commit is contained in:
S. Brent Faulkner 2009-04-06 15:16:57 -04:00 committed by Nathan Weizenbaum
parent 50aeed6c7e
commit a03307d593
2 changed files with 21 additions and 2 deletions

View File

@ -130,6 +130,7 @@ module Sass
def tabulate(string)
tab_str = nil
first = true
index_offset = (@options[:line] || 1) - 1
enum_with_index(string.gsub(/\r|\n|\r\n|\r\n/, "\n").scan(/^.*?$/)).map do |line, index|
index += 1
next if line.strip.empty?
@ -144,14 +145,14 @@ module Sass
end
end
first &&= !tab_str.nil?
next Line.new(line.strip, 0, index, 0, @options[:filename], []) if tab_str.nil?
next Line.new(line.strip, 0, index + index_offset, 0, @options[:filename], []) if tab_str.nil?
line_tabs = line_tab_str.scan(tab_str).size
raise SyntaxError.new(<<END.strip.gsub("\n", ' '), index) if tab_str * line_tabs != line_tab_str
Inconsistent indentation: #{Haml::Shared.human_indentation line_tab_str, true} used for indentation,
but the rest of the document was indented using #{Haml::Shared.human_indentation tab_str}.
END
Line.new(line.strip, line_tabs, index, tab_str.size, @options[:filename], [])
Line.new(line.strip, line_tabs, index + index_offset, tab_str.size, @options[:filename], [])
end.compact
end

View File

@ -145,6 +145,24 @@ SASS
end
end
def test_exception_location
to_render = <<SASS
rule
:attr val
// comment!
:broken
SASS
begin
Sass::Engine.new(to_render, :filename => __FILE__, :line => (__LINE__-7)).render
rescue Sass::SyntaxError => err
assert_equal(__FILE__, err.sass_filename)
assert_equal((__LINE__-6), err.sass_line)
else
assert(false, "Exception not raised for '#{to_render}'!")
end
end
def test_imported_exception
[nil, 2].each do |i|
begin