Class: Skeptic::Rules::MaxNestingDepth

Inherits:
Object
  • Object
show all
Includes:
SexpVisitor
Defined in:
lib/skeptic/rules/max_nesting_depth.rb

Constant Summary collapse

DESCRIPTION =
'Limit the maximum nesting depth'

Instance Method Summary collapse

Methods included from SexpVisitor

included

Constructor Details

#initialize(limit = nil) ⇒ MaxNestingDepth

Returns a new instance of MaxNestingDepth.



8
9
10
11
12
# File 'lib/skeptic/rules/max_nesting_depth.rb', line 8

def initialize(limit = nil)
  env[:scope] = Scope.new
  @scopes = []
  @limit  = limit
end

Instance Method Details

#apply_to(code, tokens, sexp) ⇒ Object



14
15
16
17
# File 'lib/skeptic/rules/max_nesting_depth.rb', line 14

def apply_to(code, tokens, sexp)
  visit sexp
  self
end

#nameObject



29
30
31
# File 'lib/skeptic/rules/max_nesting_depth.rb', line 29

def name
  "Maximum nesting depth (#@limit)"
end

#nestingsObject



19
20
21
# File 'lib/skeptic/rules/max_nesting_depth.rb', line 19

def nestings
  @scopes.uniq
end

#violationsObject



23
24
25
26
27
# File 'lib/skeptic/rules/max_nesting_depth.rb', line 23

def violations
  @scopes.select { |scope| scope.depth > @limit }.map do |scope|
    "#{scope.location} has #{scope.depth} levels of nesting: #{scope.levels.join(' > ')}"
  end
end