Class: Tapioca::Compilers::RspecDynamicMatchers

Inherits:
Dsl::Compiler
  • Object
show all
Extended by:
T::Generic
Defined in:
sorbet/tapioca/compilers/rspec_dynamic_matchers.rb

Constant Summary collapse

ConstantType =
type_member { { fixed: T::Module[T.anything] } }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gather_constants ⇒ Enumerable<T::Module[T.anything]>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



14
15
16
# File 'sorbet/tapioca/compilers/rspec_dynamic_matchers.rb', line 14

def self.gather_constants
  [::RSpec::Matchers]
end

Instance Method Details

#decorate ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
# File 'sorbet/tapioca/compilers/rspec_dynamic_matchers.rb', line 19

def decorate
  used_matchers = T.let(Set.new, T::Set[String])
  Dir[File.join(__dir__, "../../../test/**/*_spec.rb")].each do |file|
    File.read(file).scan(/\b(?:be|have)_[a-z0-9_]+\b/) do |name|
      used_matchers.add(name)
    end
  end

  declared_dynamic_matchers = T.let(Set.new, T::Set[String])
  Dir[File.join(__dir__, "../../../test/**/*.rb")].select { |file| File.file?(file) }.each do |file|
    content = File.read(file)

    content.scan(/\b(?:RSpec::Matchers\.)?define\s+:([a-z][a-z0-9_]*[!?]?)/) do |captures|
      declared_dynamic_matchers.add(captures.first)
    end
    content.scan(/\b(?:RSpec::Matchers\.)?define_negated_matcher\s+:([a-z][a-z0-9_]*[!?]?)/) do |captures|
      declared_dynamic_matchers.add(captures.first)
    end
    content.scan(/\b(?:RSpec::Matchers\.)?alias_matcher\s+:([a-z][a-z0-9_]*[!?]?)/) do |captures|
      declared_dynamic_matchers.add(captures.first)
    end
    content.scan(/\bmatcher\s+:([a-z][a-z0-9_]*[!?]?)/) do |captures|
      declared_dynamic_matchers.add(captures.first)
    end
  end

  known_rspec_matchers = T.let(Set.new, T::Set[String])
  Dir[File.join(__dir__, "../../rbi/gems/rspec-expectations@*.rbi")].each do |file|
    File.read(file).scan(/^\s*def\s+([a-z][a-z0-9_]*[!?]?)/) do |captures|
      known_rspec_matchers.add(captures.first)
    end
  end

  root.create_path(constant) do |mod|
    (used_matchers + declared_dynamic_matchers - known_rspec_matchers).to_a.sort.each do |name|
      mod.create_method(
        name,
        parameters: [
          create_rest_param("args", type: "T.untyped"),
          create_block_param("block", type: "T.untyped"),
        ],
      )
    end
  end
end