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

Refactor global member warnings.

--ignore-global now only toggles between showing global class or hiding it.

--warnings=-global turns warnings about global members on/off regardless of
the previous option.

Printing of the warnings is also no more duplicated in both lint.rb and app.rb.

Class#all_members and Class#all_local_members no more filter out private members.
Small refactoring inside Class to avoid confusing use of all_members as local
variable.
parent 1b07ec72
Loading
Loading
Loading
Loading
+18 −11
Original line number Diff line number Diff line
@@ -110,26 +110,33 @@ module JsDuck
        agr.aggregate(file)
      end
      agr.classify_orphans
      agr.create_global_class unless @opts.ignore_global
      agr.create_global_class
      agr.create_accessors
      agr.append_ext4_event_options
      agr.result
    end

    # Filters out class-documentations, converting them to Class objects.
    # For each other type, prints a warning message and discards it
    # Turns all aggregated data into Class objects.
    # Depending on --ignore-global either keeps or discards the global class.
    # Warnings for global members are printed regardless of that setting,
    # but of course can be turned off using --warnings=-global
    def filter_classes(docs)
      classes = []
      docs.each do |d|
        if d[:tagname] == :class
          classes << Class.new(d)
        cls = Class.new(d)
        if d[:name] != "global"
          classes << cls
        else
          type = d[:tagname].to_s
          name = d[:name]
          file = d[:files][0]
          # This warning is shown when there are orphaned members,
          # but the creation of global class has been turned off.
          Logger.instance.warn(:global, "Ignoring #{type}: #{name}", file[:filename], file[:linenr])
          # add global class only if --ignore-global not specified
          classes << cls unless @opts.ignore_global

          # Print warning for each global member
          cls.all_local_members.each do |m|
            type = m[:tagname].to_s
            name = m[:name]
            file = m[:files][0]
            Logger.instance.warn(:global, "Global #{type}: #{name}", file[:filename], file[:linenr])
          end
        end
      end
      Relations.new(classes, @opts.external_classes)
+9 −9
Original line number Diff line number Diff line
@@ -121,28 +121,28 @@ module JsDuck
        return {}
      end

      all_members = parent ? parent.members_hash(type, context) : {}
      ms = parent ? parent.members_hash(type, context) : {}

      mixins.each do |mix|
        all_members.merge!(mix.members_hash(type, context)) {|k,o,n| store_overrides(k,o,n)}
        ms.merge!(mix.members_hash(type, context)) {|k,o,n| store_overrides(k,o,n)}
      end

      # For static members, exclude everything not explicitly marked as inheritable
      if context == :statics
        all_members.delete_if {|key, member| !member[:inheritable] }
        ms.delete_if {|key, member| !member[:inheritable] }
      end

      all_members.merge!(local_members_hash(type, context)) {|k,o,n| store_overrides(k,o,n)}
      ms.merge!(local_members_hash(type, context)) {|k,o,n| store_overrides(k,o,n)}

      # If singleton has static members, include them as if they were
      # instance members.  Otherwise they will be completely excluded
      # from the docs, as the static members block is not created for
      # singletons.
      if @doc[:singleton] && @doc[:statics][type].length > 0
        all_members.merge!(local_members_hash(type, :statics)) {|k,o,n| store_overrides(k,o,n)}
        ms.merge!(local_members_hash(type, :statics)) {|k,o,n| store_overrides(k,o,n)}
      end

      all_members
      ms
    end

    # Invoked when merge! finds two members with the same name.
@@ -197,7 +197,7 @@ module JsDuck
      return ms
    end

    # Returns all public members of class, including the inherited and mixed in ones
    # Returns all members of class, including the inherited and mixed in ones
    def all_members
      all = []
      [:members, :statics].each do |group|
@@ -208,12 +208,12 @@ module JsDuck
      all
    end

    # Returns all local public members of class
    # Returns all local members of class
    def all_local_members
      all = []
      [:members, :statics].each do |group|
        @doc[group].each_value do |ms|
          all += ms.find_all {|m| !m[:private] }
          all += ms
        end
      end
      all
+0 −10
Original line number Diff line number Diff line
@@ -12,7 +12,6 @@ module JsDuck

    # Runs the linter
    def run
      warn_globals
      warn_no_doc
      warn_unnamed
      warn_optional_params
@@ -20,15 +19,6 @@ module JsDuck
      warn_duplicate_members
    end

    # print warning for each global member
    def warn_globals
      global = @relations["global"]
      return unless global
      global.all_local_members.each do |member|
        warn(:global, "Global #{member[:tagname]}: #{member[:name]}", member)
      end
    end

    # print warning for each member or parameter with no name
    def warn_unnamed
      each_member do |member|