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 double quotes, refs turboladen#91.
- Loading branch information
Andrew Crump
committed
Sep 28, 2013
1 parent
5a324e1
commit 617527e
Showing
12 changed files
with
250 additions
and
55 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
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
63 changes: 63 additions & 0 deletions
63
lib/tailor/rulers/allow_unnecessary_double_quotes_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,63 @@ | ||
require_relative '../ruler' | ||
|
||
class Tailor | ||
module Rulers | ||
class AllowUnnecessaryDoubleQuotesRuler < Tailor::Ruler | ||
|
||
def initialize(config, options) | ||
super(config, options) | ||
add_lexer_observers :nl | ||
end | ||
|
||
def nl_update(lexed_line, lineno, column) | ||
quotes(lexed_line).each do |quote| | ||
unless contains_embedded_expression?(quote) || | ||
contains_escape_sequence?(quote) | ||
measure(lineno, column(quote.first)) | ||
end | ||
end | ||
end | ||
|
||
# Checks to see if the double_quotes are unnecessary. | ||
# | ||
# @param [Fixnum] lineno Line the problem was found on. | ||
# @param [Fixnum] column Column the problem was found on. | ||
def measure(lineno, column) | ||
@problems << Problem.new('unnecessary_double_quotes', lineno, column, | ||
"Unnecessary double quotes at column #{column}, " + | ||
'expected single quotes.', @options[:level]) | ||
end | ||
|
||
private | ||
|
||
def contains_embedded_expression?(tokens) | ||
tokens.any? { |t| t[1] == :on_embexpr_beg } | ||
end | ||
|
||
def contains_escape_sequence?(tokens) | ||
tokens.any? do |t| | ||
t[1] == :on_tstring_content and t[2].match(/\\[a-z]+/) | ||
end | ||
end | ||
|
||
def quotes(tokens) | ||
tokens.select do |t| | ||
true if (double_quote_start?(t))..(double_quote_end?(t)) | ||
end.slice_before { |t| double_quote_start?(t) }.reject { |q| q.empty? } | ||
end | ||
|
||
def column(token) | ||
token[0][1] | ||
end | ||
|
||
def double_quote_start?(token) | ||
token[1] == :on_tstring_beg and token[2] == '"' | ||
end | ||
|
||
def double_quote_end?(token) | ||
token[1] == :on_tstring_end and token[2] == '"' | ||
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,97 @@ | ||
require 'spec_helper' | ||
require_relative '../support/string_quoting_cases' | ||
require 'tailor/critic' | ||
require 'tailor/configuration/style' | ||
|
||
describe 'String Quoting' do | ||
|
||
def file_name | ||
self.class.description | ||
end | ||
|
||
def contents | ||
QUOTING[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 :single_quotes_no_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 | ||
|
||
context :double_quotes_with_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 | ||
|
||
context :double_quotes_no_interpolation do | ||
it 'warns that double quotes are unnecessary' do | ||
critic.check_file(file_name, style.to_hash) | ||
expect(critic.problems[file_name]).to eql [{ | ||
:type => 'unnecessary_double_quotes', | ||
:line => 1, | ||
:column => 6, | ||
:message => 'Unnecessary double quotes at column 6, expected single quotes.', | ||
:level => :warn | ||
}] | ||
end | ||
end | ||
|
||
context :double_quotes_no_interpolation_twice do | ||
it 'warns that double quotes are unnecessary' do | ||
critic.check_file(file_name, style.to_hash) | ||
expect(critic.problems[file_name]).to eql [ | ||
{ | ||
:type => 'unnecessary_double_quotes', | ||
:line => 1, | ||
:column => 6, | ||
:message => 'Unnecessary double quotes at column 6, expected single quotes.', | ||
:level => :warn | ||
}, | ||
{ | ||
:type => 'unnecessary_double_quotes', | ||
:line => 1, | ||
:column => 14, | ||
:message => 'Unnecessary double quotes at column 14, expected single quotes.', | ||
:level => :warn | ||
} | ||
] | ||
end | ||
end | ||
|
||
context :nested_quotes 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 :escape_sequence do | ||
it 'does not warn when a double quoted string contains a newline' 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
include Pizza | ||
def barrel_roll | ||
puts "DOABARRELROLL!" | ||
puts 'DOABARRELROLL!' | ||
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
Oops, something went wrong.