Class: Tapioca::Compilers::RuboCop

Inherits:
Dsl::Compiler
  • Object
show all
Defined in:
sorbet/tapioca/compilers/rubocop.rb

Constant Summary collapse

ConstantType =
type_member { { fixed: Module } }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gather_constantsObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'sorbet/tapioca/compilers/rubocop.rb', line 19

def self.gather_constants
  all_modules.select do |klass|
    next unless klass.singleton_class < ::RuboCop::AST::NodePattern::Macros

    path = T.must(Object.const_source_location(klass.to_s)).fetch(0).to_s
    # exclude vendored code, to avoid contradicting their RBI files
    !path.include?("/vendor/bundle/ruby/") &&
      # exclude source code that already has an RBI file
      !File.exist?("#{path}i") &&
      # exclude source code that doesn't use the DSLs
      File.readlines(path).any?(/def_node_/)
  end
end

Instance Method Details

#decoratevoid

This method returns an undefined value.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'sorbet/tapioca/compilers/rubocop.rb', line 34

def decorate
  root.create_path(constant) do |klass|
    constant.instance_methods(false).each do |method_name|
      source = constant.instance_method(method_name).source.lstrip
      # For more info on these DSLs:
      #   https://www.rubydoc.info/gems/rubocop-ast/RuboCop/AST/NodePattern/Macros
      #   https://github.com/rubocop/rubocop-ast/blob/master/lib/rubocop/ast/node_pattern.rb
      #   https://github.com/rubocop/rubocop-ast/blob/master/lib/rubocop/ast/node_pattern/method_definer.rb
      # The type signatures below could maybe be stronger, but I only wanted to avoid errors:
      case source
      when /\Adef_node_matcher/
        # https://github.com/Shopify/tapioca/blob/3341a9b/lib/tapioca/rbi_ext/model.rb#L89
        klass.create_method(
          method_name.to_s,
          parameters:  [
            create_param("node", type: "RuboCop::AST::Node"),
            create_kw_rest_param("kwargs", type: "T.untyped"),
            create_block_param("block", type: "T.untyped"),
          ],
          return_type: "T.untyped",
        )
      when /\Adef_node_search/
        klass.create_method(
          method_name.to_s,
          parameters:  [
            create_param("node", type: "RuboCop::AST::Node"),
            create_rest_param("pattern", type: "T.any(String, Symbol)"),
            create_kw_rest_param("kwargs", type: "T.untyped"),
            create_block_param("block", type: "T.untyped"),
          ],
          return_type: method_name.to_s.end_with?("?") ? "T::Boolean" : "T.untyped",
        )
      end
    end
  end
end