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.
Add spaces_after_conditional ruler, refs #91.
- Loading branch information
Andrew Crump
committed
Sep 23, 2013
1 parent
6b645b0
commit 21da913
Showing
8 changed files
with
244 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require_relative '../ruler' | ||
|
||
class Tailor | ||
module Rulers | ||
class SpacesAfterConditionalRuler < Tailor::Ruler | ||
|
||
def initialize(config, options) | ||
super(config, options) | ||
add_lexer_observers :nl | ||
end | ||
|
||
def nl_update(current_lexed_line, lineno, column) | ||
measure(current_lexed_line, lineno) | ||
end | ||
|
||
# Checks to see if spacing is present after conditionals | ||
# | ||
# @param [Array] lexed_line The lexed line with a conditional | ||
# @param [Fixnum] lineno Line the problem was found on. | ||
# @param [Fixnum] column Column the problem was found on. | ||
def measure(lexed_line, lineno) | ||
|
||
idx = lexed_line.index do |pos, token, name| | ||
token == :on_kw and %w{if unless case}.include?(name) | ||
end | ||
|
||
expected_spaces = @config | ||
spaces = expected_spaces | ||
|
||
if idx | ||
column = lexed_line[idx].first.last | ||
pos, token, name = lexed_line[idx + 1] | ||
spaces = case token | ||
when :on_lparen then 0 | ||
when :on_sp | ||
next_token = lexed_line[idx + 2] | ||
next_token.first.last - pos.last | ||
end | ||
end | ||
|
||
if expected_spaces != spaces | ||
@problems << Problem.new(problem_type, lineno, column, | ||
"#{spaces} spaces after conditional at column #{column}, " + | ||
"expected #{expected_spaces}.", @options[:level]) | ||
end | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
require 'spec_helper' | ||
require_relative '../support/conditional_spacing_cases' | ||
require 'tailor/critic' | ||
require 'tailor/configuration/style' | ||
|
||
describe 'Conditional spacing' do | ||
|
||
def file_name | ||
self.class.description | ||
end | ||
|
||
def contents | ||
CONDITIONAL_SPACING[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 :no_space_after_if do | ||
it 'warns when there is no space after an if statement' do | ||
critic.check_file(file_name, style.to_hash) | ||
expect(critic.problems[file_name]).to eql [{ | ||
:type => 'spaces_after_conditional', | ||
:line => 1, | ||
:column=> 0, | ||
:message=> '0 spaces after conditional at column 0, expected 1.', | ||
:level=> :error | ||
}] | ||
end | ||
it 'warns with the correct number of expected spaces' do | ||
style.spaces_after_conditional 2, level: :error | ||
critic.check_file(file_name, style.to_hash) | ||
expect(critic.problems[file_name]).to eql [{ | ||
:type => 'spaces_after_conditional', | ||
:line => 1, | ||
:column=> 0, | ||
:message=> '0 spaces after conditional at column 0, expected 2.', | ||
:level=> :error | ||
}] | ||
end | ||
it 'does not warn if spaces are set to zero' do | ||
style.spaces_after_conditional 0, level: :error | ||
critic.check_file(file_name, style.to_hash) | ||
expect(critic.problems[file_name]).to be_empty | ||
end | ||
it 'does not warn if spaces are disabled' do | ||
style.spaces_after_conditional 2, level: :off | ||
critic.check_file(file_name, style.to_hash) | ||
expect(critic.problems[file_name]).to be_empty | ||
end | ||
end | ||
|
||
context :space_after_if do | ||
it 'does not warn when there is a space after the if' do | ||
critic.check_file(file_name, style.to_hash) | ||
expect(critic.problems[file_name]).to be_empty | ||
end | ||
it 'warns if spaces has been set to zero' do | ||
style.spaces_after_conditional 0, level: :error | ||
critic.check_file(file_name, style.to_hash) | ||
expect(critic.problems[file_name]).to eql [{ | ||
:type => 'spaces_after_conditional', | ||
:line => 1, | ||
:column=> 0, | ||
:message=> '1 spaces after conditional at column 0, expected 0.', | ||
:level=> :error | ||
}] | ||
end | ||
end | ||
|
||
context :no_parens do | ||
it 'never warns' do | ||
critic.check_file(file_name, style.to_hash) | ||
expect(critic.problems[file_name]).to be_empty | ||
end | ||
end | ||
|
||
context :nested_parens do | ||
it 'warns when there is no space after an if statement' do | ||
critic.check_file(file_name, style.to_hash) | ||
expect(critic.problems[file_name]).to eql [{ | ||
:type => 'spaces_after_conditional', | ||
:line => 1, | ||
:column=> 0, | ||
:message=> '0 spaces after conditional at column 0, expected 1.', | ||
:level=> :error | ||
}] | ||
end | ||
end | ||
|
||
context :no_space_after_unless do | ||
it 'warns when there is no space after an unless statement' do | ||
critic.check_file(file_name, style.to_hash) | ||
expect(critic.problems[file_name]).to eql [{ | ||
:type => 'spaces_after_conditional', | ||
:line => 1, | ||
:column=> 0, | ||
:message=> '0 spaces after conditional at column 0, expected 1.', | ||
:level=> :error | ||
}] | ||
end | ||
end | ||
|
||
context :space_after_unless do | ||
it 'does not warn when there is space after an unless statement' do | ||
critic.check_file(file_name, style.to_hash) | ||
expect(critic.problems[file_name]).to be_empty | ||
end | ||
end | ||
|
||
context :no_space_after_case do | ||
it 'warns when there is no space after a case statement' do | ||
critic.check_file(file_name, style.to_hash) | ||
expect(critic.problems[file_name]).to eql [{ | ||
:type => 'spaces_after_conditional', | ||
:line => 1, | ||
:column=> 5, | ||
:message=> '0 spaces after conditional at column 5, expected 1.', | ||
:level=> :error | ||
}] | ||
end | ||
end | ||
|
||
context :space_after_case do | ||
it 'does not warn when there is space after a case statement' 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
CONDITIONAL_SPACING = {} | ||
|
||
CONDITIONAL_SPACING['no_space_after_if'] = | ||
%q{if(foo) | ||
end} | ||
|
||
CONDITIONAL_SPACING['space_after_if'] = | ||
%q{if (foo) | ||
end} | ||
|
||
CONDITIONAL_SPACING['no_parens'] = | ||
%q{if foo | ||
end} | ||
|
||
CONDITIONAL_SPACING['nested_parens'] = | ||
%q{if(foo(bar)) | ||
end} | ||
|
||
CONDITIONAL_SPACING['no_space_after_unless'] = | ||
%q{unless(foo) | ||
end} | ||
|
||
CONDITIONAL_SPACING['space_after_unless'] = | ||
%q{unless (foo) | ||
end} | ||
|
||
CONDITIONAL_SPACING['no_space_after_case'] = | ||
%q{puts case(true) | ||
when true then 'a' | ||
when false then 'b' | ||
end} | ||
|
||
CONDITIONAL_SPACING['space_after_case'] = | ||
%q{puts case (true) | ||
when true then 'a' | ||
when false then 'b' | ||
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