rubocop 0.19.1 → 0.20.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rubocop might be problematic. Click here for more details.

Files changed (192) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +4 -0
  3. data/CHANGELOG.md +60 -1
  4. data/CONTRIBUTING.md +2 -1
  5. data/README.md +9 -7
  6. data/config/default.yml +3 -3
  7. data/config/disabled.yml +4 -0
  8. data/config/enabled.yml +45 -21
  9. data/lib/rubocop.rb +30 -9
  10. data/lib/rubocop/cli.rb +1 -1
  11. data/lib/rubocop/comment_config.rb +4 -2
  12. data/lib/rubocop/config.rb +16 -22
  13. data/lib/rubocop/config_loader.rb +29 -26
  14. data/lib/rubocop/cop/commissioner.rb +1 -1
  15. data/lib/rubocop/cop/cop.rb +6 -6
  16. data/lib/rubocop/cop/lint/assignment_in_condition.rb +1 -1
  17. data/lib/rubocop/cop/lint/condition_position.rb +1 -1
  18. data/lib/rubocop/cop/lint/debugger.rb +1 -1
  19. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +9 -7
  20. data/lib/rubocop/cop/lint/else_layout.rb +1 -1
  21. data/lib/rubocop/cop/lint/empty_ensure.rb +1 -1
  22. data/lib/rubocop/cop/lint/empty_interpolation.rb +22 -0
  23. data/lib/rubocop/cop/lint/end_in_method.rb +1 -1
  24. data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
  25. data/lib/rubocop/cop/lint/eval.rb +1 -1
  26. data/lib/rubocop/cop/lint/literal_in_condition.rb +2 -2
  27. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +9 -0
  28. data/lib/rubocop/cop/lint/loop.rb +2 -2
  29. data/lib/rubocop/cop/lint/require_parentheses.rb +0 -4
  30. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
  31. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
  32. data/lib/rubocop/cop/lint/space_before_first_arg.rb +36 -0
  33. data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +14 -2
  34. data/lib/rubocop/cop/lint/useless_access_modifier.rb +57 -0
  35. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
  36. data/lib/rubocop/cop/lint/void.rb +3 -3
  37. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
  38. data/lib/rubocop/cop/offense.rb +3 -21
  39. data/lib/rubocop/cop/rails/action_filter.rb +1 -1
  40. data/lib/rubocop/cop/rails/default_scope.rb +1 -1
  41. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +1 -1
  42. data/lib/rubocop/cop/rails/read_write_attribute.rb +43 -0
  43. data/lib/rubocop/cop/rails/scope_args.rb +1 -1
  44. data/lib/rubocop/cop/rails/validation.rb +1 -1
  45. data/lib/rubocop/cop/severity.rb +76 -0
  46. data/lib/rubocop/cop/style/access_modifier_indentation.rb +1 -1
  47. data/lib/rubocop/cop/style/accessor_method_name.rb +2 -2
  48. data/lib/rubocop/cop/style/alias.rb +1 -1
  49. data/lib/rubocop/cop/style/align_hash.rb +1 -1
  50. data/lib/rubocop/cop/style/and_or.rb +1 -1
  51. data/lib/rubocop/cop/style/{favor_join.rb → array_join.rb} +2 -2
  52. data/lib/rubocop/cop/style/begin_block.rb +1 -1
  53. data/lib/rubocop/cop/style/block_nesting.rb +12 -8
  54. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +1 -1
  55. data/lib/rubocop/cop/style/case_equality.rb +1 -1
  56. data/lib/rubocop/cop/style/case_indentation.rb +5 -5
  57. data/lib/rubocop/cop/style/class_methods.rb +19 -3
  58. data/lib/rubocop/cop/style/class_vars.rb +1 -1
  59. data/lib/rubocop/cop/style/collection_methods.rb +17 -7
  60. data/lib/rubocop/cop/style/colon_method_call.rb +1 -1
  61. data/lib/rubocop/cop/style/{def_parentheses.rb → def_with_parentheses.rb} +0 -0
  62. data/lib/rubocop/cop/style/{hash_methods.rb → deprecated_hash_methods.rb} +5 -4
  63. data/lib/rubocop/cop/style/double_negation.rb +1 -1
  64. data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +1 -1
  65. data/lib/rubocop/cop/style/end_block.rb +1 -1
  66. data/lib/rubocop/cop/style/even_odd.rb +2 -2
  67. data/lib/rubocop/cop/style/file_name.rb +1 -1
  68. data/lib/rubocop/cop/style/for.rb +2 -2
  69. data/lib/rubocop/cop/style/format_string.rb +1 -1
  70. data/lib/rubocop/cop/style/guard_clause.rb +69 -0
  71. data/lib/rubocop/cop/style/hash_syntax.rb +6 -10
  72. data/lib/rubocop/cop/style/if_unless_modifier.rb +2 -2
  73. data/lib/rubocop/cop/style/lambda.rb +2 -2
  74. data/lib/rubocop/cop/style/line_end_concatenation.rb +16 -9
  75. data/lib/rubocop/cop/style/multiline_if_then.rb +1 -1
  76. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +2 -2
  77. data/lib/rubocop/cop/style/negated_if.rb +37 -0
  78. data/lib/rubocop/cop/style/negated_while.rb +33 -0
  79. data/lib/rubocop/cop/style/nested_ternary_operator.rb +1 -1
  80. data/lib/rubocop/cop/style/nil_comparison.rb +14 -10
  81. data/lib/rubocop/cop/style/non_nil_check.rb +70 -0
  82. data/lib/rubocop/cop/style/not.rb +25 -1
  83. data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
  84. data/lib/rubocop/cop/style/op_method.rb +4 -4
  85. data/lib/rubocop/cop/style/parentheses_around_condition.rb +1 -1
  86. data/lib/rubocop/cop/style/predicate_name.rb +1 -1
  87. data/lib/rubocop/cop/style/proc.rb +1 -1
  88. data/lib/rubocop/cop/style/raise_args.rb +3 -2
  89. data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
  90. data/lib/rubocop/cop/style/self_assignment.rb +2 -2
  91. data/lib/rubocop/cop/style/signal_exception.rb +6 -3
  92. data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
  93. data/lib/rubocop/cop/style/single_line_methods.rb +6 -5
  94. data/lib/rubocop/cop/style/single_space_before_first_arg.rb +41 -0
  95. data/lib/rubocop/cop/style/space_around_operators.rb +1 -2
  96. data/lib/rubocop/cop/style/special_global_vars.rb +8 -8
  97. data/lib/rubocop/cop/style/trailing_comma.rb +1 -1
  98. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  99. data/lib/rubocop/cop/style/unless_else.rb +1 -1
  100. data/lib/rubocop/cop/style/variable_interpolation.rb +10 -5
  101. data/lib/rubocop/cop/style/when_then.rb +1 -1
  102. data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
  103. data/lib/rubocop/cop/style/word_array.rb +1 -1
  104. data/lib/rubocop/cop/team.rb +12 -13
  105. data/lib/rubocop/cop/util.rb +4 -0
  106. data/lib/rubocop/cop/variable_inspector/locatable.rb +1 -1
  107. data/lib/rubocop/cop/variable_inspector/variable_table.rb +1 -1
  108. data/lib/rubocop/file_inspector.rb +46 -10
  109. data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
  110. data/lib/rubocop/formatter/disabled_lines_formatter.rb +56 -0
  111. data/lib/rubocop/formatter/emacs_style_formatter.rb +1 -1
  112. data/lib/rubocop/formatter/formatter_set.rb +2 -1
  113. data/lib/rubocop/formatter/fuubar_style_formatter.rb +2 -4
  114. data/lib/rubocop/formatter/json_formatter.rb +3 -7
  115. data/lib/rubocop/formatter/progress_formatter.rb +1 -3
  116. data/lib/rubocop/formatter/simple_text_formatter.rb +7 -3
  117. data/lib/rubocop/options.rb +29 -10
  118. data/lib/rubocop/path_util.rb +2 -1
  119. data/lib/rubocop/processed_source.rb +8 -0
  120. data/lib/rubocop/target_finder.rb +33 -12
  121. data/lib/rubocop/version.rb +1 -1
  122. data/relnotes/v0.20.0.md +69 -0
  123. data/rubocop-todo.yml +2 -2
  124. data/spec/rubocop/cli_spec.rb +269 -94
  125. data/spec/rubocop/config_loader_spec.rb +14 -14
  126. data/spec/rubocop/config_spec.rb +8 -8
  127. data/spec/rubocop/cop/lint/deprecated_class_methods_spec.rb +11 -6
  128. data/spec/rubocop/cop/lint/empty_interpolation_spec.rb +18 -0
  129. data/spec/rubocop/cop/lint/eval_spec.rb +2 -4
  130. data/spec/rubocop/cop/lint/literal_in_interpolation_spec.rb +10 -0
  131. data/spec/rubocop/cop/lint/rescue_exception_spec.rb +0 -8
  132. data/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb +4 -4
  133. data/spec/rubocop/cop/lint/space_before_first_arg_spec.rb +48 -0
  134. data/spec/rubocop/cop/lint/string_conversion_in_interpolation_spec.rb +10 -0
  135. data/spec/rubocop/cop/lint/useless_access_modifier_spec.rb +154 -0
  136. data/spec/rubocop/cop/offense_spec.rb +1 -1
  137. data/spec/rubocop/cop/rails/read_write_attribute_spec.rb +19 -0
  138. data/spec/rubocop/cop/severity_spec.rb +113 -0
  139. data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +10 -10
  140. data/spec/rubocop/cop/style/alias_spec.rb +2 -2
  141. data/spec/rubocop/cop/style/and_or_spec.rb +2 -2
  142. data/spec/rubocop/cop/style/{favor_join_spec.rb → array_join_spec.rb} +1 -3
  143. data/spec/rubocop/cop/style/block_nesting_spec.rb +4 -4
  144. data/spec/rubocop/cop/style/case_equality_spec.rb +1 -0
  145. data/spec/rubocop/cop/style/case_indentation_spec.rb +12 -9
  146. data/spec/rubocop/cop/style/class_methods_spec.rb +23 -0
  147. data/spec/rubocop/cop/style/collection_methods_spec.rb +2 -2
  148. data/spec/rubocop/cop/style/{hash_methods_spec.rb → deprecated_hash_methods_spec.rb} +3 -3
  149. data/spec/rubocop/cop/style/empty_lines_around_access_modifier_spec.rb +2 -2
  150. data/spec/rubocop/cop/style/even_odd_spec.rb +8 -8
  151. data/spec/rubocop/cop/style/file_name_spec.rb +55 -42
  152. data/spec/rubocop/cop/style/for_spec.rb +4 -4
  153. data/spec/rubocop/cop/style/format_string_spec.rb +10 -10
  154. data/spec/rubocop/cop/style/guard_clause_spec.rb +77 -0
  155. data/spec/rubocop/cop/style/hash_syntax_spec.rb +4 -2
  156. data/spec/rubocop/cop/style/if_unless_modifier_spec.rb +4 -4
  157. data/spec/rubocop/cop/style/lambda_spec.rb +2 -2
  158. data/spec/rubocop/cop/style/line_end_concatenation_spec.rb +21 -0
  159. data/spec/rubocop/cop/style/multiline_if_then_spec.rb +1 -1
  160. data/spec/rubocop/cop/style/{favor_unless_over_negated_if_spec.rb → negated_if_spec.rb} +8 -3
  161. data/spec/rubocop/cop/style/{favor_until_over_negated_while_spec.rb → negated_while_spec.rb} +8 -3
  162. data/spec/rubocop/cop/style/nil_comparison_spec.rb +7 -13
  163. data/spec/rubocop/cop/style/non_nil_check_spec.rb +35 -0
  164. data/spec/rubocop/cop/style/not_spec.rb +11 -0
  165. data/spec/rubocop/cop/style/numeric_literals_spec.rb +0 -2
  166. data/spec/rubocop/cop/style/op_method_spec.rb +10 -2
  167. data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +2 -2
  168. data/spec/rubocop/cop/style/predicate_name_spec.rb +2 -1
  169. data/spec/rubocop/cop/style/raise_args_spec.rb +5 -0
  170. data/spec/rubocop/cop/style/rescue_modifier_spec.rb +2 -2
  171. data/spec/rubocop/cop/style/self_assignment_spec.rb +4 -4
  172. data/spec/rubocop/cop/style/signal_exception_spec.rb +24 -0
  173. data/spec/rubocop/cop/style/single_line_block_params_spec.rb +2 -0
  174. data/spec/rubocop/cop/style/single_space_before_first_arg_spec.rb +63 -0
  175. data/spec/rubocop/cop/style/special_global_vars_spec.rb +6 -5
  176. data/spec/rubocop/cop/style/trivial_accessors_spec.rb +6 -3
  177. data/spec/rubocop/cop/style/unless_else_spec.rb +2 -4
  178. data/spec/rubocop/cop/style/variable_interpolation_spec.rb +15 -6
  179. data/spec/rubocop/cop/style/when_then_spec.rb +3 -4
  180. data/spec/rubocop/cop/team_spec.rb +4 -18
  181. data/spec/rubocop/file_inspector_spec.rb +4 -0
  182. data/spec/rubocop/formatter/disabled_config_formatter_spec.rb +1 -1
  183. data/spec/rubocop/formatter/disabled_lines_formatter_spec.rb +69 -0
  184. data/spec/rubocop/options_spec.rb +5 -0
  185. data/spec/rubocop/target_finder_spec.rb +42 -11
  186. data/spec/support/shared_context.rb +1 -1
  187. data/spec/support/statement_modifier_helper.rb +1 -1
  188. metadata +75 -50
  189. data/lib/rubocop/cop/rails/read_attribute.rb +0 -28
  190. data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +0 -24
  191. data/lib/rubocop/cop/style/favor_until_over_negated_while.rb +0 -20
  192. data/spec/rubocop/cop/rails/read_attribute_spec.rb +0 -13
