rubocop 1.5.2 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +5 -2
  4. data/config/obsoletion.yml +196 -0
  5. data/lib/rubocop.rb +10 -0
  6. data/lib/rubocop/cli/command/suggest_extensions.rb +16 -44
  7. data/lib/rubocop/config_obsoletion.rb +63 -263
  8. data/lib/rubocop/config_obsoletion/changed_enforced_styles.rb +33 -0
  9. data/lib/rubocop/config_obsoletion/changed_parameter.rb +21 -0
  10. data/lib/rubocop/config_obsoletion/cop_rule.rb +34 -0
  11. data/lib/rubocop/config_obsoletion/extracted_cop.rb +44 -0
  12. data/lib/rubocop/config_obsoletion/parameter_rule.rb +44 -0
  13. data/lib/rubocop/config_obsoletion/removed_cop.rb +41 -0
  14. data/lib/rubocop/config_obsoletion/renamed_cop.rb +34 -0
  15. data/lib/rubocop/config_obsoletion/rule.rb +41 -0
  16. data/lib/rubocop/config_obsoletion/split_cop.rb +27 -0
  17. data/lib/rubocop/config_validator.rb +11 -4
  18. data/lib/rubocop/cop/base.rb +17 -15
  19. data/lib/rubocop/cop/cop.rb +2 -2
  20. data/lib/rubocop/cop/correctors/string_literal_corrector.rb +6 -8
  21. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +1 -1
  22. data/lib/rubocop/cop/layout/line_length.rb +6 -16
  23. data/lib/rubocop/cop/migration/department_name.rb +1 -1
  24. data/lib/rubocop/cop/mixin/string_help.rb +4 -1
  25. data/lib/rubocop/cop/naming/accessor_method_name.rb +15 -1
  26. data/lib/rubocop/cop/style/character_literal.rb +10 -11
  27. data/lib/rubocop/cop/style/float_division.rb +44 -1
  28. data/lib/rubocop/cop/style/if_unless_modifier.rb +4 -0
  29. data/lib/rubocop/cop/style/ip_addresses.rb +1 -1
  30. data/lib/rubocop/cop/style/perl_backrefs.rb +86 -9
  31. data/lib/rubocop/cop/style/redundant_argument.rb +14 -1
  32. data/lib/rubocop/cop/style/single_line_block_params.rb +30 -7
  33. data/lib/rubocop/cop/style/sole_nested_conditional.rb +12 -6
  34. data/lib/rubocop/cop/style/special_global_vars.rb +1 -13
  35. data/lib/rubocop/cop/style/string_concatenation.rb +19 -0
  36. data/lib/rubocop/cop/style/string_literals.rb +14 -8
  37. data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +4 -3
  38. data/lib/rubocop/formatter/emacs_style_formatter.rb +2 -0
  39. data/lib/rubocop/formatter/simple_text_formatter.rb +2 -0
  40. data/lib/rubocop/formatter/tap_formatter.rb +2 -0
  41. data/lib/rubocop/lockfile.rb +40 -0
  42. data/lib/rubocop/version.rb +1 -1
  43. metadata +14 -3
@@ -72,15 +72,14 @@ module RuboCop
72
72
 
73
73
  and_operator = if_branch.unless? ? ' && !' : ' && '
74
74
  if if_branch.modifier_form?
75
- correct_for_gurad_condition_style(corrector, node, if_branch, and_operator)
75
+ correct_for_guard_condition_style(corrector, node, if_branch, and_operator)
76
76
  else
77
77
  correct_for_basic_condition_style(corrector, node, if_branch, and_operator)
78
+ correct_for_comment(corrector, node, if_branch)
78
79
  end
79
-
80
- correct_for_comment(corrector, node, if_branch)
81
80
  end
82
81
 
83
- def correct_for_gurad_condition_style(corrector, node, if_branch, and_operator)
82
+ def correct_for_guard_condition_style(corrector, node, if_branch, and_operator)
84
83
  condition = if_branch.condition
85
84
  corrector.insert_after(node.condition, replacement_condition(and_operator, condition))
86
85
 
@@ -95,18 +94,25 @@ module RuboCop
95
94
  )
96
95
  corrector.replace(range, and_operator)
97
96
  corrector.remove(range_by_whole_lines(node.loc.end, include_final_newline: true))
