lib/rubocop/cop/style/hash_syntax.rb in rubocop-1.11.0 vs lib/rubocop/cop/style/hash_syntax.rb in rubocop-1.12.0

- old
+ new

@@ -52,13 +52,14 @@ # {c: 2, 'd' => 3} # should just use hash rockets # # # good # {a: 1, b: 2} # {:c => 3, 'd' => 4} - class HashSyntax < Cop + class HashSyntax < Base include ConfigurableEnforcedStyle include RangeHelp + extend AutoCorrector MSG_19 = 'Use the new Ruby 1.9 hash syntax.' MSG_NO_MIXED_KEYS = "Don't mix styles in the same hash." MSG_HASH_ROCKETS = 'Use hash rockets syntax.' @@ -102,22 +103,10 @@ else check(pairs, ':', MSG_NO_MIXED_KEYS) end end - def autocorrect(node) - lambda do |corrector| - if style == :hash_rockets || force_hash_rockets?(node.parent.pairs) - autocorrect_hash_rockets(corrector, node) - elsif style == :ruby19_no_mixed_keys || style == :no_mixed_keys - autocorrect_no_mixed_keys(corrector, node) - else - autocorrect_ruby19(corrector, node) - end - end - end - def alternative_style case style when :hash_rockets :ruby19 when :ruby19, :ruby19_no_mixed_keys @@ -125,10 +114,20 @@ end end private + def autocorrect(corrector, node) + if style == :hash_rockets || force_hash_rockets?(node.parent.pairs) + autocorrect_hash_rockets(corrector, node) + elsif style == :ruby19_no_mixed_keys || style == :no_mixed_keys + autocorrect_no_mixed_keys(corrector, node) + else + autocorrect_ruby19(corrector, node) + end + end + def sym_indices?(pairs) pairs.all? { |p| word_symbol_pair?(p) } end def word_symbol_pair?(pair) @@ -150,17 +149,19 @@ # Most hash keys can be matched against a simple regex. return true if /\A[_a-z]\w*[?!]?\z/i.match?(sym_name) # For more complicated hash keys, let the parser validate the syntax. - parse("{ #{sym_name}: :foo }").valid_syntax? + ProcessedSource.new("{ #{sym_name}: :foo }", target_ruby_version).valid_syntax? end def check(pairs, delim, msg) pairs.each do |pair| if pair.delimiter == delim location = pair.source_range.begin.join(pair.loc.operator) - add_offense(pair, location: location, message: msg) do + add_offense(location, message: msg) do |corrector| + autocorrect(corrector, pair) + opposite_style_detected end else correct_style_detected end