Permalink
Browse files

Raising useful exceptions

  • Loading branch information...
1 parent 5d4b18b commit 2760282191c984950545ed37f5f571537314e9ad @marcandre marcandre committed with Dec 1, 2009
Showing with 51 additions and 20 deletions.
  1. +1 −1 Rakefile
  2. +32 −19 lib/jsmin.rb
  3. +4 −0 test/js/error.js
  4. +14 −0 test/test_exception.rb
View
@@ -26,7 +26,7 @@ require 'rake/rdoctask'
gemspec = Gem::Specification.new do |s|
s.name = 'jsmin'
- s.version = '1.0.1'
+ s.version = '1.0.2'
s.author = 'Ryan Grove'
s.email = 'ryan@wonko.com'
s.homepage = 'http://github.com/rgrove/jsmin/'
View
@@ -55,22 +55,30 @@ module JSMin
CHR_QUOTE = '"'.freeze
CHR_SPACE = ' '.freeze
- if RUBY_VERSION >= '1.9'
- ORD_LF = "\n".freeze
- ORD_SPACE = ' '.freeze
- ORD_TILDE = '~'.freeze
- else
- ORD_LF = "\n"[0].freeze
- ORD_SPACE = ' '[0].freeze
- ORD_TILDE = '~'[0].freeze
+ ORD_LF = "\n"[0].freeze
+ ORD_SPACE = ' '[0].freeze
+ ORD_TILDE = '~'[0].freeze
+
+ class ParseError < RuntimeError
+ attr_accessor :source, :line
+ def initialize(err, source, line)
+ @source = source,
+ @line = line
+ super "JSMin Parse Error: #{err} at line #{line} of #{source}"
+ end
end
class << self
+ def raise(err)
+ super ParseError.new(err, @source, @line)
+ end
# Reads JavaScript from _input_ (which can be a String or an IO object) and
# returns a String containing minified JS.
def minify(input)
@js = StringScanner.new(input.is_a?(IO) ? input.read : input.to_s)
+ @source = input.is_a?(IO) ? input.inspect : input.to_s[0..100]
+ @line = 1
@a = "\n"
@b = nil
@@ -147,15 +155,15 @@ def action_copy
break if @a == @b
if @a[0] <= ORD_LF
- raise "JSMin parse error: unterminated string literal: #{@a}"
+ raise "unterminated string literal: #{@a.inspect}"
end
if @a == CHR_BACKSLASH
@output << @a
@a = get
if @a[0] <= ORD_LF
- raise "JSMin parse error: unterminated string literal: #{@a}"
+ raise "unterminated string literal: #{@a.inspect}"
end
end
end
@@ -181,8 +189,7 @@ def action_get
@output << @a
@a = get
elsif @a[0] <= ORD_LF
- raise "JSMin parse error: unterminated regular expression " +
- "literal: #{@a}"
+ raise "unterminated regular expression : #{@a.inspect}"
end
@output << @a
@@ -201,12 +208,18 @@ def alphanum?(c)
# Returns the next character from the input. If the character is a control
# character, it will be translated to a space or linefeed.
def get
- c = @lookahead.nil? ? @js.getch : @lookahead
- @lookahead = nil
-
- return c if c.nil? || c == CHR_LF || c[0] >= ORD_SPACE
- return "\n" if c == CHR_CR
- return ' '
+ if @lookahead
+ c = @lookahead
+ @lookahead = nil
+ else
+ c = @js.getch
+ if c == CHR_LF || c == CHR_CR
+ @line += 1
+ return CHR_LF
+ end
+ return ' ' unless c.nil? || c[0] >= ORD_SPACE
+ end
+ c
end
# Gets the next character, excluding comments.
@@ -232,7 +245,7 @@ def nextchar
end
when nil
- raise 'JSMin parse error: unterminated comment'
+ raise 'unterminated comment'
end
end
View
@@ -0,0 +1,4 @@
+// This javascript is bad
+var whatever;
+whatever = "hello
+function (
@@ -0,0 +1,14 @@
+require File.expand_path(File.dirname(__FILE__) + "/../lib/jsmin")
+require 'test/unit'
+
+class ExceptionTest < Test::Unit::TestCase
+ def test_exception
+ File.open("js/error.js") do |i|
+ JSMin.minify(i)
+ end
+ assert_raise {}
+ rescue JSMin::ParseError => e
+ assert_equal %q{JSMin Parse Error: unterminated string literal: "\n" at line 4 of #<File:js/error.js>},
+ e.to_s
+ end
+end

0 comments on commit 2760282

Please sign in to comment.