Class: ERBLint::Linters::NoUnusedDisable

Inherits:
ERBLint::Linter show all
Includes:
ERBLint::LinterRegistry
Defined in:
lib/erb_lint/linters/no_unused_disable.rb

Overview

Checks for unused disable comments.

Constant Summary

Constants included from ERBLint::LinterRegistry

ERBLint::LinterRegistry::CUSTOM_LINTERS_DIR, ERBLint::LinterRegistry::DEPRECATED_CUSTOM_LINTERS_DIR

Instance Attribute Summary

Attributes inherited from ERBLint::Linter

#config, #offenses

Instance Method Summary collapse

Methods included from ERBLint::LinterRegistry

clear, find_by_name, included, linters, load_custom_linters

Methods inherited from ERBLint::Linter

#add_offense, #clear_offenses, #enabled?, #excludes_file?, inherited, #initialize, #run_and_update_offense_status, support_autocorrect?

Constructor Details

This class inherits a constructor from ERBLint::Linter

Instance Method Details

#run(processed_source, offenses) ⇒ Object



11
12
13
14
15
16
17
18
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
44
# File 'lib/erb_lint/linters/no_unused_disable.rb', line 11

def run(processed_source, offenses)
  disabled_rules_and_line_number = {}

  processed_source.source_buffer.source_lines.each_with_index do |line, index|
    rule_disables = Utils::InlineConfigs.disabled_rules(line)
    next unless rule_disables

    rule_disables.split(",").each do |rule|
      disabled_rules_and_line_number[rule.strip] =
        (disabled_rules_and_line_number[rule.strip] ||= []).push(index + 1)
    end
  end

  offenses.each do |offense|
    rule_name = offense.linter.class.simple_name
    line_numbers = disabled_rules_and_line_number[rule_name]
    next unless line_numbers

    line_numbers.reject do |line_number|
      if (offense.source_range.line_span.first..offense.source_range.line_span.last).include?(line_number)
        disabled_rules_and_line_number[rule_name].delete(line_number)
      end
    end
  end

  disabled_rules_and_line_number.each do |rule, line_numbers|
    line_numbers.each do |line_number|
      add_offense(
        processed_source.source_buffer.line_range(line_number),
        "Unused erblint:disable comment for #{rule}",
      )
    end
  end
end