This repository has been archived by the owner on Aug 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'unnecessary-string-interpolation' of https://github.com…
…/acrmp/tailor Relates to gh-149
- Loading branch information
Showing
10 changed files
with
273 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
87 changes: 87 additions & 0 deletions
87
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,87 @@ | ||
require_relative '../ruler' | ||
|
||
class Tailor | ||
module Rulers | ||
class AllowUnnecessaryInterpolationRuler < Tailor::Ruler | ||
|
||
EVENTS = [ | ||
:on_embexpr_beg, | ||
:on_embexpr_end, | ||
:on_rbrace, | ||
: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 | ||
if no_content?(tokens) and one_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 line_number(token) | ||
token.first.first | ||
end | ||
|
||
def no_content?(tokens) | ||
! tokens.map { |t| t[1] }.include?(:on_tstring_content) | ||
end | ||
|
||
def one_expression?(tokens) | ||
tokens.select { |t| t[1] == :on_embexpr_beg }.size == 1 and | ||
tokens.select do |t| | ||
t[1] == :on_embexpr_end or t[1] == :on_rbrace | ||
end.any? | ||
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,117 @@ | ||
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=> :warn | ||
}] | ||
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_variables 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=> :warn | ||
}, | ||
{ | ||
:type => 'unnecessary_string_interpolation', | ||
:line => 1, | ||
:column=> 17, | ||
:message=> 'Variable interpolated unnecessarily', | ||
:level=> :warn | ||
} | ||
] | ||
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=> :warn | ||
}] | ||
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,45 @@ | ||
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_variables'] = | ||
%q{puts "#{bing}#{bar}" | ||
} | ||
|
||
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