@@ -103,7 +103,7 @@ module Rubocop
103
103
  end
104
104
 
105
105
  def target_finder
106
- @target_finder ||= TargetFinder.new(@config_store, @options[:debug])
106
+ @target_finder ||= TargetFinder.new(@config_store, @options)
107
107
  end
108
108
  end
109
109
  end
@@ -20,12 +20,12 @@ module Rubocop
20
20
  disabled_line_ranges.none? { |range| range.include?(line_number) }
21
21
  end
22
22
 
23
- private
24
-
25
23
  def cop_disabled_line_ranges
26
24
  @cop_disabled_line_ranges ||= analyze
27
25
  end
28
26
 
27
+ private
28
+
29
29
  def analyze
30
30
  disabled_line_ranges = Hash.new { |hash, key| hash[key] = [] }
31
31
  disablement_start_line_numbers = {}
@@ -55,6 +55,8 @@ module Rubocop
55
55
  def each_mentioned_cop
56
56
  all_cop_names = nil # For performance improvement
57
57
 
58
+ return if processed_source.comments.nil?
59
+
58
60
  processed_source.comments.each do |comment|
59
61
  match = comment.text.match(COMMENT_DIRECTIVE_REGEXP)
60
62
  next unless match
@@ -10,6 +10,8 @@ module Rubocop
10
10
  # during a run of the rubocop program, if files in several