98
- corrector.wrap(if_branch.condition, '(', ')') if if_branch.condition.or_type?
97
+ corrector.wrap(if_branch.condition, '(', ')') if wrap_condition?(if_branch.condition)
99
98
  end
100
99
 
101
100
  def correct_for_comment(corrector, node, if_branch)
101
+ return if config.for_cop('Style/IfUnlessModifier')['Enabled']
102
+
102
103
  comments = processed_source.comments_before_line(if_branch.source_range.line)
103
104
  comment_text = comments.map(&:text).join("\n") << "\n"
104
105
 
105
106
  corrector.insert_before(node.loc.keyword, comment_text) unless comments.empty?
106
107
  end
107
108
 
109
+ def wrap_condition?(node)
110
+ node.or_type? ||
111
+ (node.send_type? && node.arguments.any? && !node.parenthesized?)
112
+ end
113
+
108
114
  def replacement_condition(and_operator, condition)
109
- if condition.or_type?
115
+ if wrap_condition?(condition)
110
116
  "#{and_operator}(#{condition.source})"
111
117
  else
112
118
  "#{and_operator}#{condition.source}"
@@ -26,10 +26,6 @@ module RuboCop
26
26
  # puts $LAST_MATCH_INFO
27
27
  # puts $IGNORECASE
28
28
  # puts $ARGV # or ARGV
29
- # puts $MATCH
30
- # puts $PREMATCH
31
- # puts $POSTMATCH
32
- # puts $LAST_PAREN_MATCH
33
29
  #
34
30
  # @example EnforcedStyle: use_perl_names
35
31
  # # good
@@ -51,10 +47,6 @@ module RuboCop
51
47
  # puts $~
52
48
  # puts $=
53
49
  # puts $*
54
- # puts $&
55
- # puts $`
56
- # puts $'
57
- # puts $+
58
50
  #
59
51
  class SpecialGlobalVars < Base
60
52
  include ConfigurableEnforcedStyle
@@ -85,11 +77,7 @@ module RuboCop
85
77
  :$? => [:$CHILD_STATUS],
86
78
  :$~ => [:$LAST_MATCH_INFO],
87
79
  :$= => [:$IGNORECASE],
