Commit 261a9f1a authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Extract ExtPatterns class from JsDuck::Ast.

parent 29f5519c
Loading
Loading
Loading
Loading
+10 −33
Original line number Diff line number Diff line
require "jsduck/serializer"
require "jsduck/evaluator"
require "jsduck/function_ast"
require "jsduck/ext_patterns"

module JsDuck

@@ -10,38 +11,10 @@ module JsDuck
    def initialize(docs = [], options = {})
      @serializer = JsDuck::Serializer.new
      @evaluator = JsDuck::Evaluator.new
      init_ext_patterns(options[:ext_namespaces] || ["Ext"])
      @ext_patterns = JsDuck::ExtPatterns.new(options[:ext_namespaces] || ["Ext"])
      @docs = docs
    end

    def init_ext_patterns(namespaces)
      @ext_patterns = {
        :define => build_patterns(namespaces, [".define", ".ClassManager.create"]),
        :extend => build_patterns(namespaces, [".extend"]),
        :override => build_patterns(namespaces, [".override"]),
        :emptyfn => build_patterns(namespaces, [".emptyFn"]),
      }
    end

    # Given Array of alternate Ext namespaces builds list of patterns
    # for detecting Ext.define or some other construct:
    #
    # build_patterns(["Ext", "Foo"], [".define"]) --> ["Ext.define", "Foo.define"]
    #
    def build_patterns(namespaces, suffixes)
      patterns = []
      namespaces.each do |ns|
        suffixes.each do |suffix|
          patterns << ns + suffix
        end
      end
      patterns
    end

    def ext_pattern?(pattern, ast)
      @ext_patterns[pattern].include?(to_s(ast))
    end

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

    def ext_define?(ast)
      call?(ast) && ext_pattern?(:define, ast["callee"])
      call?(ast) && ext_pattern?("Ext.define", ast["callee"])
    end

    def ext_extend?(ast)
      call?(ast) && ext_pattern?(:extend, ast["callee"])
      call?(ast) && ext_pattern?("Ext.extend", ast["callee"])
    end

    def ext_override?(ast)
      call?(ast) && ext_pattern?(:override, ast["callee"])
      call?(ast) && ext_pattern?("Ext.override", ast["callee"])
    end

    def function?(ast)
@@ -193,7 +166,11 @@ module JsDuck
    end

    def empty_fn?(ast)
      ast["type"] == "MemberExpression" && ext_pattern?(:emptyfn, ast)
      ast["type"] == "MemberExpression" && ext_pattern?("Ext.emptyFn", ast)
    end

    def ext_pattern?(pattern, ast)
      @ext_patterns.matches?(pattern, to_s(ast))
    end

    def var?(ast)
+58 −0
Original line number Diff line number Diff line
module JsDuck

  # Identifies Ext JS builtins like Ext.define and Ext.extend, taking
  # also into account the possibility of aliasing the Ext namespace.
  #
  # For example when the following command line option is used:
  #
  #     --ext-namespaces=Ext,MyApp
  #
  # we need to identify both Ext.define and MyApp.define, but
  # Ext.define is additionally aliased withing ExtJS as
  # Ext.ClassManager.create, so we also need to recognize
  # Ext.ClassManager.create and MyApp.ClassManager.create.
  #
  # The matches? method will take care of identifying all these four
  # cases:
  #
  #     ps = ExtPatterns.new(["Ext", "MyApp"])
  #     matches?("Ext.define", "MyApp.define") --> true
  #
  class ExtPatterns
    def initialize(namespaces)
      @patterns = {
        "Ext.define" => build_patterns(namespaces, [".define", ".ClassManager.create"]),
        "Ext.extend" => build_patterns(namespaces, [".extend"]),
        "Ext.override" => build_patterns(namespaces, [".override"]),
        "Ext.emptyFn" => build_patterns(namespaces, [".emptyFn"]),
      }
    end

    # True when string matches the given pattern type.
    #
    # Pattern type is one of: "Ext.define", "Ext.extend",
    # "Ext.override", "Ext.emptyFn"
    def matches?(pattern, string)
      @patterns[pattern].include?(string)
    end

    private

    # Given Array of alternate Ext namespaces builds list of patterns
    # for detecting Ext.define or some other construct:
    #
    # build_patterns(["Ext", "Foo"], [".define"]) --> ["Ext.define", "Foo.define"]
    #
    def build_patterns(namespaces, suffixes)
      patterns = []
      namespaces.each do |ns|
        suffixes.each do |suffix|
          patterns << ns + suffix
        end
      end
      patterns
    end

  end

end