From 7922badf42b2bc0eaf02effeb0fcd3e04c9bd487 Mon Sep 17 00:00:00 2001
From: Vasilii Iakliushin <viakliushin@gitlab.com>
Date: Wed, 13 Apr 2022 16:59:21 +0000
Subject: [PATCH] Fix edge case for DocumentationLinks linter

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

**Problem**

Code that ends with a `do` block definition is ignored by parser. The
linter can skip some of broken links because of that.

**Solution**

Add a workaround for this case.
---
 app/views/groups/milestones/index.html.haml       |  4 ++--
 haml_lint/linter/documentation_links.rb           | 10 +++++++++-
 spec/haml_lint/linter/documentation_links_spec.rb |  6 ++++++
 3 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/app/views/groups/milestones/index.html.haml b/app/views/groups/milestones/index.html.haml
index 048606e2db467..5c0487db0fc9c 100644
--- a/app/views/groups/milestones/index.html.haml
+++ b/app/views/groups/milestones/index.html.haml
@@ -12,7 +12,7 @@
         = link_to _('New milestone'), new_group_milestone_path(@group), class: "btn gl-button btn-confirm", data: { qa_selector: "new_group_milestone_link" }
 
   - if @milestones.blank?
-    = render 'shared/empty_states/milestones_tab', learn_more_path: help_page_path('user/group/milestones') do
+    = render 'shared/empty_states/milestones_tab', learn_more_path: help_page_path('user/project/milestones/index') do
       - if can?(current_user, :admin_milestone, @group)
         .text-center
           = link_to _('New milestone'), new_group_milestone_path(@group), class: "btn gl-button btn-confirm", data: { qa_selector: "new_group_milestone_link" }
@@ -26,7 +26,7 @@
             = render 'milestone', milestone: milestone
       = paginate @milestones, theme: "gitlab"
 - else
-  = render 'shared/empty_states/milestones', learn_more_path: help_page_path('user/group/milestones') do
+  = render 'shared/empty_states/milestones', learn_more_path: help_page_path('user/project/milestones/index') do
     - if can?(current_user, :admin_milestone, @group)
       .text-center
         = link_to _('New milestone'), new_group_milestone_path(@group), class: "btn gl-button btn-confirm", data: { qa_selector: "new_group_milestone_link" }
diff --git a/haml_lint/linter/documentation_links.rb b/haml_lint/linter/documentation_links.rb
index 8c696b26b13d6..0cabae40c4b5a 100644
--- a/haml_lint/linter/documentation_links.rb
+++ b/haml_lint/linter/documentation_links.rb
@@ -68,7 +68,15 @@ def fetch_ast_tree(node)
         # Sometimes links are provided via data attributes in html tag
         return node.parsed_attributes.syntax_tree if node.type == :tag
 
-        node.parsed_script.syntax_tree
+        parse_script(node).syntax_tree
+      end
+
+      def parse_script(node)
+        # It's a workaround for cases for scripts ending with "do"
+        # For some reason they don't parse correctly
+        code = node.script.delete_suffix(' do')
+
+        HamlLint::ParsedRuby.new(HamlLint::RubyParser.new.parse(code))
       end
 
       def detect_path_to_file(link)
diff --git a/spec/haml_lint/linter/documentation_links_spec.rb b/spec/haml_lint/linter/documentation_links_spec.rb
index 75002097d6983..f2aab4304c1f1 100644
--- a/spec/haml_lint/linter/documentation_links_spec.rb
+++ b/spec/haml_lint/linter/documentation_links_spec.rb
@@ -43,6 +43,12 @@
       let(:haml) { "= link_to 'Description', #{link_pattern}('wrong.md'), target: '_blank'" }
 
       it { is_expected.to report_lint }
+
+      context 'when haml ends with block definition' do
+        let(:haml) { "= link_to 'Description', #{link_pattern}('wrong.md') do" }
+
+        it { is_expected.to report_lint }
+      end
     end
 
     context 'when link with wrong file path is assigned to a variable' do
-- 
GitLab