Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix rescue assignment check on begin block assigned #1

Closed
wants to merge 61 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
5508fd1
Add a Strict mode to Style/MutableConstants
rrosenblum Jul 3, 2018
ec5673b
Merge pull request #6126 from rrosenblum/strict_mutable_constants
koic Feb 16, 2019
937d098
Add expect_no_corrections helper
Feb 10, 2019
6834afe
Convert more examples to expect_correction
Feb 10, 2019
32d7bf7
Move unreleased change log entry to master section
koic Feb 16, 2019
bc0b8b4
Mark Style/ModuleFunction as unsafe autocorrect
dduugg Dec 28, 2018
ee14e9b
Merge pull request #6613 from dduugg/unsafe-module-function
koic Feb 16, 2019
06fd3db
[Fix #6751] Prevent Style/OneLineConditional from breaking on retry k…
Drenmi Feb 11, 2019
9d94d10
Add new BreakNode extension
Drenmi Feb 12, 2019
d9c607f
Fix node destructuring for `DefinedNode` extension
Drenmi Feb 12, 2019
3ec2faa
Merge pull request #6752 from rubocop-hq/expect-no-corrections-helper
Drenmi Feb 17, 2019
4f1449b
Allow parens for optional keyword value calls
gsamokovarov Feb 12, 2019
508b051
Merge pull request #6765 from gsamokovarov/omit-parentheses-kwargs
koic Feb 17, 2019
333d650
Merge pull request #6756 from Drenmi/bugfix/retry-node
koic Feb 17, 2019
4105b22
Add `range_type?` which means `irange_type?` and `erange_type?`
koic Feb 17, 2019
b10af4f
Merge pull request #6773 from koic/add_range_type_predicate_method
koic Feb 18, 2019
ff34759
Fix invalid links in CHANGELOG
pocke Feb 18, 2019
1f51e4c
Merge pull request #6775 from pocke/fix-changelog-stype
koic Feb 18, 2019
e81e7a2
[Fix #6755] Prevent Style/TrailingCommaInArgument from breaking when …
Drenmi Feb 11, 2019
8fe4946
Merge pull request #6757 from Drenmi/bugfix/trailing-comma-in-argumen…
Drenmi Feb 19, 2019
a1796a1
Cut 0.65.0
Drenmi Feb 19, 2019
2e52af0
Merge pull request #6776 from Drenmi/release/0-65-0
Drenmi Feb 19, 2019
4876607
Language updates
sgerrand Feb 20, 2019
7a1baae
Merge pull request #6782 from sgerrand/patch-1
Drenmi Feb 21, 2019
95bb95a
Fix syntax whoopsie
bquorning Feb 21, 2019
ac62e04
Inline shared_context that is used only once
bquorning Feb 21, 2019
3b7befa
Remove unused `let`s
bquorning Feb 21, 2019
b448e0f
Test the right thing
bquorning Feb 21, 2019
c22d5f2
Only define same `let` once per context
bquorning Feb 21, 2019
396648b
Prefer `it_behaves_like` over `include_examples`
bquorning Feb 21, 2019
21e7ff4
Fix spec failing after using `it_behaves_like`
bquorning Feb 21, 2019
94ef6c1
Merge pull request #6786 from bquorning/improve-specs-thanks-to-rspectre
Drenmi Feb 22, 2019
9962e3b
Add link to Formatters page
vlad-ro Feb 22, 2019
96e9123
Merge pull request #6788 from vlad-ro/patch-1
koic Feb 22, 2019
5c957cd
[Fix #6688] Add `iterator?` to deprecated methods and prefer `block_g…
tejasbubane Feb 21, 2019
9ae94da
Merge pull request #6714 from tejasbubane/new-cop-kernal-iterator-pre…
Drenmi Feb 25, 2019
694d645
Align an example for `RSpec/ExpectOffense` cop
koic Feb 25, 2019
f5b6244
[Fix #6699] Fix a false negative for `Layout/IndentationWidth`
koic Feb 25, 2019
3d9131d
Merge pull request #6792 from koic/fix_infinite_loop_indentation_width
koic Feb 26, 2019
256a992
Merge pull request #6790 from koic/align_an_example_for_rspec_expect_…
Drenmi Feb 27, 2019
c30ecf0
[Fix #6777] Fix a false positive for `Style/TrivialAccessors`
koic Feb 28, 2019
abee2ca
Merge pull request #6795 from koic/fix_false_positive_for_trivial_acc…
koic Mar 1, 2019
9d6f4f6
Fix errors for `Style/ConditionalAssignment`, `Style/IdenticalConditi…
pocke Mar 1, 2019
d3a894a
Merge pull request #6799 from pocke/()
pocke Mar 2, 2019
68156ce
Fix auto-correction for Style/SymbolArray with array contains interpo…
pocke Mar 2, 2019
ae6a799
Merge pull request #6802 from pocke/fix-SymbolArray-auto-correct-with…
koic Mar 2, 2019
9612547
Fix false negative for Layout/SpaceAroundBlockParameters
pocke Mar 1, 2019
fa6f71c
Merge pull request #6797 from pocke/fix-for-Layout/SpaceAroundBlockPa…
pocke Mar 2, 2019
d21f6d7
Fix error for `Style/NumericLiterals` on a literal that contains spaces
pocke Mar 2, 2019
47a97b7
Merge pull request #6803 from pocke/numeric-literal-sapce
koic Mar 2, 2019
a75bda2
Fix auto-correction for `Style/Lambda` with no-space argument
pocke Mar 2, 2019
dc69686
Merge pull request #6801 from pocke/Style/Lambda
pocke Mar 3, 2019
2183874
Fix auto-correction of `Style/NumericLiterals` on numeric literal wit…
pocke Mar 2, 2019
344861d
Merge pull request #6804 from pocke/Style/NumericLiterals-exponent
koic Mar 4, 2019
806c9f6
Remove powerpack dependency
dduugg Mar 3, 2019
ac4c7bc
Exclude gemspec file by default for `Metrics/BlockLength`
koic Mar 5, 2019
4654809
Merge pull request #6810 from koic/exclude_gemspec_by_default_for_met…
Drenmi Mar 6, 2019
9e83ddd
Remove argument of `iterator?` and `block_given?`
znz Mar 6, 2019
50df7bb
Merge pull request #6815 from znz/fix-block_given-arity
koic Mar 6, 2019
fe6da44
add specs for Layout/RescueEnsureAlignment fix c8ab8d9
oehlschl Feb 16, 2019
1d58f96
fix rescue assignment check on begin block assigned
formigarafa Mar 5, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ Include the output of `rubocop -V` or `bundle exec rubocop -V` if using Bundler.

```
$ [bundle exec] rubocop -V
0.64.0 (using Parser 2.5.1.2, running on ruby 2.5.1 x86_64-linux)
0.65.0 (using Parser 2.5.1.2, running on ruby 2.5.1 x86_64-linux)
```
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Metrics/BlockLength:
- 'Rakefile'
- '**/*.rake'
- 'spec/**/*.rb'
- '**/*.gemspec'

Metrics/ModuleLength:
Exclude:
Expand Down
29 changes: 28 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,40 @@

### Bug fixes

* [#6763](https://github.com/rubocop-hq/rubocop/pull/6763): Fix false positives in range literals for `Style/MethodCallWithArgsParentheses` `omit_parentheses`. ([@gsamokovarov]][])
* [#6699](https://github.com/rubocop-hq/rubocop/issues/6699): Fix infinite loop for `Layout/IndentationWidth` and `Layout/IndentationConsistency` when bad modifier indentation before good method definition. ([@koic][])
* [#6777](https://github.com/rubocop-hq/rubocop/issues/6777): Fix a false positive for `Style/TrivialAccessors` when using trivial reader/writer methods at the top level. ([@koic][])
* [#6799](https://github.com/rubocop-hq/rubocop/pull/6799): Fix errors for `Style/ConditionalAssignment`, `Style/IdenticalConditionalBranches`, `Lint/ElseLayout`, and `Layout/IndentationWidth` with empty braces. ([@pocke][])
* [#6802](https://github.com/rubocop-hq/rubocop/pull/6802): Fix auto-correction for `Style/SymbolArray` with array contains interpolation when `EnforcedStyle` is `brackets`. ([@pocke][])
* [#6797](https://github.com/rubocop-hq/rubocop/pull/6797): Fix false negative for Layout/SpaceAroundBlockParameters on block parameter with parens. ([@pocke][])
* [#6803](https://github.com/rubocop-hq/rubocop/pull/6803): Fix error for `Style/NumericLiterals` on a literal that contains spaces. ([@pocke][])
* [#6801](https://github.com/rubocop-hq/rubocop/pull/6801): Fix auto-correction for `Style/Lambda` with no-space argument. ([@pocke][])
* [#6804](https://github.com/rubocop-hq/rubocop/pull/6804): Fix auto-correction of `Style/NumericLiterals` on numeric literal with exponent. ([@pocke][])

### Changes

* [#6688](https://github.com/rubocop-hq/rubocop/pull/6688): Add `iterator?` to deprecated methods and prefer `block_given?` instead. ([@tejasbubane][])
* [#6806](https://github.com/rubocop-hq/rubocop/pull/6806): Remove `powerpack` dependency. ([@dduugg][])
* [#6810](https://github.com/rubocop-hq/rubocop/pull/6810): Exclude gemspec file by default for `Metrics/BlockLength` cop. ([@koic][])

## 0.65.0 (2019-02-19)

### New features

* [#6126](https://github.com/rubocop-hq/rubocop/pull/6126): Add an experimental strict mode to `Style/MutableConstant` that will freeze all constants, rather than just literals. ([@rrosenblum][])

### Bug fixes

* [#6765](https://github.com/rubocop-hq/rubocop/pull/6765): Fix false positives in keyword arguments for `Style/MethodCallWithArgsParentheses` `omit_parentheses`. ([@gsamokovarov][])
* [#6763](https://github.com/rubocop-hq/rubocop/pull/6763): Fix false positives in range literals for `Style/MethodCallWithArgsParentheses` `omit_parentheses`. ([@gsamokovarov][])
* [#6748](https://github.com/rubocop-hq/rubocop/issues/6748): Fix `Style/RaiseArgs` auto-correction breaking in contexts that require parentheses. ([@drenmi][])
* [#6751](https://github.com/rubocop-hq/rubocop/issues/6751): Prevent `Style/OneLineConditional` from breaking on `retry` and `break` keywords. ([@drenmi][])
* [#6755](https://github.com/rubocop-hq/rubocop/issues/6755): Prevent `Style/TrailingCommaInArgument` from breaking when a safe method call is chained on the offending method. ([@drenmi][], [@hoshinotsuyoshi][])

### Changes

* [#6766](https://github.com/rubocop-hq/rubocop/pull/6766): Drop support for Ruby 2.2.0 and 2.2.1. ([@pocke][])
* [#6733](https://github.com/rubocop-hq/rubocop/pull/6733): Warn duplicated keys in `.rubocop.yml`. ([@pocke][])
* [#6613](https://github.com/rubocop-hq/rubocop/pull/6613): Mark `Style/ModuleFunction` as `SafeAutocorrect: false` and disable autocorrect by default. ([@dduugg][])

## 0.64.0 (2019-02-10)

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ automatically fix some of the problems for you.

[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/bbatsov/rubocop?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

**Please consider [supporting financially its ongoing development](#funding).**
**Please consider [financially supporting its ongoing development](#funding).**

## Installation

Expand All @@ -50,10 +50,10 @@ gem 'rubocop', require: false
RuboCop's development is moving at a very rapid pace and there are
often backward-incompatible changes between minor releases (since we
haven't reached version 1.0 yet). To prevent an unwanted RuboCop update you
might want to use a conservative version locking in your `Gemfile`:
might want to use a conservative version lock in your `Gemfile`:

```rb
gem 'rubocop', '~> 0.64.0', require: false
gem 'rubocop', '~> 0.65.0', require: false
```

## Quickstart
Expand Down
19 changes: 17 additions & 2 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1643,13 +1643,15 @@ Metrics/BlockLength:
Description: 'Avoid long blocks with many lines.'
Enabled: true
VersionAdded: '0.44'
VersionChanged: '0.58'
VersionChanged: '0.66'
CountComments: false # count full line comments?
Max: 25
ExcludedMethods:
# By default, exclude the `#refine` method, as it tends to have larger
# associated blocks.
- refine
Exclude:
- '**/*.gemspec'

Metrics/BlockNesting:
Description: 'Avoid excessive block nesting'
Expand Down Expand Up @@ -3490,11 +3492,13 @@ Style/ModuleFunction:
StyleGuide: '#module-function'
Enabled: true
VersionAdded: '0.11'
VersionChanged: '0.53'
VersionChanged: '0.65'
EnforcedStyle: module_function
SupportedStyles:
- module_function
- extend_self
Autocorrect: false
SafeAutoCorrect: false

Style/MultilineBlockChain:
Description: 'Avoid multi-line chains of blocks.'
Expand Down Expand Up @@ -3549,6 +3553,17 @@ Style/MutableConstant:
Description: 'Do not assign mutable objects to constants.'
Enabled: true
VersionAdded: '0.34'
VersionChanged: '0.65'
EnforcedStyle: literals
SupportedStyles:
# literals: freeze literals assigned to constants
# strict: freeze all constants
# Strict mode is considered an experimental feature. It has not been updated
# with an exhaustive list of all methods that will produce frozen objects so
# there is a decent chance of getting some false positives. Luckily, there is
# no harm in freezing an already frozen object.
- literals
- strict

Style/NegatedIf:
Description: >-
Expand Down
8 changes: 3 additions & 5 deletions lib/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@
require 'English'
require 'set'
require 'forwardable'
require 'powerpack/array/butfirst'
require 'powerpack/enumerable/drop_last'
require 'powerpack/hash/symbolize_keys'
require 'powerpack/string/blank'
require 'powerpack/string/strip_indent'
require 'unicode/display_width/no_string_ext'

require_relative 'rubocop/version'

require_relative 'rubocop/core_ext/string'
require_relative 'rubocop/path_util'
require_relative 'rubocop/file_finder'
require_relative 'rubocop/platform'
Expand All @@ -38,6 +34,7 @@
require_relative 'rubocop/ast/node/args_node'
require_relative 'rubocop/ast/node/array_node'
require_relative 'rubocop/ast/node/block_node'
require_relative 'rubocop/ast/node/break_node'
require_relative 'rubocop/ast/node/case_node'
require_relative 'rubocop/ast/node/def_node'
require_relative 'rubocop/ast/node/defined_node'
Expand All @@ -51,6 +48,7 @@
require_relative 'rubocop/ast/node/range_node'
require_relative 'rubocop/ast/node/regexp_node'
require_relative 'rubocop/ast/node/resbody_node'
require_relative 'rubocop/ast/node/retry_node'
require_relative 'rubocop/ast/node/send_node'
require_relative 'rubocop/ast/node/str_node'
require_relative 'rubocop/ast/node/super_node'
Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/ast/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Builder < Parser::Builders::Default
args: ArgsNode,
array: ArrayNode,
block: BlockNode,
break: BreakNode,
case: CaseNode,
def: DefNode,
defined?: DefinedNode,
Expand All @@ -34,6 +35,7 @@ class Builder < Parser::Builders::Default
pair: PairNode,
regexp: RegexpNode,
resbody: ResbodyNode,
retry: RetryNode,
csend: SendNode,
send: SendNode,
str: StrNode,
Expand Down
4 changes: 4 additions & 0 deletions lib/rubocop/ast/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,10 @@ def numeric_type?
int_type? || float_type?
end

def range_type?
irange_type? || erange_type?
end

def_node_matcher :guard_clause?, <<-PATTERN
[{(send nil? {:raise :fail} ...) return break next} single_line?]
PATTERN
Expand Down
17 changes: 17 additions & 0 deletions lib/rubocop/ast/node/break_node.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module RuboCop
module AST
# A node extension for `break` nodes. This will be used in place of a
# plain node when the builder constructs the AST, making its methods
# available to all `break` nodes within RuboCop.
class BreakNode < Node
include MethodDispatchNode
include ParameterizedNode

def arguments
[]
end
end
end
end
4 changes: 4 additions & 0 deletions lib/rubocop/ast/node/defined_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ module AST
class DefinedNode < Node
include ParameterizedNode
include MethodDispatchNode

def node_parts
[nil, :defined?, *to_a]
end
end
end
end
17 changes: 17 additions & 0 deletions lib/rubocop/ast/node/retry_node.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module RuboCop
module AST
# A node extension for `retry` nodes. This will be used in place of a
# plain node when the builder constructs the AST, making its methods
# available to all `retry` nodes within RuboCop.
class RetryNode < Node
include MethodDispatchNode
include ParameterizedNode

def arguments
[]
end
end
end
end
2 changes: 1 addition & 1 deletion lib/rubocop/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def cops_of_department(cops, department)

def config_lines(cop)
cnf = @config_store.for(Dir.pwd).for_cop(cop)
cnf.to_yaml.lines.to_a.butfirst.map { |line| ' ' + line }
cnf.to_yaml.lines.to_a.drop(1).map { |line| ' ' + line }
end

def display_warning_summary(warnings)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/correctors/for_to_each_corrector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def collection_source
end

def requires_parentheses?
collection_node.irange_type? || collection_node.erange_type?
collection_node.range_type?
end

def end_position
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def call(corrector)
attr_reader :block_node, :method, :arguments

def remove_unparenthesized_whitespace(corrector)
return unless !arguments.empty? && !arguments.parenthesized_call?
return if arguments.empty? || arguments.parenthesized_call?

remove_leading_whitespace(corrector)
remove_trailing_whitespace(corrector)
Expand Down Expand Up @@ -69,10 +69,8 @@ def remove_leading_whitespace(corrector)
end

def remove_trailing_whitespace(corrector)
corrector.remove_preceding(
block_begin,
block_begin.begin_pos - arguments.source_range.end_pos - 1
)
size = block_begin.begin_pos - arguments.source_range.end_pos - 1
corrector.remove_preceding(block_begin, size) if size > 0
end

def replace_delimiters(corrector)
Expand Down
30 changes: 25 additions & 5 deletions lib/rubocop/cop/layout/indentation_width.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class IndentationWidth < Cop # rubocop:disable Metrics/ClassLength

SPECIAL_MODIFIERS = %w[private protected].freeze

def_node_matcher :access_modifier?, <<-PATTERN
[(send ...) access_modifier?]
PATTERN

def on_rescue(node)
_begin_node, *_rescue_nodes, else_node = *node
check_indentation(node.loc.else, else_node)
Expand Down Expand Up @@ -161,15 +165,12 @@ def autocorrect(node)
private

def check_members(base, members)
check_indentation(base, members.first)
check_indentation(base, select_check_member(members.first))

return unless members.any? && members.first.begin_type?

if indentation_consistency_style == 'rails'
each_member(members) do |member, previous_modifier|
check_indentation(previous_modifier, member,
indentation_consistency_style)
end
check_members_for_rails_style(members)
else
members.first.children.each do |member|
next if member.send_type? && member.access_modifier?
Expand All @@ -179,6 +180,23 @@ def check_members(base, members)
end
end

def select_check_member(member)
return unless member

if access_modifier?(member.children.first)
member.children.first
else
member
end
end

def check_members_for_rails_style(members)
each_member(members) do |member, previous_modifier|
check_indentation(previous_modifier, member,
indentation_consistency_style)
end
end

def each_member(members)
previous_modifier = nil
members.first.children.each do |member|
Expand Down Expand Up @@ -326,6 +344,8 @@ def starts_with_access_modifier?(body_node)
return unless body_node.begin_type?

starting_node = body_node.children.first
return unless starting_node

starting_node.send_type? && starting_node.bare_access_modifier?
end

Expand Down
12 changes: 9 additions & 3 deletions lib/rubocop/cop/layout/rescue_ensure_alignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ def alignment_source(node, starting_loc)
node.loc.begin
when :def, :defs, :class, :module
node.loc.name
when :lvasgn
node.child_nodes.first.loc.begin
else
# It is a wrapper with access modifier.
node.child_nodes.first.loc.name
Expand All @@ -114,11 +116,15 @@ def alignment_source(node, starting_loc)
def alignment_node(node)
ancestor_node = ancestor_node(node)

return ancestor_node if ancestor_node.nil? ||
ancestor_node.kwbegin_type?
return if ancestor_node.nil?

assignment_node = assignment_node(ancestor_node)
return assignment_node if same_line?(ancestor_node, assignment_node)

if assignment_node && same_line?(ancestor_node, assignment_node)
return assignment_node
end

return ancestor_node if ancestor_node.kwbegin_type?

access_modifier_node = access_modifier_node(ancestor_node)
return access_modifier_node unless access_modifier_node.nil?
Expand Down
20 changes: 13 additions & 7 deletions lib/rubocop/cop/layout/space_around_block_parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,22 @@ def last_end_pos_inside_pipes(pos)
end

def check_each_arg(args)
args.children.butfirst.each do |arg|
expr = arg.source_range
check_no_space(
range_with_surrounding_space(range: expr, side: :left).begin_pos,
expr.begin_pos - 1,
'Extra space before'
)
args.children.each do |arg|
check_arg(arg)
end
end

def check_arg(arg)
arg.children.each { |a| check_arg(a) } if arg.mlhs_type?

expr = arg.source_range
check_no_space(
range_with_surrounding_space(range: expr, side: :left).begin_pos,
expr.begin_pos - 1,
'Extra space before'
)
end

def check_space(space_begin_pos, space_end_pos, range, msg)
return if space_begin_pos != space_end_pos

Expand Down