Module: ERBLint::Linters::CustomHelpers

Instance Method Summary collapse

Instance Method Details

#basic_conditional_code_check(code) ⇒ Object

Map possible values from condition



40
41
42
43
# File 'lib/erblint-github/linters/custom_helpers.rb', line 40

def basic_conditional_code_check(code)
  conditional_match = code.match(/["'](.+)["']\sif|unless\s.+/) || code.match(/.+\s?\s["'](.+)["']\s:\s["'](.+)["']/)
  [conditional_match[1], conditional_match[2]].compact if conditional_match
end

#generate_offense(klass, processed_source, tag, message = nil, replacement = nil) ⇒ Object



27
28
29
30
31
32
# File 'lib/erblint-github/linters/custom_helpers.rb', line 27

def generate_offense(klass, processed_source, tag, message = nil, replacement = nil)
  message ||= klass::MESSAGE
  message += "\nLearn more at https://github.com/github/erblint-github#rules.\n"
  offense = ["#{simple_class_name}:#{message}", tag.node.loc.source].join("\n")
  add_offense(processed_source.to_source_range(tag.loc), offense, replacement)
end

#possible_attribute_values(tag, attr_name) ⇒ Object



34
35
36
37
# File 'lib/erblint-github/linters/custom_helpers.rb', line 34

def possible_attribute_values(tag, attr_name)
  value = tag.attributes[attr_name]&.value || nil
  basic_conditional_code_check(value || "") || [value].compact
end

#rule_disabled?(processed_source) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/erblint-github/linters/custom_helpers.rb', line 9

def rule_disabled?(processed_source)
  processed_source.parser.ast.descendants(:erb).each do |node|
    indicator_node, _, code_node, = *node
    indicator = indicator_node&.loc&.source
    comment = code_node&.loc&.source&.strip
    rule_name = simple_class_name

    if indicator == "#" && comment.start_with?("erblint:disable") && comment.match(rule_name)
      if @offenses.any?
        clear_offenses
      else
        add_offense(processed_source.to_source_range(code_node.loc),
                    "Unused erblint:disable comment for #{rule_name}")
      end
    end
  end
end

#simple_class_nameObject



49
50
51
# File 'lib/erblint-github/linters/custom_helpers.rb', line 49

def simple_class_name
  self.class.name.gsub("ERBLint::Linters::", "")
end

#tags(processed_source) ⇒ Object



45
46
47
# File 'lib/erblint-github/linters/custom_helpers.rb', line 45

def tags(processed_source)
  processed_source.parser.nodes_with_type(:tag).map { |tag_node| BetterHtml::Tree::Tag.from_node(tag_node) }
end