-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdetails_has_summary.rb
More file actions
79 lines (57 loc) · 2.06 KB
/
details_has_summary.rb
File metadata and controls
79 lines (57 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# frozen_string_literal: true
require_relative "../../custom_helpers"
require_relative "../../tag_tree_helpers"
module ERBLint
module Linters
module GitHub
module Accessibility
class DetailsHasSummary < Linter
include ERBLint::Linters::CustomHelpers
include ERBLint::Linters::TagTreeHelpers
include LinterRegistry
MESSAGE = "<details> elements need to have explicit <summary> element"
def run(processed_source)
has_summary = false
(tags, tag_tree) = build_tag_tree(processed_source)
tags.each do |tag|
next if tag.name != "details" || tag.closing?
details = tag_tree[tag]
details[:children].each do |child|
if child[:tag].name == "summary"
has_summary = true
break
end
erb_nodes = extract_erb_nodes(child[:tag].node)
next if erb_nodes.blank?
code = ""
erb_nodes.each do |erb_node|
_, _, code_node = *erb_node
code += code_node.children.first
end
ast = erb_ast(code)
ast = ast.children.first if ast.type == :block
next unless ast.method_name == :content_tag
if ast.arguments.first.value.to_s == "summary"
has_summary = true
break
end
end
generate_offense(self.class, processed_source, details[:tag]) unless has_summary
has_summary = false
end
rule_disabled?(processed_source)
end
private
def extract_erb_nodes(node)
return node if node.type == :erb
return nil unless node.type == :text
node.children.select { |x| x.try(:type) == :erb }
end
def erb_ast(code)
RuboCop::AST::ProcessedSource.new(code, RUBY_VERSION.to_f).ast
end
end
end
end
end
end