Sha256: e5c9cdeb054f0a9060abd2b2bb26cac5032bd59f3819ef2793f8941f31b70205

Contents?: true

Size: 1.23 KB

Versions: 4

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

module RuboCop
  module AST
    class NodePattern
      class Compiler
        # Base class for subcompilers
        # Implements visitor pattern
        #
        # Doc on how this fits in the compiling process:
        #   /doc/modules/ROOT/pages/node_pattern.md
        class Subcompiler
          attr_reader :compiler

          def initialize(compiler)
            @compiler = compiler
            @node = nil
          end

          def compile(node)
            prev = @node
            @node = node
            do_compile
          ensure
            @node = prev
          end

          # @api private

          private

          attr_reader :node

          def do_compile
            send(self.class.registry.fetch(node.type, :visit_other_type))
          end

          @registry = {}
          class << self
            attr_reader :registry

            def method_added(method)
              @registry[Regexp.last_match(1).to_sym] = method if method =~ /^visit_(.*)/
              super
            end

            def inherited(base)
              us = self
              base.class_eval { @registry = us.registry.dup }
              super
            end
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-ast-1.0.0 lib/rubocop/ast/node_pattern/compiler/subcompiler.rb
rubocop-ast-0.8.0 lib/rubocop/ast/node_pattern/compiler/subcompiler.rb
rubocop-ast-0.7.1 lib/rubocop/ast/node_pattern/compiler/subcompiler.rb
rubocop-ast-0.7.0 lib/rubocop/ast/node_pattern/compiler/subcompiler.rb