11
11
  # directories are inspected.
12
12
  class Config < DelegateClass(Hash)
13
+ include PathUtil
14
+
13
15
  class ValidationError < StandardError; end
14
16
 
15
17
  COMMON_PARAMS = %w(Exclude Include Severity)
@@ -66,7 +68,7 @@ module Rubocop
66
68
  end
67
69
 
68
70
  def file_to_include?(file)
69
- relative_file_path = relative_path_to_loaded_dir(file)
71
+ relative_file_path = path_relative_to_config(file)
70
72
  patterns_to_include.any? do |pattern|
71
73
  match_path?(pattern, relative_file_path)
72
74
  end
@@ -78,34 +80,26 @@ module Rubocop
78
80
  end
79
81
 
80
82
  def patterns_to_include
81
- @hash['AllCops']['Includes']
83
+ @hash['AllCops']['Include']
82
84
  end
83
85
 
84
86
  def patterns_to_exclude
85
- @hash['AllCops']['Excludes']
87
+ @hash['AllCops']['Exclude']
86
88
  end
87
89
 
88
- private
89
-
90
- def relative_path_to_loaded_dir(file)
91
- return file unless loaded_path
92
- ConfigLoader.relative_path(file, loaded_dir_pathname)
93
- end
94
-
95
- def loaded_dir_pathname
96
- return nil unless loaded_path
97
- @loaded_dir ||= begin
98
- loaded_dir = File.expand_path(File.dirname(loaded_path))
99
- Pathname.new(loaded_dir)
100
- end
90
+ def path_relative_to_config(path)
91
+ relative_path(path, base_dir_for_path_parameters)
101
92
  end
