Commit 24e7c804 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Support sparse arrays in JavaScript.

Don't crash when encountering [5, , 7]

Although nobody shouldn't write such code in their sane mind, it's
still valid JavaScript.

Fixes #418
parent 9bd4793d
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -161,7 +161,9 @@ module JsDuck
          exit(1)
        end

        properties.map {|p| node[p] }.compact.flatten
        # First flatten, then get rid of all nils, because the inner
        # arrays may also contain nils.
        properties.map {|p| node[p] }.flatten.compact
      end

      # All possible node types in Esprima-created abstract syntax tree
+15 −0
Original line number Diff line number Diff line
@@ -291,4 +291,19 @@ describe JsDuck::Js::Parser do
    end
  end

  # Sparse arrays are perfectly valid in JavaScript.
  # Omitted array members are initialized to undefined.
  describe "parsing comment before a missing array member" do
    before do
      @docs = parse(<<-EOS)
          x = [5, /* Blah */, 6];
      EOS
    end

    it "associates comment with the next array member after that" do
      @docs[0][:comment].should == " Blah "
      @docs[0][:code]["value"].should == 6
    end
  end

end