From 48bb3d219e589735275f48ea734f22f7f32a2285 Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Tue, 18 Dec 2012 13:36:35 +0200 Subject: [PATCH] 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. --- lib/jsduck/ast.rb | 5 ++--- lib/jsduck/ext_patterns.rb | 28 +++++++++++++++++++--------- lib/jsduck/options.rb | 5 ++--- lib/jsduck/source/file_parser.rb | 2 +- spec/ext_namespaces_spec.rb | 11 ++++++++++- 5 files changed, 34 insertions(+), 17 deletions(-) diff --git a/lib/jsduck/ast.rb b/lib/jsduck/ast.rb index 0f182991..4374a29b 100644 --- a/lib/jsduck/ast.rb +++ b/lib/jsduck/ast.rb @@ -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) diff --git a/lib/jsduck/ext_patterns.rb b/lib/jsduck/ext_patterns.rb index 2c59cc7f..147180c5 100644 --- a/lib/jsduck/ext_patterns.rb +++ b/lib/jsduck/ext_patterns.rb @@ -1,3 +1,5 @@ +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 diff --git a/lib/jsduck/options.rb b/lib/jsduck/options.rb index 23c4dda6..49638224 100644 --- a/lib/jsduck/options.rb +++ b/lib/jsduck/options.rb @@ -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', diff --git a/lib/jsduck/source/file_parser.rb b/lib/jsduck/source/file_parser.rb index 01579f9d..35938add 100644 --- a/lib/jsduck/source/file_parser.rb +++ b/lib/jsduck/source/file_parser.rb @@ -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 diff --git a/spec/ext_namespaces_spec.rb b/spec/ext_namespaces_spec.rb index d996ff86..0bcbd7e7 100644 --- a/spec/ext_namespaces_spec.rb +++ b/spec/ext_namespaces_spec.rb @@ -1,10 +1,19 @@ 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 -- GitLab