Sha256: f0dda90984dd5c852f1a441947abecba0bd9f6a9b819279edeee85cc06749596

Contents?: true

Size: 1.04 KB

Versions: 5

Compression:

Stored size: 1.04 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    # This module does auto-correction of nodes that could become grammatically
    # different after the correction. If the code change would alter the
    # abstract syntax tree, it is not done.
    module AutocorrectUnlessChangingAST
      def autocorrect(node)
        current_buffer_src = processed_source.buffer.source
        replaced_range = node.loc.expression
        pre = current_buffer_src[0...replaced_range.begin_pos]
        post = current_buffer_src[replaced_range.end_pos..-1]
        new_buffer_src = pre + rewrite_node(node) + post

        # Make the correction only if it doesn't change the AST for the buffer.
        if processed_source.ast != ProcessedSource.new(new_buffer_src).ast
          fail CorrectionNotPossible
        end

        @corrections << correction(node)
      end

      private

      def rewrite_node(node)
        ps = ProcessedSource.new(node.loc.expression.source)
        c = correction(ps.ast)
        Corrector.new(ps.buffer, [c]).rewrite
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubocop-0.30.1 lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb
rubocop-0.30.0 lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb
rubocop-0.29.1 lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb
rubocop-0.29.0 lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb
rubocop-0.28.0 lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb