From 5c6379453083e8d1f506bd96097104f8d28128c2 Mon Sep 17 00:00:00 2001 From: Shane da Silva Date: Tue, 31 Mar 2015 21:48:05 -0700 Subject: [PATCH] Fix UnnecessaryStringOutput on literal strings with method calls `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 Reviewed-by: Shane da Silva --- CHANGELOG.md | 2 ++ lib/haml_lint/linter/unnecessary_string_output.rb | 5 ++++- spec/haml_lint/linter/unnecessary_string_output_spec.rb | 6 ++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4adee053..77121e83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/haml_lint/linter/unnecessary_string_output.rb b/lib/haml_lint/linter/unnecessary_string_output.rb index 90508423..3340d50d 100644 --- a/lib/haml_lint/linter/unnecessary_string_output.rb +++ b/lib/haml_lint/linter/unnecessary_string_output.rb @@ -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 diff --git a/spec/haml_lint/linter/unnecessary_string_output_spec.rb b/spec/haml_lint/linter/unnecessary_string_output_spec.rb index c4ccbe88..15df3765 100644 --- a/spec/haml_lint/linter/unnecessary_string_output_spec.rb +++ b/spec/haml_lint/linter/unnecessary_string_output_spec.rb @@ -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