102
93
 
103
- def match_path?(pattern, path)
104
- case pattern
105
- when String
106
- File.basename(path) == pattern || File.fnmatch(pattern, path)
107
- when Regexp
108
- path =~ pattern
94
+ # Paths specified in .rubocop.yml files are relative to the directory where
95
+ # that file is. Paths in other config files are relative to the current
96
+ # directory. This is so that paths in config/default.yml, for example, are
97
+ # not relative to RuboCop's config directory since that wouldn't work.
98
+ def base_dir_for_path_parameters
99
+ if File.basename(loaded_path) == ConfigLoader::DOTFILE
100
+ File.dirname(loaded_path)
101
+ else
102
+ Dir.pwd
109
103
  end
110
104
  end
111
105
  end
@@ -31,15 +31,28 @@ module Rubocop
31
31
 
32
32
  hash.delete('inherit_from')
33
33
  config = Config.new(hash, path)
34
+ deprecation_check(config)
34
35
  config.warn_unless_valid
36
+ make_excludes_absolute(config)
35
37
  config
36
38
  end
37
39
 
40
+ def deprecation_check(config)
41
+ return unless config['AllCops']
42
+ if config['AllCops']['Excludes']
43
+ warn('AllCops/Excludes was renamed to AllCops/Exclude')
44
+ exit(-1)
45
+ elsif config['AllCops']['Includes']
46
+ warn('AllCops/Includes was renamed to AllCops/Include')
47
+ exit(-1)
48
+ end
49
+ end
50
+
38
51
  def make_excludes_absolute(config)
