From b66937e6527d63c6eabcd6afa023fa579958815f Mon Sep 17 00:00:00 2001 From: Maxime Lapointe Date: Wed, 29 Nov 2023 09:50:44 -0500 Subject: [PATCH] Make node_for_line return the last possible node when nothing matches exactly --- lib/haml_lint/tree/root_node.rb | 20 ++++++++++++++++++-- spec/haml_lint/linter/line_length_spec.rb | 13 +++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/haml_lint/tree/root_node.rb b/lib/haml_lint/tree/root_node.rb index c5b6bd82..dd8e5eae 100644 --- a/lib/haml_lint/tree/root_node.rb +++ b/lib/haml_lint/tree/root_node.rb @@ -17,9 +17,25 @@ def file # @param line [Integer] the line number of the node # @return [HamlLint::Node] def node_for_line(line) - find(-> { HamlLint::Tree::NullNode.new }) do |node| - node.line_numbers.cover?(line) && node != self + each do |node| + return node if node.line_numbers.cover?(line) && node != self end + + # Because HAML doesn't leave any trace in the nodes when it merges lines that + # end with a comma, it's harder to assign a node to the second line here: + # = some_call user, + # foo, bar + # So if the simple strategy (above) doesn't work, we try to see if we check if the last node + # that was before the requested line was one that could have been merged. If so, we use that one. + best_guess = nil + each do |node| + best_guess = node if node != self && node.line_numbers.end < line + end + + # There are the cases were the merging without traces can happen + return best_guess if best_guess && [:script, :silent_script, :tag].include?(best_guess.type) + + HamlLint::Tree::NullNode.new end end end diff --git a/spec/haml_lint/linter/line_length_spec.rb b/spec/haml_lint/linter/line_length_spec.rb index bdda0591..140a4367 100644 --- a/spec/haml_lint/linter/line_length_spec.rb +++ b/spec/haml_lint/linter/line_length_spec.rb @@ -75,4 +75,17 @@ it { should_not report_lint } end + + context 'when there is a directive before a long line that is a continued line' do + let(:haml) do + <<-HAML + -# haml-lint:disable LineLength + = link_to user, + class: some_unfortunately_very_extremely_definitely_over_90_characters_helper_method_with_even_more_characters + -# haml-lint:enable LineLength + HAML + end + + it { should_not report_lint } + end end