From f0722956b97ef4a824d5007757b0abb33574ec36 Mon Sep 17 00:00:00 2001
From: Peter Leitzen <pleitzen@gitlab.com>
Date: Fri, 3 Feb 2023 12:13:02 +0100
Subject: [PATCH] Linter/DocumentationLinks: Flag help links with receivers

Match the following use cases too:

= Rails.application.routes.url_helpers.help_page_path("invalid/path")

This commit also improves the speed of this lint rule by caching the
node pattern.
---
 haml_lint/linter/documentation_links.rb           |  7 ++++---
 spec/haml_lint/linter/documentation_links_spec.rb | 13 +++++--------
 2 files changed, 9 insertions(+), 11 deletions(-)

diff --git a/haml_lint/linter/documentation_links.rb b/haml_lint/linter/documentation_links.rb
index 0cabae40c4b5a..9d80344dd2023 100644
--- a/haml_lint/linter/documentation_links.rb
+++ b/haml_lint/linter/documentation_links.rb
@@ -9,11 +9,12 @@ class Linter
     class DocumentationLinks < Linter
       include ::HamlLint::LinterRegistry
       include ::Gitlab::Utils::Markdown
+      extend ::RuboCop::AST::NodePattern::Macros
 
       DOCS_DIRECTORY = File.join(File.expand_path('../..', __dir__), 'doc')
 
-      HELP_PATH_LINK_PATTERN = <<~PATTERN
-      (send nil? {:help_page_url :help_page_path} $...)
+      def_node_matcher :help_link, <<~PATTERN
+        (send _ {:help_page_url :help_page_path} $...)
       PATTERN
 
       MARKDOWN_HEADER = %r{\A\#{1,6}\s+(?<header>.+)\Z}.freeze
@@ -59,7 +60,7 @@ def validate_node(node, match)
       end
 
       def extract_link_and_anchor(ast_tree)
-        link_match, attributes_match = ::RuboCop::NodePattern.new(HELP_PATH_LINK_PATTERN).match(ast_tree)
+        link_match, attributes_match = help_link(ast_tree)
 
         { link: fetch_link(link_match), anchor: fetch_anchor(attributes_match) }.compact
       end
diff --git a/spec/haml_lint/linter/documentation_links_spec.rb b/spec/haml_lint/linter/documentation_links_spec.rb
index 380df49cde354..d47127d966124 100644
--- a/spec/haml_lint/linter/documentation_links_spec.rb
+++ b/spec/haml_lint/linter/documentation_links_spec.rb
@@ -6,7 +6,7 @@
 
 require_relative '../../../haml_lint/linter/documentation_links'
 
-RSpec.describe HamlLint::Linter::DocumentationLinks do
+RSpec.describe HamlLint::Linter::DocumentationLinks, feature_category: :tooling do
   include_context 'linter'
 
   shared_examples 'link validation rules' do |link_pattern|
@@ -95,11 +95,8 @@
     end
   end
 
-  context 'help_page_path' do
-    it_behaves_like 'link validation rules', 'help_page_path'
-  end
-
-  context 'help_page_url' do
-    it_behaves_like 'link validation rules', 'help_page_url'
-  end
+  it_behaves_like 'link validation rules', 'help_page_path'
+  it_behaves_like 'link validation rules', 'help_page_url'
+  it_behaves_like 'link validation rules', 'Rails.application.routes.url_helpers.help_page_url'
+  it_behaves_like 'link validation rules', 'Gitlab::Routing.url_helpers.help_page_url'
 end
-- 
GitLab