88
- :$* => %i[$ARGV ARGV],
89
- :$& => [:$MATCH],
90
- :$` => [:$PREMATCH],
91
- :$' => [:$POSTMATCH],
92
- :$+ => [:$LAST_PAREN_MATCH]
80
+ :$* => %i[$ARGV ARGV]
93
81
  }
94
82
 
95
83
  PERL_VARS =
@@ -11,6 +11,10 @@ module RuboCop
11
11
  # In those cases, it might be useful to extract statements to local
12
12
  # variables or methods which you can then interpolate in a string.
13
13
  #
14
+ # NOTE: When concatenation between two strings is broken over multiple
15
+ # lines, this cop does not register an offense; instead,
16
+ # `Style/LineEndConcatenation` will pick up the offense if enabled.
17
+ #
14
18
  # @example
15
19
  # # bad
16
20
  # email_with_name = user.name + ' <' + user.email + '>'
@@ -19,6 +23,10 @@ module RuboCop
19
23
  # email_with_name = "#{user.name} <#{user.email}>"
20
24
  # email_with_name = format('%s <%s>', user.name, user.email)
21
25
  #
26
+ # # accepted, line-end concatenation
27
+ # name = 'First' +
28
+ # 'Last'
29
+ #
22
30
  class StringConcatenation < Base
23
31
  include Util
24
32
  extend AutoCorrector
@@ -39,6 +47,7 @@ module RuboCop
39
47
 
40
48
  def on_send(node)
41
49
  return unless string_concatenation?(node)
50
+ return if line_end_concatenation?(node)
42
51
 
43
52
  topmost_plus_node = find_topmost_plus_node(node)
44
53
 
@@ -58,6 +67,16 @@ module RuboCop
58
67
 
59
68
  private
60
69
 
70
+ def line_end_concatenation?(node)
71
+ # If the concatenation happens at the end of the line,
72
+ # and both the receiver and argument are strings, allow
73
+ # `Style/LineEndConcatenation` to handle it instead.
74
+ node.receiver.str_type? &&
75
+ node.first_argument.str_type? &&
76
+ node.multiline? &&
77
+ node.source =~ /\+\s*\n/
78
+ end
79
+
61
80
  def find_topmost_plus_node(node)
62
81
  current = node
63
82
  while (parent = current.parent) && plus_node?(parent)
@@ -26,9 +26,10 @@ module RuboCop
26
26
  # "Just some text"
27
27
  # "No special chars or interpolation"
28
28
  # "Every string in #{project} uses double_quotes"
29
- class StringLiterals < Cop
29
+ class StringLiterals < Base
30
30
  include ConfigurableEnforcedStyle
31
31
  include StringLiteralsHelp
32
+ extend AutoCorrector
32
33
 
33
34
  MSG_INCONSISTENT = 'Inconsistent quote style.'
34
35
 
@@ -46,7 +47,7 @@ module RuboCop
46
47
  quote_styles = detect_quote_styles(node)
47
48
 
48
49
  if quote_styles.size > 1
49
- add_offense(node, message: MSG_INCONSISTENT)
50
+ register_offense(node, message: MSG_INCONSISTENT)
50
51
  else
51
52
  check_multiline_quote_style(node, quote_styles[0])
52
53
  end
@@ -54,11 +55,17 @@ module RuboCop
54
55
  ignore_node(node)
55
56
  end
56
57
 
57
- def autocorrect(node)
58
- StringLiteralCorrector.correct(node, style)
58
+ private
59
+
60
+ def autocorrect(corrector, node)
61
+ StringLiteralCorrector.correct(corrector, node, style)
59
62
  end
60
63
 
61
- private
64
+ def register_offense(node, message: nil)
65
+ add_offense(node, message: message || message(node)) do |corrector|
66
+ autocorrect(corrector, node)
67
+ end
68
+ end
62
69
 
63
70
  def all_string_literals?(nodes)
64
71
  nodes.all? { |n| n.str_type? || n.dstr_type? }
@@ -99,14 +106,13 @@ module RuboCop
99
106
  end
100
107
 
101
108
  def check_multiline_quote_style(node, quote)
102
- range = node.source_range
103
109
  children = node.children
104
110
  if unexpected_single_quotes?(quote)
105
111
  all_children_with_quotes = children.all? { |c| wrong_quotes?(c) }
106
- add_offense(node, location: range) if all_children_with_quotes
112
+ register_offense(node) if all_children_with_quotes
107
113
  elsif unexpected_double_quotes?(quote) &&
108
114
  !accept_child_double_quotes?(children)
109
- add_offense(node, location: range)
115
+ register_offense(node)
110
116
  end
111
117
  end
112
118
 
@@ -19,12 +19,13 @@ module RuboCop
19
19
  #
20
20
  # # good
21
21
  # result = "Tests #{success ? "PASS" : "FAIL"}"
22
- class StringLiteralsInInterpolation < Cop
22
+ class StringLiteralsInInterpolation < Base
23
23
  include ConfigurableEnforcedStyle
24
24
  include StringLiteralsHelp
25
+ extend AutoCorrector
25
26
 
26
- def autocorrect(node)
27
- StringLiteralCorrector.correct(node, style)
27
+ def autocorrect(corrector, node)
28
+ StringLiteralCorrector.correct(corrector, node, style)
28
29
  end
29
30
 
30
31
  private
@@ -27,6 +27,8 @@ module RuboCop
27
27
  "[Todo] #{offense.message}"
28
28
  elsif offense.corrected?
29
29
  "[Corrected] #{offense.message}"
30
+ elsif offense.correctable?
31
+ "[Correctable] #{offense.message}"
30
32
  else
31
33
  offense.message
32
34
  end
@@ -90,6 +90,8 @@ module RuboCop
90
90
  green('[Todo] ')
91
91
  elsif offense.corrected?
92
92
  green('[Corrected] ')
93
+ elsif offense.correctable?
94
+ yellow('[Correctable] ')
93
95
  else
94
96
  ''
95
97
  end
@@ -71,6 +71,8 @@ module RuboCop
71
71
  '[Todo] '
72
72
  elsif offense.corrected?
73
73
  '[Corrected] '
74
+ elsif offense.correctable?
75
+ '[Correctable] '
74
76
  else
75
77
  ''
76
78
  end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ # Encapsulation of a lockfile for use when checking for gems.
5
+ # Does not actually resolve gems, just parses the lockfile.
6
+ # @api private
7
+ class Lockfile
8
+ # Gems that the bundle depends on
9
+ def dependencies
10
+ return [] unless parser
11
+
12
+ parser.dependencies.values
13
+ end
14
+
15
+ # All activated gems, including transitive dependencies
16
+ def gems
17
+ return [] unless parser
18
+
19
+ # `Bundler::LockfileParser` returns `Bundler::LazySpecification` objects
20
+ # which are not resolved, so extract the dependencies from them
21
+ parser.dependencies.values.concat(parser.specs.flat_map(&:dependencies))
22
+ end
23
+
24
+ def includes_gem?(name)
25
+ gems.any? { |gem| gem.name == name }
26
+ end
27
+
28
+ private
29
+
30
+ def parser
31
+ return unless defined?(Bundler) && Bundler.default_lockfile
32
+ return @parser if defined?(@parser)
33
+
34
+ lockfile = Bundler.read_file(Bundler.default_lockfile)
35
+ @parser = lockfile ? Bundler::LockfileParser.new(lockfile) : nil
36
+ rescue Bundler::BundlerError
37
+ nil
38
+ end
39
+ end
40
+ end
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  # This module holds the RuboCop version information.
5
5
  module Version
6
- STRING = '1.5.2'
6
+ STRING = '1.6.0'
7
7
 
8
8
  MSG = '%<version>s (using Parser %<parser_version>s, '\
9
9
  'rubocop-ast %<rubocop_ast_version>s, ' \
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.2
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2020-12-04 00:00:00.000000000 Z
13
+ date: 2020-12-09 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: parallel
@@ -184,6 +184,7 @@ files:
184
184
  - assets/logo.png
185
185
  - assets/output.html.erb
186
186
  - config/default.yml
187
+ - config/obsoletion.yml
187
188
  - exe/rubocop
188
189
  - lib/rubocop.rb
189
190
  - lib/rubocop/ast_aliases.rb
@@ -203,6 +204,15 @@ files:
203
204
  - lib/rubocop/config_loader.rb
204
205
  - lib/rubocop/config_loader_resolver.rb
205
206
  - lib/rubocop/config_obsoletion.rb
207
+ - lib/rubocop/config_obsoletion/changed_enforced_styles.rb
208
+ - lib/rubocop/config_obsoletion/changed_parameter.rb
209
+ - lib/rubocop/config_obsoletion/cop_rule.rb
210
+ - lib/rubocop/config_obsoletion/extracted_cop.rb
211
+ - lib/rubocop/config_obsoletion/parameter_rule.rb
212
+ - lib/rubocop/config_obsoletion/removed_cop.rb
213
+ - lib/rubocop/config_obsoletion/renamed_cop.rb
214
+ - lib/rubocop/config_obsoletion/rule.rb
215
+ - lib/rubocop/config_obsoletion/split_cop.rb
206
216
  - lib/rubocop/config_regeneration.rb
207
217
  - lib/rubocop/config_store.rb
208
218
  - lib/rubocop/config_validator.rb
@@ -808,6 +818,7 @@ files:
808
818
  - lib/rubocop/formatter/tap_formatter.rb
809
819
  - lib/rubocop/formatter/text_util.rb
810
820
  - lib/rubocop/formatter/worst_offenders_formatter.rb
821
+ - lib/rubocop/lockfile.rb
811
822
  - lib/rubocop/magic_comment.rb
812
823
  - lib/rubocop/name_similarity.rb
813
824
  - lib/rubocop/options.rb
@@ -835,7 +846,7 @@ metadata:
835
846
  homepage_uri: https://rubocop.org/
836
847
  changelog_uri: https://github.com/rubocop-hq/rubocop/blob/master/CHANGELOG.md
837
848
  source_code_uri: https://github.com/rubocop-hq/rubocop/
838
- documentation_uri: https://docs.rubocop.org/rubocop/1.5/
849
+ documentation_uri: https://docs.rubocop.org/rubocop/1.6/
839
850
  bug_tracker_uri: https://github.com/rubocop-hq/rubocop/issues
840
851
  post_install_message:
841
852
  rdoc_options: []