diff --git a/CHANGELOG.md b/CHANGELOG.md index 75a10e1f..6dd1ba1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## master (unreleased) * Fix `Checkstyle` output format to handle lints with no associated linter +* Ignore comments in `SpaceBeforeScript` linter ## 0.18.1 diff --git a/lib/haml_lint/linter/space_before_script.rb b/lib/haml_lint/linter/space_before_script.rb index 85d011bd..1c285308 100644 --- a/lib/haml_lint/linter/space_before_script.rb +++ b/lib/haml_lint/linter/space_before_script.rb @@ -7,7 +7,9 @@ class Linter::SpaceBeforeScript < Linter MESSAGE_FORMAT = 'The %s symbol should have one space separating it from code'.freeze - def visit_tag(node) # rubocop:disable Metrics/CyclomaticComplexity + ALLOWED_SEPARATORS = [' ', '#'].freeze + + def visit_tag(node) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/LineLength, Metrics/PerceivedComplexity # If this tag has inline script return unless node.contains_script? @@ -26,6 +28,8 @@ def visit_tag(node) # rubocop:disable Metrics/CyclomaticComplexity end end + return if tag_with_text[index] == '#' # Ignore code comments + # Check if the character before the start of the script is a space # (need to do it this way as the parser strips whitespace from node) return unless tag_with_text[index - 1] != ' ' @@ -48,7 +52,7 @@ def visit_silent_script(node) def missing_space?(node) text = node.script - text[0] != ' ' if text + !ALLOWED_SEPARATORS.include?(text[0]) if text end end end diff --git a/spec/haml_lint/linter/space_before_script_spec.rb b/spec/haml_lint/linter/space_before_script_spec.rb index 6addbf28..fdbfe471 100644 --- a/spec/haml_lint/linter/space_before_script_spec.rb +++ b/spec/haml_lint/linter/space_before_script_spec.rb @@ -51,6 +51,15 @@ HAML it { should report_lint line: 2 } + + context 'and is a comment' do + let(:haml) { <<-HAML } + %p=# A comment + %p=#A comment + HAML + + it { should_not report_lint } + end end context 'when inline script has a separating space' do