Sha256: 30799f1b6a79d73ebc5a39982c360c20d6c8d0cba3a9dbf39c07ee852c6202c8

Contents?: true

Size: 1.78 KB

Versions: 18

Compression:

Stored size: 1.78 KB

Contents

# typed: strict
# frozen_string_literal: true

module RBI
  module Rewriters
    class AddSigTemplates < Visitor
      extend T::Sig

      sig { params(with_todo_comment: T::Boolean).void }
      def initialize(with_todo_comment: true)
        super()
        @with_todo_comment = with_todo_comment
      end

      sig { override.params(node: T.nilable(Node)).void }
      def visit(node)
        case node
        when Tree
          visit_all(node.nodes)
        when Attr
          add_attr_sig(node)
        when Method
          add_method_sig(node)
        end
      end

      private

      sig { params(attr: Attr).void }
      def add_attr_sig(attr)
        return unless attr.sigs.empty?
        return if attr.names.size > 1

        params = []
        params << SigParam.new(attr.names.first.to_s, "T.untyped") if attr.is_a?(AttrWriter)

        attr.sigs << Sig.new(
          params: params,
          return_type: "T.untyped",
        )
        add_todo_comment(attr)
      end

      sig { params(method: Method).void }
      def add_method_sig(method)
        return unless method.sigs.empty?

        method.sigs << Sig.new(
          params: method.params.map { |param| SigParam.new(param.name, "T.untyped") },
          return_type: "T.untyped",
        )
        add_todo_comment(method)
      end

      sig { params(node: NodeWithComments).void }
      def add_todo_comment(node)
        node.comments << Comment.new("TODO: fill in signature with appropriate type information") if @with_todo_comment
      end
    end
  end

  class Tree
    extend T::Sig

    sig { params(with_todo_comment: T::Boolean).void }
    def add_sig_templates!(with_todo_comment: true)
      visitor = Rewriters::AddSigTemplates.new(with_todo_comment: with_todo_comment)
      visitor.visit(self)
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
rbi-0.2.1 lib/rbi/rewriters/add_sig_templates.rb
rbi-0.2.0 lib/rbi/rewriters/add_sig_templates.rb
rbi-0.1.14 lib/rbi/rewriters/add_sig_templates.rb
rbi-0.1.13 lib/rbi/rewriters/add_sig_templates.rb
rbi-0.1.12 lib/rbi/rewriters/add_sig_templates.rb
rbi-0.1.11 lib/rbi/rewriters/add_sig_templates.rb
rbi-0.1.10 lib/rbi/rewriters/add_sig_templates.rb
rbi-0.1.9 lib/rbi/rewriters/add_sig_templates.rb
rbi-0.1.8 lib/rbi/rewriters/add_sig_templates.rb
rbi-0.1.7 lib/rbi/rewriters/add_sig_templates.rb
rbi-0.1.6 lib/rbi/rewriters/add_sig_templates.rb
rbi-0.1.5 lib/rbi/rewriters/add_sig_templates.rb
rbi-0.1.4 lib/rbi/rewriters/add_sig_templates.rb
rbi-0.1.3 lib/rbi/rewriters/add_sig_templates.rb
rbi-0.1.2 lib/rbi/rewriters/add_sig_templates.rb
rbi-0.1.1 lib/rbi/rewriters/add_sig_templates.rb
rbi-0.1.0 lib/rbi/rewriters/add_sig_templates.rb
rbi-0.0.17 lib/rbi/rewriters/add_sig_templates.rb