39
- if config['AllCops'] && config['AllCops']['Excludes']
40
- config['AllCops']['Excludes'].map! do |exclude_elem|
52
+ if config['AllCops'] && config['AllCops']['Exclude']
53
+ config['AllCops']['Exclude'].map! do |exclude_elem|
41
54
  if exclude_elem.is_a?(String) && !exclude_elem.start_with?('/')
42
- File.join(File.dirname(config.loaded_path), exclude_elem)
55
+ File.join(config.base_dir_for_path_parameters, exclude_elem)
43
56
  else
44
57
  exclude_elem
45
58
  end
@@ -47,36 +60,30 @@ module Rubocop
47
60
  end
48
61
  end
49
62
 
50
- def relative_path(path, base)
51
- path_name = Pathname.new(File.expand_path(path))
52
- path_name.relative_path_from(Pathname.new(base)).to_s
53
- end
54
-
55
- # Return an extended merge of two hashes. That is, a normal hash merge,
63
+ # Return a recursive merge of two hashes. That is, a normal hash merge,
56
64
  # with the addition that any value that is a hash, and occurs in both
57
- # arguments (i.e., cop names), will also be merged.
65
+ # arguments, will also be merged. And so on.
58
66
  def merge(base_hash, derived_hash)
59
67
  result = base_hash.merge(derived_hash)
