Skip to content

Commit

Permalink
Ignore comments in SpaceBeforeScript
Browse files Browse the repository at this point in the history
Fixes #156
  • Loading branch information
sds committed Aug 28, 2016
1 parent 6d99611 commit 4cda2ae
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 6 additions & 2 deletions lib/haml_lint/linter/space_before_script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand All @@ -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] != ' '
Expand All @@ -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
9 changes: 9 additions & 0 deletions spec/haml_lint/linter/space_before_script_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4cda2ae

Please sign in to comment.