Commit 65f48174 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Support number and string literals in type specifications.

parent 90017446
Loading
Loading
Loading
Loading
+25 −1
Original line number Diff line number Diff line
@@ -8,6 +8,8 @@ module JsDuck
  #
  # 1. Traditional type expressions found in ExtJS code:
  #
  #     "string"
  #     3.14
  #     SomeType
  #     Name.spaced.Type
  #     Number[]
@@ -141,7 +143,7 @@ module JsDuck
    #
    #     <array-type> ::= <atomic-type> [ "[]" ]*
    #
    #     <atomic-type> ::= <union-type> | <record-type> | <function-type> | <type-name>
    #     <atomic-type> ::= <union-type> | <record-type> | <function-type> | <string-literal> | <type-name>
    #
    def null_type
      if nullability = @input.scan(/[?!]/)
@@ -154,6 +156,10 @@ module JsDuck
        return false unless record_type
      elsif @input.check(/function\(/)
        return false unless function_type
      elsif @input.check(/['"]/)
        return false unless string_literal
      elsif @input.check(/\d/)
        return false unless number_literal
      else
        return false unless type_name
      end
@@ -289,6 +295,24 @@ module JsDuck
      true
    end

    #
    #     <string-literal> ::= '.*' | ".*"
    #
    def string_literal
      @out << @input.scan(/"([^\\"]|\\.)*?"|'([^\\']|\\.)*?'/)

      true
    end

    #
    #     <number-literal> ::= <digit>+ [ "." <digit>+ ]
    #
    def number_literal
      @out << @input.scan(/\d+(\.\d+)?/)

      true
    end

    #
    #     <type-name> ::= <type-application> | "*"
    #
+24 −0
Original line number Diff line number Diff line
@@ -18,6 +18,26 @@ describe JsDuck::TypeParser do
    JsDuck::TypeParser.new(relations).parse(str)
  end

  it "matches single-quoted string literal" do
    parse("'foo'").should == true
  end

  it "matches double-quoted string literal" do
    parse('"blah blah"').should == true
  end

  it "matches string literal with escape quote inside" do
    parse('"blah \\"blah"').should == true
  end

  it "matches integer number literal" do
    parse('42').should == true
  end

  it "matches float number literal" do
    parse('3.14').should == true
  end

  it "matches simple type" do
    parse("String").should == true
  end
@@ -51,6 +71,10 @@ describe JsDuck::TypeParser do
      parse("Number/String").should == true
    end

    it "literals" do
      parse("'foo'/'bar'/32/4").should == true
    end

    it "simple- and namespaced- and array types" do
      parse("Number/Ext.form.Panel/String[]/RegExp/Ext.Element").should == true
    end