Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from jonas054/more_rules
More rules
- Loading branch information
Showing
27 changed files
with
247 additions
and
47 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
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,38 @@ | ||
# encoding: utf-8 | ||
|
||
module Rubocop | ||
module Cop | ||
class DefParentheses < Cop | ||
ERROR_MESSAGE = ['Use def with parentheses when there are arguments.', | ||
"Omit the parentheses in defs when the method " + | ||
"doesn't accept any arguments."] | ||
EMPTY_PARAMS = [:params, nil, nil, nil, nil, nil] | ||
|
||
def inspect(file, source, tokens, sexp) | ||
each(:def, sexp) do |def_sexp| | ||
pos = def_sexp[1][-1] | ||
case def_sexp[2][0] | ||
when :params | ||
if def_sexp[2] != EMPTY_PARAMS | ||
add_offence(:convention, pos.lineno, ERROR_MESSAGE[0]) | ||
end | ||
when :paren | ||
if def_sexp[2][1] == EMPTY_PARAMS | ||
method_name_ix = tokens.index { |t| t.pos == pos } | ||
start = method_name_ix + 1 | ||
rparen_ix = start + tokens[start..-1].index { |t| t.text == ')' } | ||
first_body_token = tokens[(rparen_ix + 1)..-1].find do |t| | ||
not whitespace?(t) | ||
end | ||
if first_body_token.pos.lineno > pos.lineno | ||
# Only report offence if there's a line break after | ||
# the empty parens. | ||
add_offence(:convention, pos.lineno, ERROR_MESSAGE[1]) | ||
end | ||
end | ||
end | ||
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
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
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,49 @@ | ||
# encoding: utf-8 | ||
|
||
module Rubocop | ||
module Cop | ||
class IfThenElse < Cop | ||
ERROR_MESSAGE = { | ||
multiline_if_then: | ||
'Never use then for multi-line if/unless.', | ||
one_liner: | ||
'Favor the ternary operator (?:) over if/then/else/end constructs.', | ||
semicolon: | ||
'Never use if x; Use the ternary operator instead.' | ||
} | ||
|
||
def inspect(file, source, tokens, sexp) | ||
tokens.each_with_index do |t, ix| | ||
if t.type == :on_kw && ['if', 'unless'].include?(t.text) | ||
error = ERROR_MESSAGE[kind_of_if(tokens, ix + 1)] | ||
add_offence(:convention, t.pos.lineno, error) if error | ||
end | ||
end | ||
end | ||
|
||
def kind_of_if(tokens, ix) | ||
then_found = false | ||
tokens[ix..-1].each do |t| | ||
case t.type | ||
when :on_kw | ||
case t.text | ||
when 'then' then then_found = true | ||
when 'end' then return :one_liner | ||
end | ||
when :on_ignored_nl, :on_nl | ||
break | ||
when :on_semicolon | ||
return :semicolon | ||
when :on_comment | ||
break if t.text =~ /\n/ | ||
when :on_sp | ||
nil | ||
else | ||
then_found = false | ||
end | ||
end | ||
then_found ? :multiline_if_then : nil | ||
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
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
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
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
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,48 @@ | ||
# encoding: utf-8 | ||
|
||
require 'spec_helper' | ||
|
||
module Rubocop | ||
module Cop | ||
describe DefParentheses do | ||
let (:def_par) { DefParentheses.new } | ||
|
||
it 'reports an offence for def with parameters but no parens' do | ||
src = ['def func a, b', | ||
'end'] | ||
inspect_source(def_par, '', src) | ||
def_par.offences.map(&:message).should == | ||
['Use def with parentheses when there are arguments.'] | ||
end | ||
|
||
it 'reports an offence for def with empty parens' do | ||
src = ['def func()', | ||
'end'] | ||
inspect_source(def_par, '', src) | ||
def_par.offences.map(&:message).should == | ||
["Omit the parentheses in defs when the method doesn't accept any " + | ||
"arguments."] | ||
end | ||
|
||
it 'accepts def with arg and parens' do | ||
src = ['def func(a)', | ||
'end'] | ||
inspect_source(def_par, '', src) | ||
def_par.offences.map(&:message).should == [] | ||
end | ||
|
||
it 'accepts def with no args and no parens' do | ||
src = ['def func', | ||
'end'] | ||
inspect_source(def_par, '', src) | ||
def_par.offences.map(&:message).should == [] | ||
end | ||
|
||
it 'accepts empty parentheses in one liners' do | ||
src = ["def to_s() join '/' end"] | ||
inspect_source(def_par, '', src) | ||
def_par.offences.map(&:message).should == [] | ||
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
Oops, something went wrong.