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:



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

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

.valid_filename?(filename) ⇒ Boolean

Parameters:

  • filename (String)

Returns:

  • (Boolean)


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

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

Instance Method Details

#global(_yard_map) ⇒ Environ

Parameters:

  • _yard_map (YardMap)

Returns:

  • (Environ)


84
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
111
112
113
114
115
116
117
# File 'lib/solargraph/rspec/convention.rb', line 84

def global(_yard_map)
  pins = []
  pins += Solargraph::Rspec::Gems.include_helper_pins(
    root_example_group_namespace_pin: root_example_group_namespace_pin
  )
  pins += annotation_pins

  # Add pins from RSpec.configure blocks
  rspec_configure = RSpecConfigure.new(
    config: config,
    root_namespace_pin: root_example_group_namespace_pin
  )
  pins += rspec_configure.pins

  # TODO: Include gem requires conditionally based on Gemfile definition
  requires = Solargraph::Rspec::Gems.gem_names + rspec_configure.extra_requires

  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, SyntaxError => 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)


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
152
153
154
155
156
157
158
# File 'lib/solargraph/rspec/convention.rb', line 121

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, SyntaxError => 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