Class: PlatformosCheck::DisabledChecks

Inherits:
Object
  • Object
show all
Defined in:
lib/platformos_check/disabled_checks.rb

Constant Summary collapse

DISABLE_START =
'platformos-check-disable'
DISABLE_END =
'platformos-check-enable'
DISABLE_PREFIX_PATTERN =
/#{DISABLE_START}|#{DISABLE_END}/
ACTION_DISABLE_CHECKS =
:disable
ACTION_ENABLE_CHECKS =
:enable

Instance Method Summary collapse

Constructor Details

#initializeDisabledChecks

Returns a new instance of DisabledChecks.



12
13
14
15
16
17
# File 'lib/platformos_check/disabled_checks.rb', line 12

def initialize
  @disabled_checks = Hash.new do |hash, key|
    app_file, check_name = key
    hash[key] = DisabledCheck.new(app_file, check_name)
  end
end

Instance Method Details

#checks_missing_end_indexObject



54
55
56
57
58
# File 'lib/platformos_check/disabled_checks.rb', line 54

def checks_missing_end_index
  @disabled_checks.values
                  .select(&:missing_end_index?)
                  .map(&:name)
end

#disabled?(check, app_file, check_name, index) ⇒ Boolean

Returns:

  • (Boolean)


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

def disabled?(check, app_file, check_name, index)
  return true if check.ignored_patterns&.any? do |pattern|
    app_file&.relative_path&.fnmatch?(pattern)
  end

  @disabled_checks[[app_file, :all]]&.disabled?(index) ||
    @disabled_checks[[app_file, check_name]]&.disabled?(index)
end

#remove_disabled_offenses(checks) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/platformos_check/disabled_checks.rb', line 60

def remove_disabled_offenses(checks)
  checks.disableable.each do |check|
    check.offenses.reject! do |offense|
      disabled?(check, offense.app_file, offense.code_name, offense.start_index)
    end
  end
end

#update(node) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/platformos_check/disabled_checks.rb', line 19

def update(node)
  text = comment_text(node)
  if start_disabling?(text)
    checks_from_text(text).each do |check_name|
      disabled = @disabled_checks[[node.app_file, check_name]]
      disabled.start_index = node.start_index
      disabled.first_line = true if node.line_number == 1
    end
  elsif stop_disabling?(text)
    checks_from_text(text).each do |check_name|
      disabled = @disabled_checks[[node.app_file, check_name]]
      next unless disabled

      disabled.end_index = node.end_index
    end
  else
    # We want to disable checks inside comments
    # (e.g. html checks inside {% comment %})
    disabled = @disabled_checks[[node.app_file, :all]]
    unless disabled.first_line
      disabled.start_index = node.inner_markup_start_index
      disabled.end_index = node.inner_markup_end_index
    end
  end
end