Commit f6a5ed42 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Merge branch --ext-namespaces into esprima-parser.

Had to pretty much re-implement how --ext-namespaces works in new parser
context.  However, the implementation is much simpler than the one
for old parser.
parents 1284a7a8 b5302e65
Loading
Loading
Loading
Loading
+14 −2
Original line number Diff line number Diff line
@@ -6,12 +6,24 @@ module JsDuck
  # Analyzes the AST produced by EsprimaParser.
  class Ast
    # Should be initialized with EsprimaParser#parse result.
    def initialize(docs = [])
    def initialize(docs = [], options = {})
      @serializer = JsDuck::Serializer.new
      @evaluator = JsDuck::Evaluator.new
      @ext_define_patterns = build_ext_define_patterns(options[:ext_namespaces] || ["Ext"])
      @docs = docs
    end

    # Given Array of alternate Ext namespaces builds list of patterns
    # for detecting Ext.define:
    #
    # ["Ext","Foo"] --> ["Ext.define", "Ext.ClassManager.create", "Foo.define", "Foo.ClassManager.create"]
    #
    def build_ext_define_patterns(namespaces)
      namespaces.map do |ns|
        [ns + ".define", ns + ".ClassManager.create"]
      end.flatten
    end

    # Performs the detection of code in all docsets.
    #
    # @returns the processed array of docsets. (But it does it
@@ -139,7 +151,7 @@ module JsDuck
    end

    def ext_define?(ast)
      call?(ast) && ["Ext.define", "Ext.ClassManager.create"].include?(to_s(ast["callee"]))
      call?(ast) && @ext_define_patterns.include?(to_s(ast["callee"]))
    end

    def ext_extend?(ast)
+1 −1
Original line number Diff line number Diff line
@@ -75,7 +75,7 @@ module JsDuck
      when :number
        "number"
      when :dimension
        "measurement"
        "length"
      when :percentage
        "percentage"
      when :string
+1 −1
Original line number Diff line number Diff line
@@ -44,7 +44,7 @@ module JsDuck
        docs = CssParser.new(contents, options).parse
      else
        docs = JsParser.new(contents, options).parse
        docs = Ast.new(docs).detect_all!
        docs = Ast.new(docs, options).detect_all!
      end
    end

+8 −8
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@ describe JsDuck::Aggregator do
    before do
      @doc = parse(<<-EOCSS)[0]
        /**
         * @var {measurement} $button-height Default height for buttons.
         * @var {length} $button-height Default height for buttons.
         */
      EOCSS
    end
@@ -30,7 +30,7 @@ describe JsDuck::Aggregator do
      @doc[:name].should == "$button-height"
    end
    it "detects variable type" do
      @doc[:type].should == "measurement"
      @doc[:type].should == "length"
    end
    it "detects variable description" do
      @doc[:doc].should == "Default height for buttons."
@@ -41,7 +41,7 @@ describe JsDuck::Aggregator do
    before do
      @doc = parse(<<-EOCSS)[0]
        /**
         * @var {measurement} $button-height Default height for buttons.
         * @var {length} $button-height Default height for buttons.
         * @member Ext.Button
         */
      EOCSS
@@ -56,7 +56,7 @@ describe JsDuck::Aggregator do
    before do
      @doc = parse(<<-EOCSS)[0]
        /**
         * @var {measurement} [$button-height=25px]
         * @var {length} [$button-height=25px]
         */
      EOCSS
    end
@@ -83,7 +83,7 @@ describe JsDuck::Aggregator do
      @doc[:name].should == "$button-height"
    end
    it "detects variable type" do
      @doc[:type].should == "measurement"
      @doc[:type].should == "length"
    end
    it "detects variable default value" do
      @doc[:default].should == "25px"
@@ -102,7 +102,7 @@ describe JsDuck::Aggregator do
      @doc[:tagname].should == :css_var
    end
    it "detects variable type" do
      @doc[:type].should == "measurement"
      @doc[:type].should == "length"
    end
    it "detects variable default value" do
      @doc[:default].should == "25px"
@@ -126,8 +126,8 @@ describe JsDuck::Aggregator do
    it "detects float begging with dot" do
      detect_type(".6").should == "number"
    end
    it "detects measurement" do
      detect_type("15em").should == "measurement"
    it "detects length" do
      detect_type("15em").should == "length"
    end
    it "detects percentage" do
      detect_type("99.9%").should == "percentage"
+22 −0
Original line number Diff line number Diff line
require "jsduck/js_parser"
require "jsduck/ast"

describe "--ext-namespaces=Ext,MyNs,MyNs.Foo.Bar" do
  def parse(string)
    docs = JsDuck::JsParser.new(string).parse
    JsDuck::Ast.new(docs, {:ext_namespaces => ["Ext", "MyNs", "MyNs.Foo.Bar"]}).detect_all!
  end

  it "allows detecting Ext.define()" do
    parse("/** */ Ext.define('MyClass', {});")[0][:code][:tagname].should == :class
  end

  it "allows detecting MyNs.define()" do
    parse("/** */ MyNs.define('MyClass', {});")[0][:code][:tagname].should == :class
  end

  it "allows detecting MyNs.Foo.Bar.define()" do
    parse("/** */ MyNs.Foo.Bar.define('MyClass', {});")[0][:code][:tagname].should == :class
  end

end