Class: JsDuck::Process::CircularDeps

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

Overview

Checks for circular dependencies

Instance Method Summary collapse

Constructor Details

#initialize(classes) ⇒ CircularDeps

Returns a new instance of CircularDeps.



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

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



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

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

#process_all!Object

Checks all classes for circular dependencies.

When found, exits with a fatal error message.



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

def process_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



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

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