rubocop 0.12.0 → 0.13.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 (279) hide show
  1. data/CHANGELOG.md +40 -0
  2. data/README.md +49 -8
  3. data/config/default.yml +40 -0
  4. data/config/enabled.yml +37 -9
  5. data/lib/rubocop.rb +24 -10
  6. data/lib/rubocop/cli.rb +41 -106
  7. data/lib/rubocop/config.rb +3 -2
  8. data/lib/rubocop/cop/commissioner.rb +15 -5
  9. data/lib/rubocop/cop/cop.rb +47 -32
  10. data/lib/rubocop/cop/lint/assignment_in_condition.rb +2 -2
  11. data/lib/rubocop/cop/lint/block_alignment.rb +30 -9
  12. data/lib/rubocop/cop/lint/empty_ensure.rb +1 -1
  13. data/lib/rubocop/cop/lint/end_alignment.rb +4 -4
  14. data/lib/rubocop/cop/lint/end_in_method.rb +1 -1
  15. data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
  16. data/lib/rubocop/cop/lint/eval.rb +1 -3
  17. data/lib/rubocop/cop/lint/handle_exceptions.rb +1 -1
  18. data/lib/rubocop/cop/lint/literal_in_condition.rb +6 -4
  19. data/lib/rubocop/cop/lint/loop.rb +1 -1
  20. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +1 -1
  21. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
  22. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +5 -8
  23. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -1
  24. data/lib/rubocop/cop/lint/useless_assignment.rb +57 -60
  25. data/lib/rubocop/cop/lint/useless_comparison.rb +1 -1
  26. data/lib/rubocop/cop/lint/useless_setter_call.rb +85 -0
  27. data/lib/rubocop/cop/lint/void.rb +6 -8
  28. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +1 -1
  29. data/lib/rubocop/cop/rails/read_attribute.rb +1 -1
  30. data/lib/rubocop/cop/rails/validation.rb +1 -1
  31. data/lib/rubocop/cop/style/access_control.rb +4 -6
  32. data/lib/rubocop/cop/style/alias.rb +1 -3
  33. data/lib/rubocop/cop/style/align_array.rb +47 -0
  34. data/lib/rubocop/cop/style/align_hash.rb +145 -0
  35. data/lib/rubocop/cop/style/align_parameters.rb +9 -3
  36. data/lib/rubocop/cop/style/and_or.rb +3 -4
  37. data/lib/rubocop/cop/style/ascii_comments.rb +1 -3
  38. data/lib/rubocop/cop/style/ascii_identifiers.rb +1 -1
  39. data/lib/rubocop/cop/style/attr.rb +1 -4
  40. data/lib/rubocop/cop/style/begin_block.rb +1 -1
  41. data/lib/rubocop/cop/style/block_comments.rb +1 -1
  42. data/lib/rubocop/cop/style/block_nesting.rb +2 -2
  43. data/lib/rubocop/cop/style/blocks.rb +14 -2
  44. data/lib/rubocop/cop/style/case_equality.rb +1 -3
  45. data/lib/rubocop/cop/style/case_indentation.rb +1 -1
  46. data/lib/rubocop/cop/style/character_literal.rb +1 -2
  47. data/lib/rubocop/cop/style/class_and_module_camel_case.rb +1 -1
  48. data/lib/rubocop/cop/style/class_methods.rb +1 -3
  49. data/lib/rubocop/cop/style/{avoid_class_vars.rb → class_vars.rb} +6 -2
  50. data/lib/rubocop/cop/style/collection_methods.rb +7 -9
  51. data/lib/rubocop/cop/style/colon_method_call.rb +1 -2
  52. data/lib/rubocop/cop/style/comment_annotation.rb +6 -6
  53. data/lib/rubocop/cop/style/constant_name.rb +1 -3
  54. data/lib/rubocop/cop/style/def_parentheses.rb +4 -12
  55. data/lib/rubocop/cop/style/documentation.rb +2 -2
  56. data/lib/rubocop/cop/style/dot_position.rb +2 -4
  57. data/lib/rubocop/cop/style/empty_line_between_defs.rb +21 -6
  58. data/lib/rubocop/cop/style/empty_lines.rb +1 -1
  59. data/lib/rubocop/cop/style/empty_literal.rb +3 -12
  60. data/lib/rubocop/cop/style/encoding.rb +6 -6
  61. data/lib/rubocop/cop/style/end_block.rb +1 -1
  62. data/lib/rubocop/cop/style/end_of_line.rb +5 -5
  63. data/lib/rubocop/cop/style/even_odd.rb +2 -2
  64. data/lib/rubocop/cop/style/favor_join.rb +1 -3
  65. data/lib/rubocop/cop/style/favor_modifier.rb +7 -3
  66. data/lib/rubocop/cop/style/favor_sprintf.rb +1 -1
  67. data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +1 -1
  68. data/lib/rubocop/cop/style/final_newline.rb +23 -0
  69. data/lib/rubocop/cop/style/{avoid_for.rb → for.rb} +2 -2
  70. data/lib/rubocop/cop/style/{avoid_global_vars.rb → global_vars.rb} +19 -6
  71. data/lib/rubocop/cop/style/hash_methods.rb +3 -5
  72. data/lib/rubocop/cop/style/hash_syntax.rb +4 -4
  73. data/lib/rubocop/cop/style/if_then_else.rb +1 -1
  74. data/lib/rubocop/cop/style/indentation_width.rb +4 -4
  75. data/lib/rubocop/cop/style/lambda.rb +2 -2
  76. data/lib/rubocop/cop/style/leading_comment_space.rb +1 -1
  77. data/lib/rubocop/cop/style/line_length.rb +7 -8
  78. data/lib/rubocop/cop/style/method_and_variable_snake_case.rb +1 -1
  79. data/lib/rubocop/cop/style/method_call_parentheses.rb +1 -4
  80. data/lib/rubocop/cop/style/method_length.rb +4 -4
  81. data/lib/rubocop/cop/style/module_function.rb +1 -3
  82. data/lib/rubocop/cop/style/multiline_block_chain.rb +44 -0
  83. data/lib/rubocop/cop/style/nil_comparison.rb +1 -3
  84. data/lib/rubocop/cop/style/not.rb +1 -1
  85. data/lib/rubocop/cop/style/numeric_literals.rb +26 -6
  86. data/lib/rubocop/cop/style/op_method.rb +2 -2
  87. data/lib/rubocop/cop/style/parameter_lists.rb +4 -4
  88. data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -2
  89. data/lib/rubocop/cop/style/perl_backrefs.rb +26 -0
  90. data/lib/rubocop/cop/style/proc.rb +1 -3
  91. data/lib/rubocop/cop/style/reduce_arguments.rb +7 -5
  92. data/lib/rubocop/cop/style/redundant_begin.rb +1 -1
  93. data/lib/rubocop/cop/style/redundant_return.rb +9 -2
  94. data/lib/rubocop/cop/style/redundant_self.rb +9 -2
  95. data/lib/rubocop/cop/style/regexp_literal.rb +7 -8
  96. data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
  97. data/lib/rubocop/cop/style/semicolon.rb +10 -10
  98. data/lib/rubocop/cop/style/signal_exception.rb +2 -4
  99. data/lib/rubocop/cop/style/single_line_methods.rb +2 -4
  100. data/lib/rubocop/cop/style/space_after_comma_etc.rb +1 -1
  101. data/lib/rubocop/cop/style/space_after_control_keyword.rb +1 -1
  102. data/lib/rubocop/cop/style/space_after_method_name.rb +1 -1
  103. data/lib/rubocop/cop/style/space_before_modifier_keyword.rb +34 -0
  104. data/lib/rubocop/cop/style/{avoid_perlisms.rb → special_global_vars.rb} +17 -8
  105. data/lib/rubocop/cop/style/string_literals.rb +1 -2
  106. data/lib/rubocop/cop/style/surrounding_space.rb +9 -8
  107. data/lib/rubocop/cop/style/symbol_array.rb +1 -1
  108. data/lib/rubocop/cop/style/symbol_name.rb +9 -2
  109. data/lib/rubocop/cop/style/tab.rb +5 -5
  110. data/lib/rubocop/cop/style/ternary_operator.rb +2 -6
  111. data/lib/rubocop/cop/style/trailing_blank_lines.rb +32 -0
  112. data/lib/rubocop/cop/style/trailing_whitespace.rb +5 -6
  113. data/lib/rubocop/cop/style/trivial_accessors.rb +5 -5
  114. data/lib/rubocop/cop/style/unless_else.rb +1 -1
  115. data/lib/rubocop/cop/style/variable_interpolation.rb +1 -3
  116. data/lib/rubocop/cop/style/when_then.rb +1 -4
  117. data/lib/rubocop/cop/style/while_until_do.rb +7 -5
  118. data/lib/rubocop/cop/style/word_array.rb +1 -1
  119. data/lib/rubocop/cop/team.rb +100 -0
  120. data/lib/rubocop/cop/variable_inspector.rb +323 -235
  121. data/lib/rubocop/cop/variable_inspector/assignment.rb +103 -0
  122. data/lib/rubocop/cop/variable_inspector/locatable.rb +162 -0
  123. data/lib/rubocop/cop/variable_inspector/reference.rb +31 -0
  124. data/lib/rubocop/cop/variable_inspector/scope.rb +70 -0
  125. data/lib/rubocop/cop/variable_inspector/variable.rb +87 -0
  126. data/lib/rubocop/cop/variable_inspector/variable_table.rb +129 -0
  127. data/lib/rubocop/formatter/json_formatter.rb +8 -8
  128. data/lib/rubocop/formatter/progress_formatter.rb +4 -4
  129. data/lib/rubocop/processed_source.rb +22 -1
  130. data/lib/rubocop/version.rb +1 -1
  131. data/rubocop.gemspec +1 -1
  132. data/spec/rubocop/cli_spec.rb +32 -30
  133. data/spec/rubocop/config_spec.rb +4 -6
  134. data/spec/rubocop/cop/commissioner_spec.rb +4 -5
  135. data/spec/rubocop/cop/cop_spec.rb +8 -26
  136. data/spec/rubocop/cop/lint/assignment_in_condition_spec.rb +5 -9
  137. data/spec/rubocop/cop/lint/block_alignment_spec.rb +105 -57
  138. data/spec/rubocop/cop/lint/empty_ensure_spec.rb +1 -1
  139. data/spec/rubocop/cop/lint/end_alignment_spec.rb +1 -1
  140. data/spec/rubocop/cop/lint/end_in_method_spec.rb +1 -1
  141. data/spec/rubocop/cop/lint/ensure_return_spec.rb +1 -1
  142. data/spec/rubocop/cop/lint/eval_spec.rb +3 -3
  143. data/spec/rubocop/cop/lint/handle_exceptions_spec.rb +2 -2
  144. data/spec/rubocop/cop/lint/literal_in_condition_spec.rb +1 -1
  145. data/spec/rubocop/cop/lint/loop_spec.rb +1 -1
  146. data/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb +1 -1
  147. data/spec/rubocop/cop/lint/rescue_exception_spec.rb +5 -5
  148. data/spec/rubocop/cop/lint/unreachable_code_spec.rb +1 -1
  149. data/spec/rubocop/cop/lint/useless_assignment_spec.rb +1545 -108
  150. data/spec/rubocop/cop/lint/useless_comparison_spec.rb +1 -1
  151. data/spec/rubocop/cop/lint/useless_setter_call_spec.rb +101 -0
  152. data/spec/rubocop/cop/lint/void_spec.rb +1 -1
  153. data/spec/rubocop/cop/offence_spec.rb +4 -4
  154. data/spec/rubocop/cop/rails/has_and_belongs_to_many_spec.rb +1 -1
  155. data/spec/rubocop/cop/rails/read_attribute_spec.rb +1 -1
  156. data/spec/rubocop/cop/rails/validation_spec.rb +1 -1
  157. data/spec/rubocop/cop/style/access_control_spec.rb +20 -20
  158. data/spec/rubocop/cop/style/alias_spec.rb +3 -3
  159. data/spec/rubocop/cop/style/align_array_spec.rb +62 -0
  160. data/spec/rubocop/cop/style/align_hash_spec.rb +267 -0
  161. data/spec/rubocop/cop/style/align_parameters_spec.rb +2 -2
  162. data/spec/rubocop/cop/style/and_or_spec.rb +1 -1
  163. data/spec/rubocop/cop/style/ascii_comments_spec.rb +2 -2
  164. data/spec/rubocop/cop/style/ascii_identifiers_spec.rb +2 -2
  165. data/spec/rubocop/cop/style/attr_spec.rb +1 -1
  166. data/spec/rubocop/cop/style/begin_block_spec.rb +1 -1
  167. data/spec/rubocop/cop/style/block_comments_spec.rb +1 -1
  168. data/spec/rubocop/cop/style/block_nesting_spec.rb +3 -3
  169. data/spec/rubocop/cop/style/blocks_spec.rb +25 -1
  170. data/spec/rubocop/cop/style/case_equality_spec.rb +1 -1
  171. data/spec/rubocop/cop/style/case_indentation_spec.rb +5 -5
  172. data/spec/rubocop/cop/style/character_literal_spec.rb +1 -1
  173. data/spec/rubocop/cop/style/class_and_module_camel_case_spec.rb +1 -1
  174. data/spec/rubocop/cop/style/class_methods_spec.rb +1 -1
  175. data/spec/rubocop/cop/style/class_vars_spec.rb +25 -0
  176. data/spec/rubocop/cop/style/collection_methods_spec.rb +5 -6
  177. data/spec/rubocop/cop/style/colon_method_call_spec.rb +0 -3
  178. data/spec/rubocop/cop/style/comment_annotation_spec.rb +20 -18
  179. data/spec/rubocop/cop/style/constant_name_spec.rb +1 -1
  180. data/spec/rubocop/cop/style/def_with_parentheses_spec.rb +1 -1
  181. data/spec/rubocop/cop/style/def_without_parentheses_spec.rb +1 -1
  182. data/spec/rubocop/cop/style/documentation_spec.rb +1 -1
  183. data/spec/rubocop/cop/style/dot_position_spec.rb +5 -5
  184. data/spec/rubocop/cop/style/empty_line_between_defs_spec.rb +35 -4
  185. data/spec/rubocop/cop/style/empty_lines_spec.rb +1 -1
  186. data/spec/rubocop/cop/style/empty_literal_spec.rb +7 -7
  187. data/spec/rubocop/cop/style/encoding_spec.rb +11 -5
  188. data/spec/rubocop/cop/style/end_block_spec.rb +1 -1
  189. data/spec/rubocop/cop/style/end_of_line_spec.rb +4 -3
  190. data/spec/rubocop/cop/style/even_odd_spec.rb +1 -1
  191. data/spec/rubocop/cop/style/favor_join_spec.rb +2 -2
  192. data/spec/rubocop/cop/style/favor_modifier_spec.rb +13 -10
  193. data/spec/rubocop/cop/style/favor_sprintf_spec.rb +4 -4
  194. data/spec/rubocop/cop/style/favor_unless_over_negated_if_spec.rb +1 -1
  195. data/spec/rubocop/cop/style/favor_until_over_negated_while_spec.rb +4 -4
  196. data/spec/rubocop/cop/style/final_newline_spec.rb +25 -0
  197. data/spec/rubocop/cop/style/{avoid_for_spec.rb → for_spec.rb} +8 -12
  198. data/spec/rubocop/cop/style/{avoid_global_vars_spec.rb → global_vars_spec.rb} +13 -3
  199. data/spec/rubocop/cop/style/hash_methods_spec.rb +1 -1
  200. data/spec/rubocop/cop/style/hash_syntax_spec.rb +20 -9
  201. data/spec/rubocop/cop/style/if_with_semicolon_spec.rb +3 -3
  202. data/spec/rubocop/cop/style/indentation_width_spec.rb +19 -19
  203. data/spec/rubocop/cop/style/lambda_spec.rb +6 -6
  204. data/spec/rubocop/cop/style/leading_comment_space_spec.rb +1 -1
  205. data/spec/rubocop/cop/style/line_length_spec.rb +3 -3
  206. data/spec/rubocop/cop/style/method_and_variable_snake_case_spec.rb +8 -9
  207. data/spec/rubocop/cop/style/method_call_parentheses_spec.rb +1 -1
  208. data/spec/rubocop/cop/style/method_length_spec.rb +18 -17
  209. data/spec/rubocop/cop/style/module_function_spec.rb +1 -1
  210. data/spec/rubocop/cop/style/multiline_block_chain_spec.rb +84 -0
  211. data/spec/rubocop/cop/style/multiline_if_then_spec.rb +2 -2
  212. data/spec/rubocop/cop/style/nil_comparison_spec.rb +1 -1
  213. data/spec/rubocop/cop/style/not_spec.rb +1 -1
  214. data/spec/rubocop/cop/style/numeric_literals_spec.rb +15 -25
  215. data/spec/rubocop/cop/style/one_line_conditional_spec.rb +2 -2
  216. data/spec/rubocop/cop/style/op_method_spec.rb +3 -3
  217. data/spec/rubocop/cop/style/parameter_lists_spec.rb +5 -5
  218. data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +4 -8
  219. data/spec/rubocop/cop/style/perl_backrefs_spec.rb +23 -0
  220. data/spec/rubocop/cop/style/proc_spec.rb +1 -1
  221. data/spec/rubocop/cop/style/reduce_arguments_spec.rb +18 -11
  222. data/spec/rubocop/cop/style/redundant_begin_spec.rb +1 -1
  223. data/spec/rubocop/cop/style/redundant_return_spec.rb +16 -1
  224. data/spec/rubocop/cop/style/redundant_self_spec.rb +6 -1
  225. data/spec/rubocop/cop/style/regexp_literal_spec.rb +19 -23
  226. data/spec/rubocop/cop/style/rescue_modifier_spec.rb +3 -3
  227. data/spec/rubocop/cop/style/semicolon_spec.rb +3 -3
  228. data/spec/rubocop/cop/style/signal_exception_spec.rb +1 -1
  229. data/spec/rubocop/cop/style/single_line_methods_spec.rb +22 -18
  230. data/spec/rubocop/cop/style/space_after_colon_spec.rb +4 -4
  231. data/spec/rubocop/cop/style/space_after_comma_spec.rb +4 -4
  232. data/spec/rubocop/cop/style/space_after_control_keyword_spec.rb +1 -1
  233. data/spec/rubocop/cop/style/space_after_method_name_spec.rb +1 -1
  234. data/spec/rubocop/cop/style/space_after_semicolon_spec.rb +3 -3
  235. data/spec/rubocop/cop/style/space_around_braces_spec.rb +13 -12
  236. data/spec/rubocop/cop/style/space_around_equals_in_default_parameter_spec.rb +3 -3
  237. data/spec/rubocop/cop/style/space_around_operators_spec.rb +25 -25
  238. data/spec/rubocop/cop/style/space_before_modifier_keyword_spec.rb +53 -0
  239. data/spec/rubocop/cop/style/space_inside_brackets_spec.rb +9 -9
  240. data/spec/rubocop/cop/style/space_inside_hash_literal_braces_spec.rb +47 -61
  241. data/spec/rubocop/cop/style/space_inside_parens_spec.rb +4 -4
  242. data/spec/rubocop/cop/style/special_global_vars_spec.rb +52 -0
  243. data/spec/rubocop/cop/style/string_literals_spec.rb +5 -5
  244. data/spec/rubocop/cop/style/symbol_array_spec.rb +1 -1
  245. data/spec/rubocop/cop/style/symbol_name_spec.rb +27 -18
  246. data/spec/rubocop/cop/style/tab_spec.rb +1 -1
  247. data/spec/rubocop/cop/style/ternary_operator_spec.rb +2 -2
  248. data/spec/rubocop/cop/style/trailing_blank_lines_spec.rb +24 -0
  249. data/spec/rubocop/cop/style/trailing_whitespace_spec.rb +7 -7
  250. data/spec/rubocop/cop/style/trivial_accessors_spec.rb +6 -14
  251. data/spec/rubocop/cop/style/unless_else_spec.rb +3 -3
  252. data/spec/rubocop/cop/style/variable_interpolation_spec.rb +5 -5
  253. data/spec/rubocop/cop/style/when_then_spec.rb +15 -15
  254. data/spec/rubocop/cop/style/while_until_do_spec.rb +3 -3
  255. data/spec/rubocop/cop/style/word_array_spec.rb +1 -1
  256. data/spec/rubocop/cop/team_spec.rb +158 -0
  257. data/spec/rubocop/cop/variable_inspector/assignment_spec.rb +217 -0
  258. data/spec/rubocop/cop/variable_inspector/locatable_spec.rb +740 -0
  259. data/spec/rubocop/cop/variable_inspector/scope_spec.rb +191 -0
  260. data/spec/rubocop/cop/variable_inspector/variable_spec.rb +79 -0
  261. data/spec/rubocop/cop/variable_inspector/variable_table_spec.rb +275 -0
  262. data/spec/rubocop/cop/variable_inspector_spec.rb +13 -533
  263. data/spec/rubocop/formatter/clang_style_formatter_spec.rb +4 -4
  264. data/spec/rubocop/formatter/disabled_config_formatter_spec.rb +1 -1
  265. data/spec/rubocop/formatter/emacs_style_formatter_spec.rb +3 -3
  266. data/spec/rubocop/formatter/file_list_formatter_spec.rb +3 -3
  267. data/spec/rubocop/formatter/progress_formatter_spec.rb +1 -1
  268. data/spec/spec_helper.rb +5 -1
  269. data/spec/support/ast_helper.rb +15 -0
  270. data/spec/support/shared_context.rb +18 -0
  271. data/spec/support/shared_examples.rb +1 -1
  272. metadata +95 -32
  273. checksums.yaml +0 -7
  274. data/lib/rubocop/cop/lint/unused_local_variable.rb +0 -32
  275. data/lib/rubocop/cop/style/avoid_perl_backrefs.rb +0 -19
  276. data/spec/rubocop/cop/lint/unused_local_variable_spec.rb +0 -588
  277. data/spec/rubocop/cop/style/avoid_class_vars_spec.rb +0 -27
  278. data/spec/rubocop/cop/style/avoid_perl_backrefs_spec.rb +0 -20
  279. data/spec/rubocop/cop/style/avoid_perlisms_spec.rb +0 -47
