Class: Solargraph::Rspec::Convention

Inherits:
Convention::Base
  • Object
show all
Defined in:
lib/solargraph/rspec/convention.rb

Overview

Provides completion for RSpec DSL and helper methods.

- `describe` and `context` blocks
- `let` and `let!` methods
- `subject` method
- `described_class` method
- `it` method with correct binding
- `RSpec::Matchers` module

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configConfig

Returns:



73
74
75
# File 'lib/solargraph/rspec/convention.rb', line 73

def self.config
  @config ||= Config.new
end

.valid_filename?(filename) ⇒ Boolean

Parameters:

  • filename (String)

Returns:

  • (Boolean)


79
80
81
# File 'lib/solargraph/rspec/convention.rb', line 79

def self.valid_filename?(filename)
  filename.include?('spec/')
end

Instance Method Details

#global(_yard_map) ⇒ Environ

Parameters:

  • yard_map (YardMap)

Returns:

  • (Environ)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/solargraph/rspec/convention.rb', line 85

def global(_yard_map)
  pins = []
  pins += Solargraph::Rspec::TestHelpers.include_helper_pins(
    root_example_group_namespace_pin: root_example_group_namespace_pin
  )
  pins += annotation_pins
  # TODO: Include gem requires conditionally based on Gemfile definition
  requires = Solargraph::Rspec::TestHelpers.gem_names

  if pins.any?
    Solargraph.logger.debug(
      "[RSpec] added global pins #{pins.map(&:inspect)}"
    )
  end

  Solargraph.logger.debug "[RSpec] added requires #{requires}"

  Environ.new(requires: requires, pins: pins)
rescue StandardError => e
  raise e if ENV['SOLARGRAPH_DEBUG']

  Solargraph.logger.warn(
    "[RSpec] Error processing global pins: #{e.message}\n#{e.backtrace.join("\n")}"
  )
  EMPTY_ENVIRON
end

#local(source_map) ⇒ Environ

Parameters:

  • source_map (SourceMap)

Returns:

  • (Environ)


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/solargraph/rspec/convention.rb', line 114

def local(source_map)
  Solargraph.logger.debug "[RSpec] processing #{source_map.filename}"

  return EMPTY_ENVIRON unless self.class.valid_filename?(source_map.filename)

  # @type [Array<Pin::Base>]
  pins = []
  # @type [Array<Pin::Namespace>]
  namespace_pins = []

  rspec_walker = SpecWalker.new(source_map: source_map, config: config)

  CORRECTOR_CLASSES.each do |corrector_class|
    corrector_class.new(
      namespace_pins: namespace_pins,
      rspec_walker: rspec_walker,
      added_pins: pins
    ).correct(source_map)
  end

  rspec_walker.walk!
  pins += namespace_pins

  if pins.any?
    Solargraph.logger.debug(
      "[RSpec] added #{pins.map(&:inspect)} to #{source_map.filename}"
    )
  end

  Environ.new(requires: [], pins: pins)
rescue StandardError => e
  raise e if ENV['SOLARGRAPH_DEBUG']

  Solargraph.logger.warn(
    "[RSpec] Error processing #{source_map.filename}: #{e.message}\n#{e.backtrace.join("\n")}"
  )
  EMPTY_ENVIRON
end