Skip to content

Commit

Permalink
[Sass] Only display text on the current line when reporting css2sass …
Browse files Browse the repository at this point in the history
…errors.
  • Loading branch information
nex3 committed Sep 23, 2009
1 parent de8e5a0 commit 0e68e5f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
10 changes: 7 additions & 3 deletions doc-src/SASS_CHANGELOG.md
Expand Up @@ -16,10 +16,14 @@ In addition, when the `sass` executable encounters an error,
it now prints the filename where the error occurs,
as well as a backtrace of Sass imports.

### Minor Changes
### `css2sass` Error Handling

* `css2sass` error handling and reporting has been improved,
including fixing line-number reporting for templates without trailing newlines.
Several bug fixes and minor improvements have been made, including:

* Fixing line-number reporting for errors on the last line of templates
that didn't have trailing newlines.

* Only displaying the text for the current line when reporting CSS parsing errors.

## [2.2.4](http://github.com/nex3/haml/commit/2.2.4)

Expand Down
4 changes: 2 additions & 2 deletions lib/sass/css.rb
Expand Up @@ -201,13 +201,13 @@ def assert_match(re)

pos = @template.pos

after = @template.string[pos - 15...pos]
after = @template.string[[pos - 15, 0].max...pos].gsub(/.*\n/m, '')
after = "..." + after if pos >= 15

# Display basic regexps as plain old strings
expected = re.source == Regexp.escape(re.source) ? "\"#{re.source}\"" : re.inspect

was = @template.rest[0...15]
was = @template.rest[0...15].gsub(/\n.*/m, '')
was += "..." if @template.rest.size >= 15
raise Sass::SyntaxError.new(
"Invalid CSS after #{after.inspect}: expected #{expected}, was #{was.inspect}")
Expand Down
22 changes: 19 additions & 3 deletions test/sass/css2sass_test.rb
Expand Up @@ -235,15 +235,31 @@ def test_error_reporting
assert(false, "Expected exception")
rescue Sass::SyntaxError => err
assert_equal(1, err.sass_line)
assert_equal('Invalid CSS after nil: expected /\{/, was ""', err.message)
assert_equal('Invalid CSS after "foo": expected /\{/, was ""', err.message)
end

def test_error_reporting_in_line
css2sass("foo\nbar }")
css2sass("foo\nbar }\nbaz")
assert(false, "Expected exception")
rescue Sass::SyntaxError => err
assert_equal(2, err.sass_line)
assert_equal('Invalid CSS after "o\nbar ": expected /\{/, was "}"', err.message)
assert_equal('Invalid CSS after "bar ": expected /\{/, was "}"', err.message)
end

def test_error_truncate_after
css2sass("#{"a" * 15}foo")
assert(false, "Expected exception")
rescue Sass::SyntaxError => err
assert_equal(1, err.sass_line)
assert_equal('Invalid CSS after "...aaaaaaaaaaaafoo": expected /\{/, was ""', err.message)
end

def test_error_truncate_was
css2sass("foo }#{"a" * 15}")
assert(false, "Expected exception")
rescue Sass::SyntaxError => err
assert_equal(1, err.sass_line)
assert_equal('Invalid CSS after "foo ": expected /\{/, was "}aaaaaaaaaaaaaa..."', err.message)
end

private
Expand Down

0 comments on commit 0e68e5f

Please sign in to comment.