Sha256: 497d5b31b8d040d5943d4c2b532020038af86b5f0c6a7d6d7493ff4b1169b7d0

Contents?: true

Size: 1.22 KB

Versions: 12

Compression:

Stored size: 1.22 KB

Contents

require 'jsduck/logger'

module JsDuck

  # Checks for circular dependencies
  class CircularDeps
    def initialize(classes)
      @classes = classes
    end

    # Checks all classes for circular dependencies.
    #
    # When found, exits with a fatal error message.
    def check_all
      @classes.each do |cls|
        if chain = check(cls)
          Logger.fatal("Class #{cls[:name]} has a circular dependency: #{chain}")
          exit 1
        end
      end
    end

    # Checks class for circular dependencies.
    #
    # When all OK, returns false.
    #
    # When circular dependencies found returns a string describing the
    # problematic dependency chain e.g. "Foo extends Bar mixins Foo".
    def check(cls, names = [])
      names += [cls[:name]]

      if cls.parent && chain = track_circular(" extends ", cls.parent, names)
        return chain
      end

      cls.mixins.each do |mixin|
        if chain = track_circular(" mixins ", mixin, names)
          return chain
        end
      end

      false
    end

    def track_circular(type, cls, names)
      names += [type]
      if names.include?(cls[:name])
        (names + [cls[:name]]).join("")
      else
        check(cls, names)
      end
    end

  end

end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
jsduck-4.10.4 lib/jsduck/circular_deps.rb
jsduck-4.10.3 lib/jsduck/circular_deps.rb
jsduck-4.10.2 lib/jsduck/circular_deps.rb
jsduck-4.10.1 lib/jsduck/circular_deps.rb
jsduck-4.10.0 lib/jsduck/circular_deps.rb
jsduck-4.9.0 lib/jsduck/circular_deps.rb
jsduck-4.8.0 lib/jsduck/circular_deps.rb
jsduck-4.7.1 lib/jsduck/circular_deps.rb
jsduck-4.7.0 lib/jsduck/circular_deps.rb
jsduck-4.6.2 lib/jsduck/circular_deps.rb
jsduck-4.6.1 lib/jsduck/circular_deps.rb
jsduck-4.6.0 lib/jsduck/circular_deps.rb