60
68
  keys_appearing_in_both = base_hash.keys & derived_hash.keys
61
69
  keys_appearing_in_both.each do |key|
62
70
  if base_hash[key].is_a?(Hash)
63
- result[key] = base_hash[key].merge(derived_hash[key])
71
+ result[key] = merge(base_hash[key], derived_hash[key])
64
72
  end
65
73
  end
66
74
  result
67
75
  end
68
76
 
69
77
  def base_configs(path, inherit_from)
70
- Array(inherit_from).map do |f|
78
+ configs = Array(inherit_from).map do |f|
71
79
  f = File.join(File.dirname(path), f) unless f.start_with?('/')
72
- if auto_gen_config? && f.include?(AUTO_GENERATED_FILE)
73
- warn "Remove #{AUTO_GENERATED_FILE} from the current " \
74
- 'configuration before generating it again.'
75
- exit(1)
80
+ unless auto_gen_config? && f.include?(AUTO_GENERATED_FILE)
81
+ print 'Inheriting ' if debug?
82
+ load_file(f)
76
83
  end
77
- print 'Inheriting ' if debug?
78
- load_file(f)
79
84
  end
85
+
86
+ configs.compact
80
87
  end
81
88
 
82
89
  # Returns the path of .rubocop.yml searching upwards in the
@@ -94,18 +101,17 @@ module Rubocop
94
101
 
95
102
  found_files = config_files_in_path(config_file)
96
103
  if found_files.any? && found_files.last != config_file
97
- print 'AllCops/Excludes ' if debug?
104
+ print 'AllCops/Exclude ' if debug?
98
105
  add_excludes_from_higher_level(config, load_file(found_files.last))
99
106
  end
100
- make_excludes_absolute(config)
101
107
  merge_with_default(config, config_file)
102
108
  end
103
109
 
104
110
  def add_excludes_from_higher_level(config, highest_config)
105
- if highest_config['AllCops'] && highest_config['AllCops']['Excludes']
111
+ if highest_config['AllCops'] && highest_config['AllCops']['Exclude']
106
112
  config['AllCops'] ||= {}
107
- excludes = config['AllCops']['Excludes'] ||= []
108
- highest_config['AllCops']['Excludes'].each do |path|
113
+ excludes = config['AllCops']['Exclude'] ||= []
114
+ highest_config['AllCops']['Exclude'].each do |path|
109
115
  unless path.is_a?(Regexp) || path.start_with?('/')
110
116
  path = File.join(File.dirname(highest_config.loaded_path), path)
111
117
  end
@@ -129,9 +135,6 @@ module Rubocop
129
135
 
130
136
  def resolve_inheritance(path, hash)
131
137
  base_configs(path, hash['inherit_from']).reverse_each do |base_config|
132
- if File.basename(base_config.loaded_path) == DOTFILE
133
- make_excludes_absolute(base_config)
134
- end
135
138
  base_config.each do |k, v|
136
139
  hash[k] = hash.key?(k) ? merge(v, hash[k]) : v if v.is_a?(Hash)
137
140
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Rubocop
4
4
  module Cop
5
- # Commissioner class is responsible for processing the AST and delagating
5
+ # Commissioner class is responsible for processing the AST and delegating
6
6
  # work to the specified cops.
7
7
  class Commissioner < Parser::AST::Processor
8
8
  attr_reader :errors
@@ -145,11 +145,11 @@ module Rubocop
145
145
  alias_method :name, :cop_name
146
146
 
147
147
  def include_file?(file)
148
- buffer_name_matches_any?('Include', true)
148
+ file_name_matches_any?(file, 'Include', true)
149
149
  end
150
150
 
151
151
  def exclude_file?(file)
