forked from turboladen/tailor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ruler for unnecessary interpolation, refs turboladen#91.
- Loading branch information
Andrew Crump
committed
Sep 27, 2013
1 parent
6b645b0
commit 807919d
Showing
10 changed files
with
257 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
lib/tailor/rulers/allow_unnecessary_interpolation_ruler.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
require_relative '../ruler' | ||
|
||
class Tailor | ||
module Rulers | ||
class AllowUnnecessaryInterpolationRuler < Tailor::Ruler | ||
|
||
EVENTS = [ | ||
:on_embexpr_beg, | ||
:on_tstring_beg, | ||
:on_tstring_content, | ||
:on_tstring_end | ||
] | ||
|
||
def initialize(config, options) | ||
super(config, options) | ||
reset_tokens | ||
add_lexer_observers :ignored_nl, :nl | ||
end | ||
|
||
def ignored_nl_update(lexed_line, lineno, column) | ||
add_string_tokens(lexed_line) | ||
end | ||
|
||
def nl_update(lexed_line, lineno, column) | ||
add_string_tokens(lexed_line) | ||
each_string(@tokens).each do |string| | ||
measure(line_number(@tokens.first), string) | ||
end | ||
reset_tokens | ||
end | ||
|
||
# Checks if variables are interpolated unnecessarily | ||
# | ||
# @param [Array] tokens The filtered tokens | ||
def measure(lineno, tokens) | ||
return if @config | ||
unless has_content?(tokens) or no_expression?(tokens) | ||
@problems << Problem.new('unnecessary_string_interpolation', lineno, | ||
column(tokens.first), 'Variable interpolated unnecessarily', | ||
@options[:level]) | ||
end | ||
end | ||
|
||
private | ||
|
||
def add_string_tokens(lexed_line) | ||
@tokens += string_tokens(lexed_line) | ||
end | ||
|
||
def column(token) | ||
token.first.last + 1 | ||
end | ||
|
||
def each_string(tokens) | ||
tokens.select do |t| | ||
true if (t[1] == :on_tstring_beg)..(t[1] == :on_tstring_end) | ||
end.slice_before { |t| t[1] == :on_tstring_beg } | ||
end | ||
|
||
def has_content?(tokens) | ||
tokens.map { |t| t[1] }.include?(:on_tstring_content) | ||
end | ||
|
||
def line_number(token) | ||
token.first.first | ||
end | ||
|
||
def no_expression?(tokens) | ||
tokens.size < 3 | ||
end | ||
|
||
def reset_tokens | ||
@tokens = [] | ||
end | ||
|
||
def string_tokens(lexed_line) | ||
lexed_line.select { |t| EVENTS.include?(t[1]) } | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
require 'spec_helper' | ||
require_relative '../support/string_interpolation_cases' | ||
require 'tailor/critic' | ||
require 'tailor/configuration/style' | ||
|
||
describe 'String interpolation cases' do | ||
|
||
def file_name | ||
self.class.description | ||
end | ||
|
||
def contents | ||
INTERPOLATION[file_name] || begin | ||
raise "Example not found: #{file_name}" | ||
end | ||
end | ||
|
||
before do | ||
Tailor::Logger.stub(:log) | ||
FakeFS.activate! | ||
FileUtils.touch file_name | ||
File.open(file_name, 'w') { |f| f.write contents } | ||
end | ||
|
||
let(:critic) { Tailor::Critic.new } | ||
|
||
let(:style) do | ||
style = Tailor::Configuration::Style.new | ||
style.trailing_newlines 0, level: :off | ||
style.allow_invalid_ruby true, level: :off | ||
style | ||
end | ||
|
||
context :one_variable_interpolated_only do | ||
it 'warns when interpolation is used unnecessarily' do | ||
critic.check_file(file_name, style.to_hash) | ||
expect(critic.problems[file_name]).to eql [{ | ||
:type => 'unnecessary_string_interpolation', | ||
:line => 1, | ||
:column=> 6, | ||
:message=> 'Variable interpolated unnecessarily', | ||
:level=> :error | ||
}] | ||
end | ||
end | ||
|
||
context :mixed_content_and_expression do | ||
it 'does not warn' do | ||
critic.check_file(file_name, style.to_hash) | ||
expect(critic.problems[file_name]).to be_empty | ||
end | ||
end | ||
|
||
context :no_string do | ||
it 'does not warn' do | ||
critic.check_file(file_name, style.to_hash) | ||
expect(critic.problems[file_name]).to be_empty | ||
end | ||
end | ||
|
||
context :two_strings_with_unnecessary_interpolation do | ||
it 'warns against both strings' do | ||
critic.check_file(file_name, style.to_hash) | ||
expect(critic.problems[file_name]).to eql [ | ||
{ | ||
:type => 'unnecessary_string_interpolation', | ||
:line => 1, | ||
:column=> 6, | ||
:message=> 'Variable interpolated unnecessarily', | ||
:level=> :error | ||
}, | ||
{ | ||
:type => 'unnecessary_string_interpolation', | ||
:line => 1, | ||
:column=> 17, | ||
:message=> 'Variable interpolated unnecessarily', | ||
:level=> :error | ||
} | ||
] | ||
end | ||
end | ||
|
||
context :multiline_string_with_unnecessary_interpolation do | ||
it 'warns against the first line' do | ||
critic.check_file(file_name, style.to_hash) | ||
expect(critic.problems[file_name]).to eql [{ | ||
:type => 'unnecessary_string_interpolation', | ||
:line => 1, | ||
:column=> 6, | ||
:message=> 'Variable interpolated unnecessarily', | ||
:level=> :error | ||
}] | ||
end | ||
end | ||
|
||
context :multiline_word_list do | ||
it 'does not warn' do | ||
critic.check_file(file_name, style.to_hash) | ||
expect(critic.problems[file_name]).to be_empty | ||
end | ||
end | ||
|
||
context :nested_interpolation do | ||
it 'does not warn' do | ||
critic.check_file(file_name, style.to_hash) | ||
expect(critic.problems[file_name]).to be_empty | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
INTERPOLATION = {} | ||
|
||
INTERPOLATION['one_variable_interpolated_only'] = | ||
%q{puts "#{bing}" | ||
} | ||
|
||
INTERPOLATION['mixed_content_and_expression'] = | ||
%q{puts "hello: #{bing}" | ||
} | ||
|
||
INTERPOLATION['no_string'] = | ||
%q{puts bing | ||
} | ||
|
||
INTERPOLATION['two_strings_with_unnecessary_interpolation'] = | ||
%q{puts "#{foo}" + "#{bar}" | ||
} | ||
|
||
INTERPOLATION['multiline_string_with_unnecessary_interpolation'] = | ||
%q{puts "#{foo + | ||
bar - | ||
baz}" | ||
} | ||
|
||
INTERPOLATION['multiline_word_list'] = | ||
%q{%w{ | ||
foo | ||
bar | ||
baz | ||
}} | ||
|
||
INTERPOLATION['nested_interpolation'] = | ||
%q[def friendly_time(time) | ||
if hours < 24 | ||
"#{(hours > 0) ? "#{hours} hour" : '' }#{(hours > 1) ? 's' : ''}" + | ||
" #{(mins > 0) ? "#{mins} minute" : '' }#{(mins > 1) ? 's' : ''}" + | ||
" #{seconds} second#{(seconds > 1) ? "s" : ''} ago" | ||
else | ||
time.to_s | ||
end | ||
end] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters