Sha256: 23e4fc2c43413362ec2c9b12ddd99ed12bf66b207415a05adf2ea9944fffb7c7

Contents?: true

Size: 1.01 KB

Versions: 7

Compression:

Stored size: 1.01 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
          return
        end

        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

7 entries across 7 versions & 1 rubygems

Version Path
rubocop-0.34.2 lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb
rubocop-0.34.1 lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb
rubocop-0.34.0 lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb
rubocop-0.33.0 lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb
rubocop-0.32.1 lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb
rubocop-0.32.0 lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb
rubocop-0.31.0 lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb