Skip to content

Commit

Permalink
Fix UnnecessaryStringOutput on literal strings with method calls
Browse files Browse the repository at this point in the history
`UnnecessaryStringOutput` would incorrectly report a lint on lines like
the following:

  = 'user'.pluralize(@users.count)

Fix it by actually parsing the script and linting if the resulting type
is a string or interpolated string.

Change-Id: Id35b9ce1e406df3252c54cd73e8c778785718b8a
Reviewed-on: https://gerrit.brigade.com/48481
Tested-by: jenkins <jenkins@brigade.com>
Reviewed-by: Shane da Silva <shane.dasilva@brigade.com>
  • Loading branch information
sds committed Apr 1, 2015
1 parent 6ee58fd commit 5c63794
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* Add `MultilineScript` linter to report scripts with trailing operators that
should be merged with the following line
* Add `AltText` linter to report missing `alt` attributes on `img` tags
* Fix `UnnecessaryStringOutput` to not report warnings for strings with methods
called on them

## 0.11.0

Expand Down
5 changes: 4 additions & 1 deletion lib/haml_lint/linter/unnecessary_string_output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ def visit_script(node)
private

def outputs_string_literal?(script_node)
%w[' "].include?(script_node.script.lstrip[0])
tree = parse_ruby(script_node.script)
[:str, :dstr].include?(tree.type)
rescue ::Parser::SyntaxError # rubocop:disable Lint/HandleExceptions
# Gracefully ignore syntax errors, as that's managed by a different linter
end
end
end
6 changes: 6 additions & 0 deletions spec/haml_lint/linter/unnecessary_string_output_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,10 @@

it { should_not report_lint }
end

context 'when script outputs literal string with method called on it' do
let(:haml) { "= 'user'.pluralize(@users.count)" }

it { should_not report_lint }
end
end

0 comments on commit 5c63794

Please sign in to comment.