Commit 2739b18b authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Auto-create get/set methods for @cfg with @accessor.

The generated methods will have the following documentation:

    @cfg foo

    @method getFoo
    Returns the value of {@link #cfg-foo}.

    @method setFoo
    Sets the value of {@link #cfg-foo}.
parent bb4eb13c
Loading
Loading
Loading
Loading
+71 −0
Original line number Diff line number Diff line
require 'jsduck/logger'

module JsDuck

  class Accessors
    # Given a class, generates accessor methods to configs with
    # @accessor tag.  Modifies the class by adding these methods.
    # When class already contains a getter or setter, the method is
    # not added.
    def create(cls)
      # Grab all configs tagged as @accessor
      accessors = cls[:members][:cfg].find_all {|cfg| cfg[:accessor] }

      # Build lookup table of method names
      methods = {}
      cls[:members][:method].each do |m|
        methods[m[:name]] = m;
      end

      accessors.each do |cfg|
        # add getter if no method with same name exists
        get = create_getter(cfg)
        if !methods[get[:name]]
          cls[:members][:method] << get
        end
        # add setter if no method with same name exists
        set = create_setter(cfg)
        if !methods[set[:name]]
          cls[:members][:method] << set
        end
      end
    end

    def create_getter(cfg)
      return {
        :tagname => :method,
        :name => "get" + upcase_first(cfg[:name]),
        :doc => "Returns the value of {@link #cfg-#{cfg[:name]}}.",
        :params => [],
        :return => {
          :type => cfg[:type],
          :doc => "",
        },
        :owner => cfg[:owner],
      }
    end

    def create_setter(cfg)
      return {
        :tagname => :method,
        :name => "set" + upcase_first(cfg[:name]),
        :doc => "Sets the value of {@link #cfg-#{cfg[:name]}}.",
        :params => [{
            :type => cfg[:type],
            :name => cfg[:name],
            :doc => "",
          }],
        :return => {
          :type => "undefined",
          :doc => "",
        },
        :owner => cfg[:owner],
      }
    end

    def upcase_first(str)
      str[0,1].upcase + str[1..-1]
    end
  end

end
+9 −0
Original line number Diff line number Diff line
require 'jsduck/class'
require 'jsduck/accessors'

module JsDuck

@@ -165,6 +166,14 @@ module JsDuck
      end
    end

    # Creates accessor method for configs marked with @accessor
    def create_accessors
      accessors = Accessors.new
      @classes.each_value do |cls|
        accessors.create(cls)
      end
    end

    # Are we dealing with ExtJS 4?
    # True if any of the classes is defined with Ext.define()
    def ext4?
+1 −0
Original line number Diff line number Diff line
@@ -125,6 +125,7 @@ module JsDuck
      end
      agr.classify_orphans
      agr.create_global_class unless @opts.ignore_global
      agr.create_accessors
      agr.append_ext4_event_options
      agr.result
    end
+2 −0
Original line number Diff line number Diff line
@@ -123,6 +123,8 @@ module JsDuck
          boolean_at_tag(/@(private|ignore|hide)/, :private)
        elsif look(/@protected\b/)
          boolean_at_tag(/@protected/, :protected)
        elsif look(/@accessor\b/)
          boolean_at_tag(/@accessor/, :accessor)
        elsif look(/@markdown\b/)
          # this is detected just to be ignored
          boolean_at_tag(/@markdown/, :markdown)
+1 −0
Original line number Diff line number Diff line
@@ -168,6 +168,7 @@ module JsDuck
        :optional => detect_optional(:cfg, doc_map),
        :default => detect_default(:cfg, doc_map, code),
        :properties => detect_subproperties(docs, :cfg),
        :accessor => !!doc_map[:accessor],
      }, doc_map)
    end

Loading