Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add &= and != to SpaceBeforeScript #203

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 10 additions & 5 deletions lib/haml_lint/linter/space_before_script.rb
Expand Up @@ -41,18 +41,23 @@ def visit_tag(node) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplex
def visit_script(node)
# Plain text nodes with interpolation are converted to script nodes, so we
# need to ignore them here.
return unless document.source_lines[node.line - 1].lstrip.start_with?('=')
record_lint(node, MESSAGE_FORMAT % '=') if missing_space?(node)
line = document.source_lines[node.line - 1].lstrip
return unless line.start_with?('=', '&=', '!=')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh. Today I learned that you can send multiple matchers to start_with?. 👍

text = if line.start_with?('=')
node.script
else
line[/=(.*)/, 1]
end
record_lint(node, MESSAGE_FORMAT % line[/[&!]?=/]) if missing_space?(text)
end

def visit_silent_script(node)
record_lint(node, MESSAGE_FORMAT % '-') if missing_space?(node)
record_lint(node, MESSAGE_FORMAT % '-') if missing_space?(node.script)
end

private

def missing_space?(node)
text = node.script
def missing_space?(text)
!ALLOWED_SEPARATORS.include?(text[0]) if text
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/haml_lint/tree/script_node.rb
Expand Up @@ -10,7 +10,7 @@ def parsed_script
HamlLint::ParsedRuby.new(HamlLint::RubyParser.new.parse(script))
end

# Returns the source for the script following the `-` marker.
# Returns the source for the script following the `=` marker.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.

#
# @return [String]
def script
Expand Down
61 changes: 61 additions & 0 deletions spec/haml_lint/linter/space_before_script_spec.rb
Expand Up @@ -33,6 +33,67 @@
it { should report_lint line: 2 }
end

context 'when unsafe script has separating space' do
let(:haml) { <<-HAML }
%span Hello
!= some_code
%span World
HAML

it { should_not report_lint }

context 'and nested' do
let(:haml) { <<-HAML }
%span Hello
%span
!= some_code
%span World
HAML

it { should_not report_lint }
end
end

context 'when unsafe script has no separating space' do
let(:haml) { <<-HAML }
%span Hello
!=some_code
%span World
HAML

it { should report_lint line: 2 }

context 'and inline' do
let(:haml) { <<-HAML }
%span Hello
%span!=some_code
%span World
HAML

it { should report_lint line: 2 }
end
end

context 'when escaping script has no separating space' do
let(:haml) { <<-HAML }
%span Hello
&=some_code
%span World
HAML

it { should report_lint line: 2 }

context 'and inline' do
let(:haml) { <<-HAML }
%span Hello
%span&=some_code
%span World
HAML

it { should report_lint line: 2 }
end
end

context 'when script has a separating space' do
let(:haml) { <<-HAML }
%span Hello
Expand Down