152
- buffer_name_matches_any?('Exclude', false)
152
+ file_name_matches_any?(file, 'Exclude', false)
153
153
  end
154
154
 
155
155
  def relevant_file?(file)
@@ -158,10 +158,10 @@ module Rubocop
158
158
 
159
159
  private
160
160
 
161
- def buffer_name_matches_any?(parameter, default_result)
161
+ def file_name_matches_any?(file, parameter, default_result)
162
162
  patterns = cop_config && cop_config[parameter]
163
163
  return default_result unless patterns
164
- path = relative_path(processed_source.buffer.name)
164
+ path = config.path_relative_to_config(file)
165
165
  patterns.any? { |pattern| match_path?(pattern, path) }
166
166
  end
167
167
 
@@ -178,11 +178,11 @@ module Rubocop
178
178
  def custom_severity
179
179
  severity = cop_config && cop_config['Severity']
180
180
  if severity
181
- if Offense::SEVERITIES.include?(severity.to_sym)
181
+ if Severity::NAMES.include?(severity.to_sym)
182
182
  severity.to_sym
183
183
  else
184
184
  warn "Warning: Invalid severity '#{severity}'. " +
185
- "Valid severities are #{Offense::SEVERITIES.join(', ')}."
185
+ "Valid severities are #{Severity::NAMES.join(', ')}."
186
186
  .color(:red)
187
187
  end
188
188
  end
@@ -8,7 +8,7 @@ module Rubocop
8
8
  class AssignmentInCondition < Cop
9
9
  include SafeAssignment
10
10
 
11
- MSG = 'Assignment in condition - you probably meant to use ==.'
11
+ MSG = 'Assignment in condition - you probably meant to use `==`.'
12
12
 
13
13
  def on_if(node)
14
14
  check(node)
@@ -40,7 +40,7 @@ module Rubocop
40
40
  end
41
41
 
42
42
  def message(keyword)
43
- "Place the condition on the same line as #{keyword}."
43
+ "Place the condition on the same line as `#{keyword}`."
44
44
  end
45
45
 
46
46
  def on_different_line?(keyword_line, cond_line)
@@ -5,7 +5,7 @@ module Rubocop
5
5
  module Lint
6
6
  # This cop checks for calls to debugger or pry.
7
7
  class Debugger < Cop
8
- MSG = 'Remove calls to debugger.'
8
+ MSG = 'Remove calls to `debugger`.'
9
9
 
10
10
  # debugger call node
11
11
  #
@@ -7,7 +7,7 @@ module Rubocop
7
7
  class DeprecatedClassMethods < Cop
8
8
  include AST::Sexp
9
9
 
10
- MSG = '%s is deprecated in favor of %s.'
10
+ MSG = '`%s` is deprecated in favor of `%s`.'
11
11
 
12
12
  DEPRECATED_METHODS = [
13
13
  [:File, :exists?, :exist?],
@@ -18,12 +18,13 @@ module Rubocop
18
18
  receiver, method_name, *_args = *node
19
19
 
20
20
  DEPRECATED_METHODS.each do |data|
21
- next unless receiver == class_node(data)
21
+ next unless class_nodes(data).include?(receiver)
22
22
  next unless method_name == data[1]
23
23
 
24
24
  add_offense(node, :selector,
25
- MSG.format(deprecated_method(data),
26
- replacement_method(data)))
25
+ format(MSG,
26
+ deprecated_method(data),
27
+ replacement_method(data)))
27
28
  end
28
29
  end
29
30
 
@@ -32,7 +33,7 @@ module Rubocop
32
33
  receiver, method_name, *_args = *node
33
34
 
34
35
  DEPRECATED_METHODS.each do |data|
35
- next unless receiver == class_node(data)
36
+ next unless class_nodes(data).include?(receiver)
36
37
  next unless method_name == data[1]
37
38
 
