Skip to content

Commit

Permalink
use libsass provided error filename and line no
Browse files Browse the repository at this point in the history
  • Loading branch information
bolandrm committed Jun 22, 2016
1 parent f8b95c5 commit 73397b6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 29 deletions.
5 changes: 4 additions & 1 deletion lib/sassc/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ def render

if status != 0
message = Native.context_get_error_message(context)
raise SyntaxError.new(message)
filename = Native.context_get_error_file(context)
line = Native.context_get_error_line(context)

raise SyntaxError.new(message, filename: filename, line: line)
end

css = Native.context_get_output_string(context)
Expand Down
13 changes: 7 additions & 6 deletions lib/sassc/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ class UnsupportedValue < BaseError; end
# it's important to provide filename and line number information.
# This will be used in various error reports to users, including backtraces;
class SyntaxError < BaseError
LINE_INFO_REGEX = /on line (\d+) of (.+)/
def initialize(message, filename: nil, line: nil)
@filename = filename
@line = line
super(message)
end

def backtrace
return nil if super.nil?
Expand All @@ -20,11 +24,8 @@ def backtrace

# The backtrace of the error within Sass files.
def sass_backtrace
line_info = message.split("\n").find { |line| line.match(LINE_INFO_REGEX) }
return [] unless line_info

_, line, filename = line_info.match(LINE_INFO_REGEX).to_a
["#{Pathname.getwd.join(filename)}:#{line}"]
return [] unless @filename && @line
["#{@filename}:#{@line}"]
end
end
end
34 changes: 12 additions & 22 deletions test/error_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,24 @@

module SassC
class ErrorTest < MiniTest::Test
def render(data, opts={})
Engine.new(data, opts).render
end

def test_first_backtrace_is_sass
line = 2
filename = "app/assets/stylesheets/application.scss"

begin
raise SassC::SyntaxError.new(<<-ERROR)
Error: property "padding" must be followed by a ':'
on line #{line} of #{filename}
>> padding top: 10px;
--^
ERROR
rescue SassC::SyntaxError => err
expected = "#{Pathname.getwd.join(filename)}:#{line}"
assert_equal expected, err.backtrace.first
end

begin
raise SassC::SyntaxError.new(<<-ERROR)
Error: no mixin named border-radius
template = <<-SCSS
.foo {
baz: bang;
padding top: 10px;
}
SCSS

Backtrace:
\t#{filename}:#{line}
on line #{line} of #{filename}
>> @include border-radius(5px);
-------------^
ERROR
render(template, filename: filename)
rescue SassC::SyntaxError => err
expected = "#{Pathname.getwd.join(filename)}:#{line}"
expected = "#{filename}:3"
assert_equal expected, err.backtrace.first
end
end
Expand Down

0 comments on commit 73397b6

Please sign in to comment.