Class: JsDuck::Warning::Basic

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

Overview

A basic warning type.

Instance Method Summary collapse

Constructor Details

#initialize(type, msg) ⇒ Basic

Creates a simple warning with a message text. The warning is disabled by default.



11
12
13
14
15
16
17
18
# File 'lib/jsduck/warning/basic.rb', line 11

def initialize(type, msg)
  @type = type
  @msg = msg

  @rules = []
  # disable by default
  set(false)
end

Instance Method Details

#docObject

Documentation for the warning.



50
51
52
# File 'lib/jsduck/warning/basic.rb', line 50

def doc
  " #{@rules[0][:enabled] ? '+' : '-'}#{@type} - #{@msg}"
end

#enabled?(filename = "", params = []) ⇒ Boolean

True when warning is enabled for the given filename. (The params parameter is ignored).

Returns:

  • (Boolean)


44
45
46
47
# File 'lib/jsduck/warning/basic.rb', line 44

def enabled?(filename="", params=[])
  # Find the most recently added rule that has an effect to our current item
  @rules.find {|r| r[:path_re].nil? || r[:path_re] =~ filename }[:enabled]
end

#set(enabled, path_pattern = nil, params = []) ⇒ Object

Enables or disables the warning. Optionally enables/disables it for files matching a path_pattern. params array is not used for the basic warning type.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jsduck/warning/basic.rb', line 23

def set(enabled, path_pattern=nil, params=[])
  if path_pattern
    # Prepend to the front of array, so we can use #find to
    # search for the latest rule.
    @rules.unshift({
      :enabled => enabled,
      :path_re => Regexp.new(Regexp.escape(path_pattern))
    })
  else
    # When no path specified, the warning is turned on/off
    # globally, so we can discard all the existing rules and
    # start over with just one.
    @rules = [{
      :enabled => enabled,
      :path_re => nil
    }]
  end
end