Skip to content

Commit

Permalink
[rubocop#3666] Enable Uncommunicative cops & add AllowedNames
Browse files Browse the repository at this point in the history
  • Loading branch information
garettarrowood committed Dec 28, 2017
1 parent 7e82e5f commit a559956
Show file tree
Hide file tree
Showing 19 changed files with 158 additions and 111 deletions.
26 changes: 26 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,32 @@ Naming/PredicateName:
- def_node_matcher
- def_node_search

Naming/UncommunicativeBlockParamName:
AllowedNames:
- 'child_node1'
- 'child_node2'
- 'expression1'
- 'expression2'
- 'node1'
- 'node2'
- 'operator1'
- 'operator2'
- 'range1'
- 'range2'
- 'token1'
- 'token2'
- 'var1'
- 'var2'

Naming/UncommunicativeMethodArgName:
AllowedNames:
- 'node1'
- 'node2'
- 'range1'
- 'range2'
- 'token1'
- 'token2'

Style/FrozenStringLiteralComment:
EnforcedStyle: always

Expand Down
12 changes: 0 additions & 12 deletions config/disabled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,6 @@ Layout/MultilineAssignmentLayout:
StyleGuide: '#indent-conditional-assignment'
Enabled: false

Naming/UncommunicativeBlockParamName:
Description: >-
Checks for block parameter names that contain capital letters,
end in numbers, or do not meet a minimal length.
Enabled: false

Naming/UncommunicativeMethodArgName:
Description: >-
Checks for method argument names that contain capital letters,
end in numbers, or do not meet a minimal length.
Enabled: false

# By default, the rails cops are not run. Override in project or home
# directory .rubocop.yml files, or by giving the -R/--rails option.
Rails:
Expand Down
12 changes: 12 additions & 0 deletions config/enabled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,18 @@ Naming/PredicateName:
StyleGuide: '#bool-methods-qmark'
Enabled: true

Naming/UncommunicativeBlockParamName:
Description: >-
Checks for block parameter names that contain capital letters,
end in numbers, or do not meet a minimal length.
Enabled: true

Naming/UncommunicativeMethodArgName:
Description: >-
Checks for method argument names that contain capital letters,
end in numbers, or do not meet a minimal length.
Enabled: true

Naming/VariableName:
Description: 'Use the configured style when naming variables.'
StyleGuide: '#snake-case-symbols-methods-vars'
Expand Down
32 changes: 16 additions & 16 deletions lib/rubocop/cop/layout/extra_spacing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def investigate(processed_source)
@corrected = Set.new
end

processed_source.tokens.each_cons(2) do |t1, t2|
check_tokens(processed_source.ast, t1, t2)
processed_source.tokens.each_cons(2) do |token1, token2|
check_tokens(processed_source.ast, token1, token2)
end
end

Expand All @@ -64,16 +64,16 @@ def assignment_tokens
Set.new(tokens.uniq(&:line))
end

def check_tokens(ast, t1, t2)
return if t2.type == :tNL
def check_tokens(ast, token1, token2)
return if token2.type == :tNL

if force_equal_sign_alignment? &&
@asgn_tokens.include?(t2) &&
(@asgn_lines.include?(t2.line - 1) ||
@asgn_lines.include?(t2.line + 1))
check_assignment(t2)
@asgn_tokens.include?(token2) &&
(@asgn_lines.include?(token2.line - 1) ||
@asgn_lines.include?(token2.line + 1))
check_assignment(token2)
else
check_other(t1, t2, ast)
check_other(token1, token2, ast)
end
end

Expand All @@ -95,8 +95,8 @@ def should_aligned_with_preceding_line?(token)
@asgn_lines.include?(token.line - 1)
end

def check_other(t1, t2, ast)
extra_space_range(t1, t2) do |range|
def check_other(token1, token2, ast)
extra_space_range(token1, token2) do |range|
# Unary + doesn't appear as a token and needs special handling.
next if ignored_range?(ast, range.begin_pos)
next if unary_plus_non_offense?(range)
Expand All @@ -105,14 +105,14 @@ def check_other(t1, t2, ast)
end
end

def extra_space_range(t1, t2)
return if t1.line != t2.line
def extra_space_range(token1, token2)
return if token1.line != token2.line

start_pos = t1.end_pos
end_pos = t2.begin_pos - 1
start_pos = token1.end_pos
end_pos = token2.begin_pos - 1
return if end_pos <= start_pos

return if allow_for_alignment? && aligned_tok?(t2)
return if allow_for_alignment? && aligned_tok?(token2)

yield range_between(start_pos, end_pos)
end
Expand Down
10 changes: 6 additions & 4 deletions lib/rubocop/cop/layout/space_before_comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ class SpaceBeforeComment < Cop
MSG = 'Put a space before an end-of-line comment.'.freeze

def investigate(processed_source)
processed_source.tokens.each_cons(2) do |t1, t2|
next unless t2.comment?
next unless t1.line == t2.line
add_offense(t2.pos, location: t2.pos) if t1.pos.end == t2.pos.begin
processed_source.tokens.each_cons(2) do |token1, token2|
next unless token2.comment?
next unless token1.line == token2.line
if token1.pos.end == token2.pos.begin
add_offense(token2.pos, location: token2.pos)
end
end
end

Expand Down
33 changes: 18 additions & 15 deletions lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,25 @@ def hash_literal_with_braces(node)
yield begin_index, end_index
end

def check(t1, t2)
def check(token1, token2)
# No offense if line break inside.
return if t1.line < t2.line
return if t2.comment? # Also indicates there's a line break.
return if token1.line < token2.line
return if token2.comment? # Also indicates there's a line break.

is_empty_braces = t1.left_brace? && t2.right_curly_brace?
expect_space = expect_space?(t1, t2)
is_empty_braces = token1.left_brace? && token2.right_curly_brace?
expect_space = expect_space?(token1, token2)

if offense?(t1, expect_space)
incorrect_style_detected(t1, t2, expect_space, is_empty_braces)
if offense?(token1, expect_space)
incorrect_style_detected(token1, token2,
expect_space, is_empty_braces)
else
correct_style_detected
end
end

def expect_space?(t1, t2)
is_same_braces = t1.type == t2.type
is_empty_braces = t1.left_brace? && t2.right_curly_brace?
def expect_space?(token1, token2)
is_same_braces = token1.type == token2.type
is_empty_braces = token1.left_brace? && token2.right_curly_brace?

if is_same_braces && style == :compact
false
Expand All @@ -111,16 +112,18 @@ def expect_space?(t1, t2)
end
end

def incorrect_style_detected(t1, t2, expect_space, is_empty_braces)
brace = (t1.text == '{' ? t1 : t2).pos
def incorrect_style_detected(token1, token2,
expect_space, is_empty_braces)
brace = (token1.text == '{' ? token1 : token2).pos
range = expect_space ? brace : space_range(brace)
add_offense(
range,
location: range,
message: message(brace, is_empty_braces, expect_space)
) do
style = expect_space ? :no_space : :space
ambiguous_or_unexpected_style_detected(style, t1.text == t2.text)
ambiguous_or_unexpected_style_detected(style,
token1.text == token2.text)
end
end

Expand All @@ -132,8 +135,8 @@ def ambiguous_or_unexpected_style_detected(style, is_match)
end
end

def offense?(t1, expect_space)
has_space = t1.space_after?
def offense?(token1, expect_space)
has_space = token1.space_after?
expect_space ? !has_space : has_space
end

Expand Down
14 changes: 7 additions & 7 deletions lib/rubocop/cop/layout/space_inside_parens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ def autocorrect(range)
private

def each_extraneous_space(tokens)
tokens.each_cons(2) do |t1, t2|
next unless parens?(t1, t2)
tokens.each_cons(2) do |token1, token2|
next unless parens?(token1, token2)

# If the second token is a comment, that means that a line break
# follows, and that the rules for space inside don't apply.
next if t2.comment?
next unless t2.line == t1.line && t1.space_after?
next if token2.comment?
next unless token2.line == token1.line && token1.space_after?

yield range_between(t1.end_pos, t2.begin_pos)
yield range_between(token1.end_pos, token2.begin_pos)
end
end

def parens?(t1, t2)
t1.left_parens? || t2.right_parens?
def parens?(token1, token2)
token1.left_parens? || token2.right_parens?
end
end
end
Expand Down
22 changes: 14 additions & 8 deletions lib/rubocop/cop/lint/implicit_string_concatenation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ class ImplicitStringConcatenation < Cop
'arguments, separate them with a comma.'.freeze

def on_dstr(node)
each_bad_cons(node) do |child1, child2|
range = child1.source_range.join(child2.source_range)
message = format(MSG, display_str(child1), display_str(child2))
each_bad_cons(node) do |child_node1, child_node2|
range = child_node1.source_range.join(child_node2.source_range)
message = format(MSG,
display_str(child_node1),
display_str(child_node2))
if node.parent && node.parent.array_type?
message << FOR_ARRAY
elsif node.parent && node.parent.send_type?
Expand All @@ -46,17 +48,17 @@ def on_dstr(node)
private

def each_bad_cons(node)
node.children.each_cons(2) do |child1, child2|
node.children.each_cons(2) do |child_node1, child_node2|
# `'abc' 'def'` -> (dstr (str "abc") (str "def"))
next unless string_literal?(child1) && string_literal?(child2)
next unless child1.last_line == child2.first_line
next unless string_literals?(child_node1, child_node2)
next unless child_node1.last_line == child_node2.first_line

# Make sure we don't flag a string literal which simply has
# embedded newlines
# `"abc\ndef"` also -> (dstr (str "abc") (str "def"))
next unless child1.source[-1] == ending_delimiter(child1)
next unless child_node1.source[-1] == ending_delimiter(child_node1)

yield child1, child2
yield child_node1, child_node2
end
end

Expand All @@ -74,6 +76,10 @@ def string_literal?(node)
(node.dstr_type? && node.children.all? { |c| string_literal?(c) })
end

def string_literals?(node1, node2)
string_literal?(node1) && string_literal?(node2)
end

def display_str(node)
if node.source =~ /\n/
str_content(node).inspect
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/lint/unneeded_disable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def trailing_range?(ranges, range)
ranges
.drop_while { |r| !r.equal?(range) }
.each_cons(2)
.map { |r1, r2| r1.end.join(r2.begin).source }
.map { |range1, range2| range1.end.join(range2.begin).source }
.all? { |intervening| intervening =~ /\A\s*,\s*\Z/ }
end

Expand Down
6 changes: 3 additions & 3 deletions lib/rubocop/cop/lint/unreachable_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ class UnreachableCode < Cop
def on_begin(node)
expressions = *node

expressions.each_cons(2) do |e1, e2|
next unless flow_expression?(e1)
expressions.each_cons(2) do |expression1, expression2|
next unless flow_expression?(expression1)

add_offense(e2)
add_offense(expression2)
end
end

Expand Down
8 changes: 5 additions & 3 deletions lib/rubocop/cop/mixin/documentation_comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ def documentation_comment?(node)
end
end

def preceding_comment?(n1, n2)
n1 && n2 && precede?(n2, n1) &&
comment_line?(n2.loc.expression.source)
# The args node1 & node2 may represent a RuboCop::AST::Node
# or a Parser::Source::Comment. Both respond to #loc.
def preceding_comment?(node1, node2)
node1 && node2 && precede?(node2, node1) &&
comment_line?(node2.loc.expression.source)
end

def preceding_lines(node)
Expand Down
15 changes: 8 additions & 7 deletions lib/rubocop/cop/mixin/space_after_punctuation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ def investigate(processed_source)
private

def each_missing_space(tokens)
tokens.each_cons(2) do |t1, t2|
next unless kind(t1)
next unless space_missing?(t1, t2)
next unless space_required_before?(t2)
tokens.each_cons(2) do |token1, token2|
next unless kind(token1)
next unless space_missing?(token1, token2)
next unless space_required_before?(token2)

yield t1
yield token1
end
end

def space_missing?(t1, t2)
t1.line == t2.line && t2.column == t1.column + offset
def space_missing?(token1, token2)
token1.line == token2.line &&
token2.column == token1.column + offset
end

def space_required_before?(token)
Expand Down
18 changes: 9 additions & 9 deletions lib/rubocop/cop/mixin/space_before_punctuation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ def investigate(processed_source)
private

def each_missing_space(tokens)
tokens.each_cons(2) do |t1, t2|
next unless kind(t2)
next unless space_missing?(t1, t2)
next if space_required_after?(t1)
tokens.each_cons(2) do |token1, token2|
next unless kind(token2)
next unless space_missing?(token1, token2)
next if space_required_after?(token1)

pos_before_punctuation = range_between(t1.end_pos,
t2.begin_pos)
pos_before_punctuation = range_between(token1.end_pos,
token2.begin_pos)

yield t2, pos_before_punctuation
yield token2, pos_before_punctuation
end
end

def space_missing?(t1, t2)
t1.line == t2.line && t2.begin_pos > t1.end_pos
def space_missing?(token1, token2)
token1.line == token2.line && token2.begin_pos > token1.end_pos
end

def space_required_after?(token)
Expand Down
Loading

0 comments on commit a559956

Please sign in to comment.