Skip to content

Commit

Permalink
Add MultilineScript linter
Browse files Browse the repository at this point in the history
Change-Id: I257b82c0bc5cdaed215579def77074cea8084deb
Reviewed-on: http://gerrit.causes.com/47624
Tested-by: jenkins <jenkins@brigade.com>
Reviewed-by: Shane da Silva <shane.dasilva@brigade.com>
  • Loading branch information
sds committed Mar 11, 2015
1 parent ee07324 commit 11ca42a
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
with 1.8-style hash rockets spanning multiple lines
* Fix `RuboCop` linter to not report `Style/EmptyElse` warnings for HAML code
containing `if`/`else` blocks containing only HAML filters
* Add `MultilineScript` linter to report scripts with trailing operators that
should be merged with the following line

## 0.11.0

Expand Down
3 changes: 3 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ linters:
MultilinePipe:
enabled: true

MultilineScript:
enabled: true

ObjectReferenceAttributes:
enabled: true

Expand Down
40 changes: 40 additions & 0 deletions lib/haml_lint/linter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Below is a list of linters supported by `haml-lint`, ordered alphabetically.
* [LeadingCommentSpace](#leadingcommentspace)
* [LineLength](#linelength)
* [MultilinePipe](#multilinepipe)
* [MultilineScript](#multilinescript)
* [ObjectReferenceAttributes](#objectreferenceattributes)
* [RuboCop](#rubocop)
* [RubyComments](#rubycomments)
Expand Down Expand Up @@ -242,6 +243,45 @@ The multiline bar was
almost always suggests an unnecessarily complicated template that should have
its logic extracted into a helper.

## MultilineScript

Don't span Ruby script over multiple lines using operators.

**Bad**
```haml
- if condition ||
- other_condition
Display something!
```

**Good**
```haml
- if condition || other_condition
Display something!
```

While writing code this way may sometimes work, it is actually a result of a
quirk in how HAML generates code from a template. While the following code
will compile and run:

```haml
- if condition ||
- other_condition
Display something!
```

...this code will fail with a parse error:

```haml
- if condition ||
- other_condition
Display something!
- else
Otherwise display this!
```

Thus it's best to stay away from writing code this way.

## ObjectReferenceAttributes

Don't use the
Expand Down
43 changes: 43 additions & 0 deletions lib/haml_lint/linter/multiline_script.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module HamlLint
# Checks scripts spread over multiple lines.
class Linter::MultilineScript < Linter
include LinterRegistry

# List of operators that can split a script into two lines that we want to
# alert on.
SPLIT_OPERATORS = %w[
|| or && and
||= &&=
^ << >> | &
<<= >>= |= &=
+ - * / ** %
+= -= *= /= **= %=
< <= <=> >= >
= == === != =~ !~
.. ...
? :
not
if unless while until
begin
].to_set

def visit_script(node)
check(node)
end

def visit_silent_script(node)
check(node)
end

private

def check(node)
operator = node.script[/\s+(\S+)\z/, 1]
if SPLIT_OPERATORS.include?(operator)
add_lint(node,
"Script with trailing operator `#{operator}` should be " \
'merged with the script on the following line')
end
end
end
end
34 changes: 34 additions & 0 deletions spec/haml_lint/linter/multiline_script_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'spec_helper'

describe HamlLint::Linter::MultilineScript do
include_context 'linter'

context 'when silent script is split with a Boolean operator' do
let(:haml) { <<-HAML }
- if condition ||
- true
Result
HAML

it { should report_lint line: 1 }
end

context 'when silent script is split with an equality operator' do
let(:haml) { <<-HAML }
- if condition ==
- something
Result
HAML

it { should report_lint line: 1 }
end

context 'when script is split with a binary operator' do
let(:haml) { <<-HAML }
= 1 +
= 2
HAML

it { should report_lint line: 1 }
end
end

0 comments on commit 11ca42a

Please sign in to comment.