@@ -0,0 +1,740 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ module VariableInspector
8
+ describe Locatable do
9
+ include ASTHelper
10
+ include AST::Sexp
11
+
12
+ class LocatableObject
13
+ include Locatable
14
+
15
+ attr_reader :node, :scope
16
+
17
+ def initialize(node, scope)
18
+ @node = node
19
+ @scope = scope
20
+ end
21
+ end
22
+
23
+ let(:ast) do
24
+ processed_source = Rubocop::SourceParser.parse(source)
25
+ processed_source.ast
26
+ end
27
+
28
+ let(:def_node) do
29
+ found_node = scan_node(ast, include_origin_node: true) do |node|
30
+ break node if node.type == :def
31
+ end
32
+ fail 'No def node found!' unless found_node
33
+ found_node
34
+ end
35
+
36
+ let(:lvasgn_node) do
37
+ found_node = scan_node(ast) do |node|
38
+ break node if node.type == :lvasgn
39
+ end
40
+ fail 'No lvasgn node found!' unless found_node
41
+ found_node
42
+ end
43
+
44
+ let(:scope) { Scope.new(def_node) }
45
+ let(:assignment) { LocatableObject.new(lvasgn_node, scope) }
46
+
47
+ describe '#ancestor_nodes_in_scope' do
48
+ let(:source) do
49
+ <<-END
50
+ class SomeClass
51
+ def some_method(flag)
52
+ puts 'Hello World!'
53
+
54
+ if flag > 0
55
+ foo = 1
56
+ end
57
+ end
58
+ end
59
+ END
60
+ end
61
+
62
+ it 'returns its ancestor nodes in the scope excluding scope node' do
63
+ ancestor_types = assignment.ancestor_nodes_in_scope.map(&:type)
64
+ expect(ancestor_types).to eq([:begin, :if])
65
+ end
66
+ end
67
+
68
+ describe '#branch_point_node' do
69
+ context 'when it is not in branch' do
70
+ let(:source) do
71
+ <<-END
72
+ def some_method(flag)
73
+ foo = 1
74
+ end
75
+ END
76
+ end
77
+
78
+ it 'returns nil' do
79
+ expect(assignment.branch_point_node).to be_nil
80
+ end
81
+ end
82
+
83
+ context 'when it is inside of if' do
84
+ let(:source) do
85
+ <<-END
86
+ def some_method(flag)
87
+ if flag
88
+ foo = 1
89
+ end
90
+ end
91
+ END
92
+ end
93
+
94
+ it 'returns the if node' do
95
+ expect(assignment.branch_point_node.type).to eq(:if)
96
+ end
97
+ end
98
+
99
+ context 'when it is inside of else of if' do
100
+ let(:source) do
101
+ <<-END
102
+ def some_method(flag)
103
+ if flag
104
+ else
105
+ foo = 1
106
+ end
107
+ end
108
+ END
109
+ end
110
+
111
+ it 'returns the if node' do
112
+ expect(assignment.branch_point_node.type).to eq(:if)
113
+ end
114
+ end
115
+
116
+ context 'when it is inside of if condition' do
117
+ let(:source) do
118
+ <<-END
119
+ def some_method(flag)
120
+ if foo = 1
121
+ do_something
122
+ end
123
+ end
124
+ END
125
+ end
126
+
127
+ it 'returns nil' do
128
+ expect(assignment.branch_point_node).to be_nil
129
+ end
130
+ end
131
+
132
+ context 'when multiple if are nested' do
133
+ context 'and it is inside of inner if' do
134
+ let(:source) do
135
+ <<-END
136
+ def some_method(a, b)
137
+ if a
138
+ if b
139
+ foo = 1
140
+ end
141
+ end
142
+ end
143
+ END
144
+ end
145
+
146
+ it 'returns inner if node' do
147
+ if_node = assignment.branch_point_node
148
+ expect(if_node.type).to eq(:if)
149
+ condition_node = if_node.children.first
150
+ expect(condition_node).to eq(s(:lvar, :b))
151
+ end
152
+ end
153
+
154
+ context 'and it is inside of inner if condition' do
155
+ let(:source) do
156
+ <<-END
157
+ def some_method(a, b)
158
+ if a
159
+ if foo = 1
160
+ do_something
161
+ end
162
+ end
163
+ end
164
+ END
165
+ end
166
+
167
+ it 'returns the next outer if node' do
168
+ if_node = assignment.branch_point_node
169
+ expect(if_node.type).to eq(:if)
170
+ condition_node = if_node.children.first
171
+ expect(condition_node).to eq(s(:lvar, :a))
172
+ end
173
+ end
174
+ end
175
+
176
+ context 'when it is inside of when of case' do
177
+ let(:source) do
178
+ <<-END
179
+ def some_method(flag)
180
+ case flag
181
+ when 1
182
+ foo = 1
183
+ end
184
+ end
185
+ END
186
+ end
187
+
188
+ it 'returns the case node' do
189
+ expect(assignment.branch_point_node.type).to eq(:case)
190
+ end
191
+ end
192
+
193
+ context 'when it is on the left side of &&' do
194
+ let(:source) do
195
+ <<-END
196
+ def some_method
197
+ (foo = 1) && do_something
198
+ end
199
+ END
200
+ end
201
+
202
+ it 'returns nil' do
203
+ expect(assignment.branch_point_node).to be_nil
204
+ end
205
+ end
206
+
207
+ context 'when it is on the right side of &&' do
208
+ let(:source) do
209
+ <<-END
210
+ def some_method
211
+ do_something && (foo = 1)
212
+ end
213
+ END
214
+ end
215
+
216
+ it 'returns the and node' do
217
+ expect(assignment.branch_point_node.type).to eq(:and)
218
+ end
219
+ end
220
+
221
+ context 'when it is on the left side of ||' do
222
+ let(:source) do
223
+ <<-END
224
+ def some_method
225
+ (foo = 1) || do_something
226
+ end
227
+ END
228
+ end
229
+
230
+ it 'returns nil' do
231
+ expect(assignment.branch_point_node).to be_nil
232
+ end
233
+ end
234
+
235
+ context 'when it is on the right side of ||' do
236
+ let(:source) do
237
+ <<-END
238
+ def some_method
239
+ do_something || (foo = 1)
240
+ end
241
+ END
242
+ end
243
+
244
+ it 'returns the or node' do
245
+ expect(assignment.branch_point_node.type).to eq(:or)
246
+ end
247
+ end
248
+
249
+ context 'when multiple && are chained' do
250
+ context 'and it is on the right side of the right &&' do
251
+ let(:source) do
252
+ <<-END
253
+ def some_method
254
+ do_something && do_anything && (foo = 1)
255
+ end
256
+ END
257
+ end
258
+
259
+ it 'returns the right and node' do
260
+ and_node = assignment.branch_point_node
261
+ expect(and_node.type).to eq(:and)
262
+ right_side_node = and_node.children[1]
263
+ expect(right_side_node.type).to eq(:begin)
264
+ end
265
+ end
266
+
267
+ context 'and it is on the right side of the left &&' do
268
+ let(:source) do
269
+ <<-END
270
+ def some_method
271
+ do_something && (foo = 1) && do_anything
272
+ end
273
+ END
274
+ end
275
+
276
+ it 'returns the left and node' do
277
+ and_node = assignment.branch_point_node
278
+ expect(and_node.type).to eq(:and)
279
+ right_side_node = and_node.children[1]
280
+ expect(right_side_node.type).to eq(:begin)
281
+ end
282
+ end
283
+ end
284
+
285
+ context 'when it is inside of begin with rescue' do
286
+ let(:source) do
287
+ <<-END
288
+ def some_method(flag)
289
+ begin
290
+ foo = 1
291
+ rescue
292
+ do_something
293
+ end
294
+ end
295
+ END
296
+ end
297
+
298
+ it 'returns the rescue node' do
299
+ expect(assignment.branch_point_node.type).to eq(:rescue)
300
+ end
301
+ end
302
+
303
+ context 'when it is inside of rescue' do
304
+ let(:source) do
305
+ <<-END
306
+ def some_method(flag)
307
+ begin
308
+ do_something
309
+ rescue
310
+ foo = 1
311
+ end
312
+ end
313
+ END
314
+ end
315
+
316
+ it 'returns the rescue node' do
317
+ expect(assignment.branch_point_node.type).to eq(:rescue)
318
+ end
319
+ end
320
+
321
+ context 'when it is inside of begin with ensure' do
322
+ let(:source) do
323
+ <<-END
324
+ def some_method(flag)
325
+ begin
326
+ foo = 1
327
+ ensure
328
+ do_something
329
+ end
330
+ end
331
+ END
332
+ end
333
+
334
+ it 'returns the ensure node' do
335
+ expect(assignment.branch_point_node.type).to eq(:ensure)
336
+ end
337
+ end
338
+
339
+ context 'when it is inside of ensure' do
340
+ let(:source) do
341
+ <<-END
342
+ def some_method(flag)
343
+ begin
344
+ do_something
345
+ ensure
346
+ foo = 1
347
+ end
348
+ end
349
+ END
350
+ end
351
+
352
+ it 'returns nil' do
353
+ expect(assignment.branch_point_node).to be_nil
354
+ end
355
+ end
356
+
357
+ context 'when it is inside of begin without rescue' do
358
+ let(:source) do
359
+ <<-END
360
+ def some_method(flag)
361
+ begin
362
+ foo = 1
363
+ end
364
+ end
365
+ END
366
+ end
367
+
368
+ it 'returns nil' do
369
+ expect(assignment.branch_point_node).to be_nil
370
+ end
371
+ end
372
+ end
373
+
374
+ describe '#branch_body_node' do
375
+ context 'when it is not in branch' do
376
+ let(:source) do
377
+ <<-END
378
+ def some_method(flag)
379
+ foo = 1
380
+ end
381
+ END
382
+ end
383
+
384
+ it 'returns nil' do
385
+ expect(assignment.branch_body_node).to be_nil
386
+ end
387
+ end
388
+
389
+ context 'when it is inside body of if' do
390
+ let(:source) do
391
+ <<-END
392
+ def some_method(flag)
393
+ if flag
394
+ foo = 1
395
+ puts foo
396
+ end
397
+ end
398
+ END
399
+ end
400
+
401
+ it 'returns the body node' do
402
+ expect(assignment.branch_body_node.type).to eq(:begin)
403
+ end
404
+ end
405
+
406
+ context 'when it is inside body of else of if' do
407
+ let(:source) do
408
+ <<-END
409
+ def some_method(flag)
410
+ if flag
411
+ do_something
412
+ else
413
+ foo = 1
414
+ puts foo
415
+ end
416
+ end
417
+ END
418
+ end
419
+
420
+ it 'returns the body node' do
421
+ expect(assignment.branch_body_node.type).to eq(:begin)
422
+ end
423
+ end
424
+
425
+ context 'when it is on the right side of &&' do
426
+ let(:source) do
427
+ <<-END
428
+ def some_method
429
+ do_something && (foo = 1)
430
+ end
431
+ END
432
+ end
433
+
434
+ it 'returns the right side node' do
435
+ expect(assignment.branch_body_node.type).to eq(:begin)
436
+ end
437
+ end
438
+
439
+ context 'when it is on the right side of ||' do
440
+ let(:source) do
441
+ <<-END
442
+ def some_method
443
+ do_something || (foo = 1)
444
+ end
445
+ END
446
+ end
447
+
448
+ it 'returns the right side node' do
449
+ expect(assignment.branch_body_node.type).to eq(:begin)
450
+ end
451
+ end
452
+
453
+ context 'when it is inside of begin with rescue' do
454
+ let(:source) do
455
+ <<-END
456
+ def some_method(flag)
457
+ begin
458
+ foo = 1
459
+ rescue
460
+ do_something
461
+ end
462
+ end
463
+ END
464
+ end
465
+
466
+ it 'returns the body node' do
467
+ expect(assignment.branch_body_node.type).to eq(:lvasgn)
468
+ end
469
+ end
470
+
471
+ context 'when it is inside of rescue' do
472
+ let(:source) do
473
+ <<-END
474
+ def some_method(flag)
475
+ begin
476
+ do_something
477
+ rescue
478
+ foo = 1
479
+ end
480
+ end
481
+ END
482
+ end
483
+
484
+ it 'returns the resbody node' do
485
+ expect(assignment.branch_body_node.type).to eq(:resbody)
486
+ end
487
+ end
488
+
489
+ context 'when it is inside of begin with ensure' do
490
+ let(:source) do
491
+ <<-END
492
+ def some_method(flag)
493
+ begin
494
+ foo = 1
495
+ ensure
496
+ do_something
497
+ end
498
+ end
499
+ END
500
+ end
501
+
502
+ it 'returns the body node' do
503
+ expect(assignment.branch_body_node.type).to eq(:lvasgn)
504
+ end
505
+ end
506
+ end
507
+
508
+ describe '#branch_id' do
509
+ context 'when it is not in branch' do
510
+ let(:source) do
511
+ <<-END
512
+ def some_method(flag)
513
+ foo = 1
514
+ end
515
+ END
516
+ end
517
+
518
+ it 'returns nil' do
519
+ expect(assignment.branch_id).to be_nil
520
+ end
521
+ end
522
+
523
+ context 'when it is inside body of if' do
524
+ let(:source) do
525
+ <<-END
526
+ def some_method(flag)
527
+ if flag
528
+ foo = 1
529
+ puts foo
530
+ end
531
+ end
532
+ END
533
+ end
534
+
535
+ it 'returns BRANCHNODEID_if_true' do
536
+ expect(assignment.branch_id).to match(/^\d+_if_true/)
537
+ end
538
+ end
539
+
540
+ context 'when it is inside body of else of if' do
541
+ let(:source) do
542
+ <<-END
543
+ def some_method(flag)
544
+ if flag
545
+ do_something
546
+ else
547
+ foo = 1
548
+ puts foo
549
+ end
550
+ end
551
+ END
552
+ end
553
+
554
+ it 'returns BRANCHNODEID_if_false' do
555
+ expect(assignment.branch_id).to match(/^\d+_if_false/)
556
+ end
557
+ end
558
+
559
+ context 'when it is inside body of when of case' do
560
+ let(:source) do
561
+ <<-END
562
+ def some_method(flag)
563
+ case flag
564
+ when first
565
+ do_something
566
+ when second
567
+ foo = 1
568
+ puts foo
569
+ else
570
+ do_something
571
+ end
572
+ end
573
+ END
574
+ end
575
+
576
+ it 'returns BRANCHNODEID_case_whenINDEX' do
577
+ expect(assignment.branch_id).to match(/^\d+_case_when1/)
578
+ end
579
+ end
580
+
581
+ context 'when it is inside body of when of case' do
582
+ let(:source) do
583
+ <<-END
584
+ def some_method(flag)
585
+ case flag
586
+ when first
587
+ do_something
588
+ when second
589
+ do_something
590
+ else
591
+ foo = 1
592
+ puts foo
593
+ end
594
+ end
595
+ END
596
+ end
597
+
598
+ it 'returns BRANCHNODEID_case_else' do
599
+ expect(assignment.branch_id).to match(/^\d+_case_else/)
600
+ end
601
+ end
602
+
603
+ context 'when it is on the left side of &&' do
604
+ let(:source) do
605
+ <<-END
606
+ def some_method
607
+ (foo = 1) && do_something
608
+ end
609
+ END
610
+ end
611
+
612
+ it 'returns nil' do
613
+ expect(assignment.branch_id).to be_nil
614
+ end
615
+ end
616
+
617
+ context 'when it is on the right side of &&' do
618
+ let(:source) do
619
+ <<-END
620
+ def some_method
621
+ do_something && (foo = 1)
622
+ end
623
+ END
624
+ end
625
+
626
+ it 'returns BRANCHNODEID_and_right' do
627
+ expect(assignment.branch_id).to match(/^\d+_and_right/)
628
+ end
629
+ end
630
+
631
+ context 'when it is on the left side of ||' do
632
+ let(:source) do
633
+ <<-END
634
+ def some_method
635
+ (foo = 1) || do_something
636
+ end
637
+ END
638
+ end
639
+
640
+ it 'returns nil' do
641
+ expect(assignment.branch_id).to be_nil
642
+ end
643
+ end
644
+
645
+ context 'when it is on the right side of ||' do
646
+ let(:source) do
647
+ <<-END
648
+ def some_method
649
+ do_something || (foo = 1)
650
+ end
651
+ END
652
+ end
653
+
654
+ it 'returns BRANCHNODEID_or_right' do
655
+ expect(assignment.branch_id).to match(/^\d+_or_right/)
656
+ end
657
+ end
658
+
659
+ context 'when it is inside of begin with rescue' do
660
+ let(:source) do
661
+ <<-END
662
+ def some_method(flag)
663
+ begin
664
+ foo = 1
665
+ rescue
666
+ do_something
667
+ end
668
+ end
669
+ END
670
+ end
671
+
672
+ it 'returns BRANCHNODEID_rescue_main' do
673
+ expect(assignment.branch_id).to match(/^\d+_rescue_main/)
674
+ end
675
+ end
676
+
677
+ context 'when it is inside of rescue' do
678
+ let(:source) do
679
+ <<-END
680
+ def some_method(flag)
681
+ begin
682
+ do_something
683
+ rescue FirstError
684
+ do_something
685
+ rescue SecondError
686
+ foo = 1
687
+ end
688
+ end
689
+ END
690
+ end
691
+
692
+ it 'returns BRANCHNODEID_rescue_rescueINDEX' do
693
+ expect(assignment.branch_id).to match(/^\d+_rescue_rescue1/)
694
+ end
695
+ end
696
+
697
+ context 'when it is inside of else of rescue' do
698
+ let(:source) do
699
+ <<-END
700
+ def some_method(flag)
701
+ begin
702
+ do_something
703
+ rescue FirstError
704
+ do_something
705
+ rescue SecondError
706
+ do_something
707
+ else
708
+ foo = 1
709
+ end
710
+ end
711
+ END
712
+ end
713
+
714
+ it 'returns BRANCHNODEID_rescue_else' do
715
+ expect(assignment.branch_id).to match(/^\d+_rescue_else/)
716
+ end
717
+ end
718
+
719
+ context 'when it is inside of begin with ensure' do
720
+ let(:source) do
721
+ <<-END
722
+ def some_method(flag)
723
+ begin
724
+ foo = 1
725
+ ensure
726
+ do_something
727
+ end
728
+ end
729
+ END
730
+ end
731
+
732
+ it 'returns BRANCHNODEID_ensure_main' do
733
+ expect(assignment.branch_id).to match(/^\d+_ensure_main/)
734
+ end
735
+ end
736
+ end
737
+ end
738
+ end
739
+ end
740
+ end