Commit 3bde89f9 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Extract Process::GlobalMembers from BatchParser.

Moving all the filter_class logic to GlobalMembers processor,
which can take care of the global member corner case without
disturbing the main BatchParser logic.
parent 277937ed
Loading
Loading
Loading
Loading
+7 −24
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ require 'jsduck/class'
require 'jsduck/relations'
require 'jsduck/logger'
require 'jsduck/process/ignored_classes'
require 'jsduck/process/global_members'
require 'jsduck/process/enums'
require 'jsduck/process/accessors'
require 'jsduck/process/ext4_events'
@@ -33,7 +34,7 @@ module JsDuck
    def run
      @parsed_files = parallel_parse(@opts.input_files)
      result = aggregate(@parsed_files)
      @relations = filter_classes(result)
      @relations = to_class_objects(result)
      apply_extra_processing
      return @relations
    end
@@ -63,6 +64,7 @@ module JsDuck
      classes_hash = agr.result

      Process::IgnoredClasses.new(classes_hash).process_all!
      Process::GlobalMembers.new(classes_hash, @opts).process_all!
      Process::Accessors.new(classes_hash).process_all!
      Process::Ext4Events.new(classes_hash, @opts).process_all!
      Process::Enums.new(classes_hash).process_all!
@@ -72,29 +74,10 @@ module JsDuck
      classes_hash.values
    end

    # 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|
        cls = Class.new(d)
        if d[:name] != "global"
          classes << cls
        else
          # 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.warn(:global, "Global #{type}: #{name}", file[:filename], file[:linenr])
          end
        end
      end
    # Turns all aggregated data into Class objects and places the
    # classes inside Relations container.
    def to_class_objects(docs)
      classes = docs.map {|d| Class.new(d) }
      Relations.new(classes, @opts.external_classes)
    end

+36 −0
Original line number Diff line number Diff line
require 'jsduck/logger'

module JsDuck
  module Process

    # Prints warning for each global member.
    # Removes "global" class when --ignore-global option used.
    # Warnings for global members are printed regardless of that setting,
    # but of course can be turned off using --warnings=-global
    class GlobalMembers
      def initialize(classes_hash, opts)
        @classes_hash = classes_hash
        @opts = opts
      end

      def process_all!
        # Do nothing when there's no "global" class.
        return unless @classes_hash["global"]

        # Warnings for each global member
        @classes_hash["global"][:members].each do |m|
          type = m[:tagname].to_s
          name = m[:name]
          file = m[:files][0]
          Logger.warn(:global, "Global #{type}: #{name}", file[:filename], file[:linenr])
        end

        # Throw away the "global" class when --ignore-global option used
        if @opts.ignore_global
          @classes_hash.delete("global")
        end
      end
    end

  end
end