Commit 48bb3d21 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Turn ExtNamespaces into singleton.

This way it doesn't have to be initialized every time Ast class
is invoked. Also one less options object to be passed around.
parent 3ac648c1
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -8,10 +8,9 @@ module JsDuck
  # Analyzes the AST produced by EsprimaParser.
  class Ast
    # Should be initialized with EsprimaParser#parse result.
    def initialize(docs = [], options = {})
    def initialize(docs = [])
      @serializer = JsDuck::Serializer.new
      @evaluator = JsDuck::Evaluator.new
      @ext_patterns = JsDuck::ExtPatterns.new(options[:ext_namespaces] || ["Ext"])
      @docs = docs
    end

@@ -174,7 +173,7 @@ module JsDuck
    end

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

    def fire_event?(ast)
+19 −9
Original line number Diff line number Diff line
require "jsduck/util/singleton"

module JsDuck

  # Identifies Ext JS builtins like Ext.define and Ext.extend, taking
@@ -15,17 +17,14 @@ module JsDuck
  # The matches? method will take care of identifying all these four
  # cases:
  #
  #     ps = ExtPatterns.new(["Ext", "MyApp"])
  #     matches?("Ext.define", "MyApp.define") --> true
  #     ExtPatterns.set(["Ext", "MyApp"])
  #     ExtPatterns.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"]),
      }
    include Util::Singleton

    def initialize
      set(["Ext"])
    end

    # True when string matches the given pattern type.
@@ -36,6 +35,17 @@ module JsDuck
      @patterns[pattern].include?(string)
    end

    # Reconfigures ExtPatterns with different set of namespaces.
    # Called when --ext-namespaces option is passed to JSDuck.
    def set(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

    private

    # Given Array of alternate Ext namespaces builds list of patterns
+2 −3
Original line number Diff line number Diff line
@@ -125,7 +125,6 @@ module JsDuck
      @data_path = nil # This gets assigned in JsDuck::WebWriter after writing the data file.
      @local_storage_db = "docs"
      @touch_examples_ui = false
      @ext_namespaces = ["Ext"]
      @imports = []
      @new_since = nil

@@ -527,8 +526,8 @@ module JsDuck
          "In such case pass --ext-namespaces=Ext,YourNS option",
          "and JSDuck will recognize both Ext.define() and",
          "YourNs.define() plus few other things that depend on",
          "Ext namespace like Ext.emptyFn.") do |ns|
          @ext_namespaces = ns
          "Ext namespace like Ext.emptyFn.") do |namespaces|
          ExtPatterns.set(namespaces)
        end

        opts.on('--touch-examples-ui',
+1 −1
Original line number Diff line number Diff line
@@ -43,7 +43,7 @@ module JsDuck
          docs = CssParser.new(contents, options).parse
        else
          docs = JsParser.new(contents, options).parse
          docs = Ast.new(docs, options).detect_all!
          docs = Ast.new(docs).detect_all!
        end
      end

+10 −1
Original line number Diff line number Diff line
require "jsduck/js_parser"
require "jsduck/ast"
require "jsduck/ext_patterns"

describe "--ext-namespaces=Ext,MyNs,MyNs.Foo.Bar" do
  before do
    JsDuck::ExtPatterns.set(["Ext", "MyNs", "MyNs.Foo.Bar"])
  end

  after do
    JsDuck::ExtPatterns.set(["Ext"])
  end

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

  it "allows detecting Ext.define()" do