diff --git a/lib/jsduck/ast.rb b/lib/jsduck/ast.rb index 0f182991c5964b5a64424678ec90814c54b7b796..4374a29b691fbd1d7f51ce45da9e11101c350c30 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 2c59cc7f9ef268dc2687a4d7a44351d10ba4908c..147180c5b2a1da01e7c896a1e4ea45aa4813d988 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 23c4dda6bd1c3f95ca6c5b81b9b8e5dd9f4270c2..49638224b7badf88efb8633438e8cc1d7013649b 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 01579f9dc8e759f3568fab99d01605d4c1125571..35938addfd79553404da48ec5d41b6f8da58cef6 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 d996ff867c81820af6055261cd8c9c84e112b0cc..0bcbd7e73f3f7e28e9522761ff3a81801e0fc693 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