38
39
  corrector.replace(node.loc.selector,
@@ -43,8 +44,9 @@ module Rubocop
43
44
 
44
45
  private
45
46
 
46
- def class_node(data)
47
- s(:const, nil, data[0])
47
+ def class_nodes(data)
48
+ [s(:const, nil, data[0]),
49
+ s(:const, s(:cbase), data[0])]
48
50
  end
49
51
 
50
52
  def deprecated_method(data)
@@ -49,7 +49,7 @@ module Rubocop
49
49
  end
50
50
 
51
51
  def message
52
- 'Odd else layout detected. Perhaps you meant to use elsif instead?'
52
+ 'Odd `else` layout detected. Did you mean to use `elsif`?'
53
53
  end
54
54
  end
55
55
  end
@@ -5,7 +5,7 @@ module Rubocop
5
5
  module Lint
6
6
  # This cop checks for empty `ensure` blocks
7
7
  class EmptyEnsure < Cop
8
- MSG = 'Empty ensure block detected.'
8
+ MSG = 'Empty `ensure` block detected.'
9
9
 
10
10
  def on_ensure(node)
11
11
  _body, ensure_body = *node
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ module Lint
6
+ # This cop checks for empty interpolation.
7
+ #
8
+ # @example
9
+ #
10
+ # "result is #{}"
11
+ class EmptyInterpolation < Cop
12
+ MSG = 'Empty interpolation detected.'
13
+
14
+ def on_dstr(node)
15
+ node.children.select { |n| n.type == :begin }.each do |begin_node|
16
+ add_offense(begin_node, :expression) if begin_node.children.empty?
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -7,7 +7,7 @@ module Rubocop
7
7
  class EndInMethod < Cop
8
8
  include CheckMethods
9
9
 
10
- MSG = 'END found in method definition. Use `at_exit` instead.'
10
+ MSG = '`END` found in method definition. Use `at_exit` instead.'
11
11
 
12
12
  private
13
13
 
@@ -5,7 +5,7 @@ module Rubocop
5
5
  module Lint
6
6
  # This cop checks for *return* from an *ensure* block.
7
7
  class EnsureReturn < Cop
8
- MSG = 'Never return from an ensure block.'
8
+ MSG = 'Never return from an `ensure` block.'
9
9
 
10
10
  def on_ensure(node)
11
11
  _body, ensure_body = *node
@@ -5,7 +5,7 @@ module Rubocop
5
5
  module Lint
6
6
  # This cop checks for the use of *Kernel#eval*.
7
7
  class Eval < Cop
8
- MSG = 'The use of eval is a serious security risk.'
8
+ MSG = 'The use of `eval` is a serious security risk.'
9
9
 
10
10
  def on_send(node)
11
11
  receiver, method_name, = *node
@@ -18,7 +18,7 @@ module Rubocop
18
18
  # end
19
19
  #
20
20
  class LiteralInCondition < Cop
21
- MSG = 'Literal %s appeared in a condition.'
21
+ MSG = 'Literal `%s` appeared in a condition.'
22
22
 
23
23
  LITERALS = [:str, :dstr, :int, :float, :array,
24
24
  :hash, :regexp, :nil, :true, :false]
@@ -58,7 +58,7 @@ module Rubocop
58
58
  end
59
59
 
60
60
  def message(node)
61
- MSG.format(node.loc.expression.source)
61
+ format(MSG, node.loc.expression.source)
62
62
  end
63
63
 
64
64
  private
@@ -17,11 +17,20 @@ module Rubocop
17
17
  def on_dstr(node)
18
18
  node.children.select { |n| n.type == :begin }.each do |begin_node|
19
19
  final_node = begin_node.children.last
20
+ next unless final_node
21
+ # handle strings like __FILE__
22
+ return if special_string?(final_node)
20
23
  next unless LITERALS.include?(final_node.type)
21
24
 
22
25
  add_offense(final_node, :expression)
23
26
  end
24
27
  end
28
+
29
+ private
30
+
31
+ def special_string?(node)
32
+ node.type == :str && !node.loc.respond_to?(:begin)
33
+ end
25
34
  end
26
35
  end
27
36
  end