Class: JsDuck::CircularDeps

Inherits:
Object
  • Object
show all
Defined in:
lib/jsduck/circular_deps.rb

Overview

Checks for circular dependencies

Instance Method Summary collapse

Constructor Details

#initialize(classes) ⇒ CircularDeps

Returns a new instance of CircularDeps.



7
8
9
# File 'lib/jsduck/circular_deps.rb', line 7

def initialize(classes)
  @classes = classes
end

Instance Method Details

#check(cls, names = []) ⇒ Object

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”.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jsduck/circular_deps.rb', line 29

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

#check_allObject

Checks all classes for circular dependencies.

When found, exits with a fatal error message.



14
15
16
17
18
19
20
21
# File 'lib/jsduck/circular_deps.rb', line 14

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

#track_circular(type, cls, names) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/jsduck/circular_deps.rb', line 45

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