From 2d7ac524b39a66f5f9c95cda12ca6a2578a51c70 Mon Sep 17 00:00:00 2001 From: Shane da Silva Date: Fri, 5 Dec 2014 19:09:39 +0000 Subject: [PATCH] Output dummy puts statements for plain text nodes We were getting false positives about ternary statements from the `RuboCop` lint for code like the following: - if something Some plain text - else Some other plain text This was because the code being generated ignored plain text nodes, so it would produce: if something else end ...causing the lint to be reported. The solution was to output dummy `puts` statements for plain text nodes. Change-Id: I86f110b32ee3503482c937a6ae673251545aab15 Reviewed-on: http://gerrit.causes.com/45070 Tested-by: jenkins Reviewed-by: Shane da Silva --- CHANGELOG.md | 2 ++ lib/haml_lint/script_extractor.rb | 6 ++++++ spec/haml_lint/script_extractor_spec.rb | 10 +++++++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0dd5817a..f1199f3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ report lints. * Fix bug where any `haml` version 4.0.6 or later would not remove the special end-of-document marker from parse trees +* Fix bug where RuboCop's `Style/OneLineConditional` lint would incorrectly be + reported for HAML code with `if`/`else` statements ## 0.8.0 diff --git a/lib/haml_lint/script_extractor.rb b/lib/haml_lint/script_extractor.rb index 733cbfee..dc14abdc 100644 --- a/lib/haml_lint/script_extractor.rb +++ b/lib/haml_lint/script_extractor.rb @@ -42,6 +42,12 @@ def visit_root(_node) yield # Collect lines of code from children end + def visit_plain(node) + # Comment out the actual text as we don't want to deal with RuboCop + # StringQuotes lints + add_line("puts # #{node.text}", node) + end + def visit_tag(node) additional_attributes = node.dynamic_attributes_sources diff --git a/spec/haml_lint/script_extractor_spec.rb b/spec/haml_lint/script_extractor_spec.rb index f43fbcf9..7b15b939 100644 --- a/spec/haml_lint/script_extractor_spec.rb +++ b/spec/haml_lint/script_extractor_spec.rb @@ -12,6 +12,14 @@ it { should == '' } end + context 'with plain text' do + let(:haml) { <<-HAML } + Hello world + HAML + + it { should == 'puts # Hello world' } + end + context 'with only tags with text content' do let(:haml) { <<-HAML } %h1 Hello World @@ -20,7 +28,7 @@ %b Ipsum HAML - it { should == '' } + it { should == 'puts # Lorem' } end context 'with a silent script node' do