Commit 91ef4ba3 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

More descriptive message for JS syntax errors.

Instead of crashing with a mysterious "undefined method `range`
for nil:NilClass" message, raise our own exception, telling that
the problem is with JavaScript syntax.

Fixes #415
parent c509b029
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -19,6 +19,10 @@ module JsDuck
      # into Esprima AST, and associate comments with syntax nodes.
      def parse
        ast = RKelly::Parser.new.parse(@input)
        unless ast
          raise "Invalid JavaScript syntax"
        end

        ast = ADAPTER.adapt(ast)
        # Adjust Program node range
        ast["range"] = [0, @input.length-1]
+10 −0
Original line number Diff line number Diff line
@@ -6,6 +6,16 @@ describe JsDuck::Js::Parser do
    JsDuck::Js::Parser.new(input).parse
  end

  describe "parsing invalid JavaScript" do
    it "causes JS syntax error to be raised" do
      begin
        parse("if ( x } alert('Hello');")
      rescue
        $!.to_s.should == "Invalid JavaScript syntax"
      end
    end
  end

  describe "parsing two comments" do
    before do
      @docs = parse(<<-EOS)