Commit cd351aa8 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Correctly inherit all @accessor fields.

The generated getters, setters and events now get all the fields
from the originating config.  Previously there was just a static
list of some fields that get copied over.
parent b1f10447
Loading
Loading
Loading
Loading
+21 −8
Original line number Diff line number Diff line
@@ -3,6 +3,17 @@ require 'jsduck/logger'
module JsDuck
  module Process

    # Expands accessors.
    #
    # Looks up configs with @accessor tag (or configs defined inside
    # config: {} or eventedConfig: {} block).
    #
    # For such config "foo" it generates:
    #
    # - getter "getFoo"
    # - setter "setFoo"
    # - event "foochange" (when tagged with @evented)
    #
    class Accessors
      def initialize(classes)
        @classes = classes
@@ -13,6 +24,8 @@ module JsDuck
        @classes.each_value {|cls| process(cls) }
      end

      private

      # 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
@@ -113,15 +126,15 @@ module JsDuck
          }, cfg)
      end

      # Copy over from @cfg all the fields that aren't already present.
      # Except :type and :default which don't make sense for methods and events.
      def add_shared(hash, cfg)
        hash.merge!({
            :owner => cfg[:owner],
            :files => cfg[:files],
            :private => cfg[:private],
            :protected => cfg[:protected],
            :autodetected => cfg[:autodetected],
            :hide => cfg[:hide],
          })
        ignored_fields = [:type, :default, :accessor, :evented]

        cfg.each_pair do |key, value|
          hash[key] = value unless ignored_fields.include?(key) || hash[key]
        end
        hash
      end

      def upcase_first(str)
+47 −0
Original line number Diff line number Diff line
@@ -114,6 +114,53 @@ describe JsDuck::Aggregator do

  end

  describe "@accessor with other tags" do
    before do
      @docs = parse(<<-EOF)
        /** @class MyClass */
          /**
           * @cfg {String} foo
           * Original comment.
           * @accessor
           * @evented
           * @protected
           * @deprecated 2.0 Don't use it any more
           */
      EOF
      @members = {}
      @docs["MyClass"][:members].each do |m|
        @members[m[:name]] = m
      end
    end

    it "adds @protected to getter" do
      @members["getFoo"][:protected].should == true
    end

    it "adds @deprecated to getter" do
      @members["getFoo"][:deprecated].should_not == nil
    end

    it "doesn't add @accessor to getter" do
      @members["getFoo"][:accessor].should == nil
    end

    it "doesn't add @evented to getter" do
      @members["getFoo"][:evented].should == nil
    end

    # Lighter tests for setter and event.
    # The same method takes care of inheriting in all cases.

    it "adds @protected to setter" do
      @members["setFoo"][:protected].should == true
    end

    it "adds @protected to event" do
      @members["foochange"][:protected].should == true
    end
  end

  describe "@accessor tag on private cfg" do
    before do
      @docs = parse(<<-EOF)