Commit 9aac289a authored by Vasilii Iakliushin's avatar Vasilii Iakliushin

Verify links and anchors for deeply nested urls

Contributes to https://gitlab.com/gitlab-org/gitlab/-/issues/328203

**Problem**

NodePattern#match returns the first matching subnode and ignores other
matches

**Solution**

Apply NodePattern#match for all node descendants
parent 77a544a4
...@@ -13,7 +13,7 @@ module HamlLint ...@@ -13,7 +13,7 @@ module HamlLint
DOCS_DIRECTORY = File.join(File.expand_path('../..', __dir__), 'doc') DOCS_DIRECTORY = File.join(File.expand_path('../..', __dir__), 'doc')
HELP_PATH_LINK_PATTERN = <<~PATTERN HELP_PATH_LINK_PATTERN = <<~PATTERN
`(send nil? {:help_page_url :help_page_path} $...) (send nil? {:help_page_url :help_page_path} $...)
PATTERN PATTERN
MARKDOWN_HEADER = %r{\A\#{1,6}\s+(?<header>.+)\Z}.freeze MARKDOWN_HEADER = %r{\A\#{1,6}\s+(?<header>.+)\Z}.freeze
...@@ -33,8 +33,17 @@ module HamlLint ...@@ -33,8 +33,17 @@ module HamlLint
private private
def check(node) def check(node)
match = extract_link_and_anchor(node) ast_tree = fetch_ast_tree(node)
return unless ast_tree
ast_tree.descendants.each do |child_node|
match = extract_link_and_anchor(child_node)
validate_node(node, match)
end
end
def validate_node(node, match)
return if match.empty? return if match.empty?
path_to_file = detect_path_to_file(match[:link]) path_to_file = detect_path_to_file(match[:link])
...@@ -49,11 +58,7 @@ module HamlLint ...@@ -49,11 +58,7 @@ module HamlLint
end end
end end
def extract_link_and_anchor(node) def extract_link_and_anchor(ast_tree)
ast_tree = fetch_ast_tree(node)
return {} unless ast_tree
link_match, attributes_match = ::RuboCop::NodePattern.new(HELP_PATH_LINK_PATTERN).match(ast_tree) link_match, attributes_match = ::RuboCop::NodePattern.new(HELP_PATH_LINK_PATTERN).match(ast_tree)
{ link: fetch_link(link_match), anchor: fetch_anchor(attributes_match) }.compact { link: fetch_link(link_match), anchor: fetch_anchor(attributes_match) }.compact
......
...@@ -80,6 +80,12 @@ RSpec.describe HamlLint::Linter::DocumentationLinks do ...@@ -80,6 +80,12 @@ RSpec.describe HamlLint::Linter::DocumentationLinks do
it { is_expected.to report_lint } it { is_expected.to report_lint }
end end
context 'when the second link is invalid' do
let(:haml) { ".data-form{ data: { url: #{link_pattern}('README.md'), wrong_url: #{link_pattern}('wrong.md') } }" }
it { is_expected.to report_lint }
end
end end
context 'help_page_path' do context 'help_page_path' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment