solargraph-rspec 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b29106f92a5279085cce258a7aabc92070d5452446a8f5a0b9c141d2a3d5a52d
4
+ data.tar.gz: a153e3a43ff36c494484ecef9e7ef4f31066b0c002f0c3e16505a3357231591f
5
+ SHA512:
6
+ metadata.gz: 7c12a1454723f6acfe6bafcb856ff300734d27429d6ee08f053f2c05b30f7ff3885bb595ce14fb360bbfeec1cbf9339938a52cfa2f63874fce4cbd2d6efacbcf
7
+ data.tar.gz: 3b81a6a82e580bc0e5ff86a0aeb66d61f6f9c8a8e7191ac0df52a31ac482b9dc774cd10b43d881babf80fdd6e60c93dc87a25a7f72ad296aea6d57743c550055
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,41 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+ NewCops: enable
4
+
5
+ Style/StringLiterals:
6
+ Enabled: true
7
+ EnforcedStyle: single_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Style/Documentation:
14
+ Enabled: false
15
+
16
+ Layout/LineLength:
17
+ Max: 120
18
+
19
+ Metrics/ParameterLists:
20
+ Enabled: false
21
+
22
+ Metrics/AbcSize:
23
+ Enabled: false
24
+
25
+ Metrics/CyclomaticComplexity:
26
+ Enabled: false
27
+
28
+ Metrics/PerceivedComplexity:
29
+ Enabled: false
30
+
31
+ Metrics/MethodLength:
32
+ Enabled: false
33
+
34
+ Metrics/ModuleLength:
35
+ Enabled: false
36
+
37
+ Metrics/BlockLength:
38
+ Enabled: false
39
+
40
+ Metrics/ClassLength:
41
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,22 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ -
11
+
12
+ ## [0.1.0] - 2024-05-11 (First Release 🎉)
13
+
14
+ ### Added
15
+
16
+ - `describe` and `it` methods completion
17
+ - memoized `let` and `let!` methods completion
18
+ - implicit and explicit `subject` methods
19
+ - `described_class` with appropriate type inference
20
+ - `RSpec::Matchers` methods completion
21
+ - Completes normal ruby methods within `describe/context` blocks
22
+ - RSpec DSL suggestions (eg. `it`, `describe`, `fit`.. etc.)
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at l.mula@finlink.de. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Lekë Mula
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,115 @@
1
+ # Solargraph::Rspec - A Solargraph plugin for better RSpec support
2
+
3
+ ![Tests](https://github.com/lekemula/solargraph-rspec/actions/workflows/ruby.yml/badge.svg)
4
+ [![codecov](https://codecov.io/gh/lekemula/solargraph-rspec/graph/badge.svg?token=FH7ER8ZDPW)](https://codecov.io/gh/lekemula/solargraph-rspec)
5
+
6
+
7
+ RSpec is a testing framework of choice for many Ruby developers. But at the same time is highly dynamic and heavily relying on metaprogramming making it hard to provide accurate code completion and type inference.
8
+
9
+ This gem aims to provide a better support for RSpec in Solargraph and it supports the following features:
10
+ - `describe` and `it` methods completion
11
+ - memoized `let` and `let!` methods completion
12
+ - implicit and explicit `subject` methods
13
+ - `described_class` with appropriate type inference
14
+ - `RSpec::Matchers` methods completion
15
+ - and more to come...
16
+
17
+ ![solargraph-rspec_smaller](https://github.com/lekemula/solargraph-rspec/assets/9197495/66f86e5b-258e-4d8c-8a64-0685d15ece00)
18
+
19
+ ## Installation
20
+
21
+ ### Install `solargraph` and `solargraph-rspec`
22
+
23
+ Install the gems from the command line:
24
+
25
+ ```bash
26
+ gem install solargraph solargraph-rspec
27
+ ```
28
+
29
+ Or add it to your Gemfile:
30
+
31
+ ```ruby
32
+ group :development do
33
+ gem 'solargraph'
34
+ gem 'solargraph-rspec'
35
+ end
36
+ ```
37
+
38
+ If you add them to your Gemfile, you'll have to tell your IDE plugin to use bundler to load the right version of solargraph.
39
+
40
+ Add `solargraph-rspec` to your `.solargraph.yml` and remove the `spec` directory from the `exclude` list.
41
+
42
+ (if you don't have a `.solargraph.yml` in your project root, you can run `solargraph config` to add one)
43
+
44
+ ```diff
45
+ @@ -2,7 +2,6 @@
46
+ include:
47
+ - "**/*.rb"
48
+ exclude:
49
+ -- spec/**/*
50
+ - test/**/*
51
+ - vendor/**/*
52
+ - ".bundle/**/*"
53
+ @@ -18,5 +17,6 @@ formatter:
54
+ only: []
55
+ extra_args: []
56
+ require_paths: []
57
+ -plugins: []
58
+ +plugins:
59
+ + - solargraph-rspec
60
+ max_files: 5000
61
+ ```
62
+ ### Configuration
63
+
64
+ If you have your own custom `let`-like memoized methods, you can add them to your `.solargraph.yml` file like this:
65
+
66
+ ```yaml
67
+ # .solargraph.yml
68
+ # ...
69
+ rspec:
70
+ let_methods:
71
+ - let_it_be
72
+ ```
73
+
74
+ ### Gem completions
75
+
76
+ Solargraph utilizes the YARD documentation to provide code completion. If you want to have completion for gems in your project, you can generate YARD documentation for them ([Read more](https://solargraph.org/guides/yard)).
77
+
78
+ Run `yard gems` to generate documentation for your installed gems.
79
+
80
+ Run `yard config --gem-install-yri` to generate YARD documentation automatically when you install new gems.
81
+
82
+ ## Acknowledgements
83
+
84
+ This gem is inspired by the [solargraph-rails](https://github.com/iftheshoefritz/solargraph-rails) which gave me an idea of how to extend Solargraph with custom features and provided me with simple and very understandable test suite which helped me to get started with playing around with Solargraph.
85
+
86
+ In fact, most of the code I initially wrote on [a fork](https://github.com/lekemula/solargraph-rails/tree/rspec-support) of it, but then I realized that it would make more sense to extract it into a separate gem where it could be used by non-Rails projects as well.
87
+
88
+ It also goes without saying that the Solargraph gem itself is a great tool that it has helped me a lot in my daily work and I'm very grateful to [@castwide](https://github.com/castwide) for creating it and maintaining it. :heart:
89
+
90
+ It's codebase IMO is an exemplary of how Ruby code written in a very simple POROs without any wild metaprogramming magic and supplemented with YARDocs, for such a complex tools can be very readable and understandable even for a total newbie like me in this domain!
91
+
92
+ ## Contributing
93
+
94
+ ### Bug Reports and Feature Requests
95
+
96
+ [GitHub Issues](https://github.com/lekemula/solargraph-rspec/issues) are the best place to ask questions, report problems, and suggest improvements.
97
+
98
+ ### Development
99
+
100
+ Code contributions are always appreciated. Feel free to fork the repo and submit pull requests. Check for open issues that could use help. Start new issues to discuss changes that have a major impact on the code or require large time commitments.
101
+
102
+ Contributing is easy:
103
+ 1. Create a fork and clone it
104
+ 2. Run `bundle install` to install dependencies
105
+ 3. Run `yard gems` to generate YARD documentation for your installed gems
106
+ 4. Run `bundle exec spec` to run the tests
107
+ 5. Introduce your awesome changes
108
+ 6. Ensure they are well covered with tests
109
+ 7. Record your changes in the [CHANGELOG.md](./CHANGELOG.md)
110
+ 7. Submit a pull request :rocket:
111
+
112
+ ## License
113
+
114
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
115
+
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Solargraph
4
+ module Rspec
5
+ # @example .solargraph.yml configuration for rspec
6
+ # rspec:
7
+ # let_methods:
8
+ # - let_it_be
9
+ class Config
10
+ def initialize(solargraph_config = Solargraph::Workspace::Config.new('./'))
11
+ @solargraph_config = solargraph_config
12
+ Solargraph.logger.debug "[RSpec] Solargraph config: #{raw_data}"
13
+ end
14
+
15
+ # @return [Solargraph::Workspace::Config]
16
+ attr_reader :solargraph_config
17
+
18
+ # @return [Array<Symbol>]
19
+ def let_methods
20
+ (Rspec::LET_METHODS + additional_let_methods).map(&:to_sym)
21
+ end
22
+
23
+ private
24
+
25
+ # @return [Hash]
26
+ def rspec_raw_data
27
+ @rspec_raw_data ||= raw_data['rspec'] || {}
28
+ end
29
+
30
+ # @return [Array<Symbol>]
31
+ def additional_let_methods
32
+ (rspec_raw_data['let_methods'] || []).map(&:to_sym)
33
+ end
34
+
35
+ # @return [Hash]
36
+ def raw_data
37
+ @solargraph_config.raw_data
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,223 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'config'
4
+ require_relative 'spec_walker'
5
+ require_relative 'correctors/context_block_namespace_corrector'
6
+ require_relative 'correctors/example_and_hook_blocks_binding_corrector'
7
+ require_relative 'correctors/described_class_corrector'
8
+ require_relative 'correctors/let_methods_corrector'
9
+ require_relative 'correctors/subject_method_corrector'
10
+ require_relative 'correctors/context_block_methods_corrector'
11
+ require_relative 'correctors/implicit_subject_method_corrector'
12
+ require_relative 'correctors/dsl_methods_corrector'
13
+ require_relative 'util'
14
+
15
+ module Solargraph
16
+ module Rspec
17
+ ROOT_NAMESPACE = 'RSpec::ExampleGroups'
18
+ HELPER_MODULES = ['RSpec::Matchers'].freeze
19
+ HOOK_METHODS = %w[before after around].freeze
20
+ LET_METHODS = %w[let let!].freeze
21
+ EXAMPLE_METHODS = %w[
22
+ example
23
+ it
24
+ specify
25
+ focus
26
+ fexample
27
+ fit
28
+ fspecify
29
+ xexample
30
+ xit
31
+ xspecify
32
+ skip
33
+ pending
34
+ ].freeze
35
+
36
+ CONTEXT_METHODS = %w[
37
+ example_group
38
+ describe
39
+ context
40
+ xdescribe
41
+ xcontext
42
+ fdescribe
43
+ fcontext
44
+ shared_examples
45
+ include_examples
46
+ it_behaves_like
47
+ it_should_behave_like
48
+ shared_context
49
+ include_context
50
+ ].freeze
51
+
52
+ # Provides completion for RSpec DSL and helper methods.
53
+ # - `describe` and `context` blocks
54
+ # - `let` and `let!` methods
55
+ # - `subject` method
56
+ # - `described_class` method
57
+ # - `it` method with correct binding
58
+ # - `RSpec::Matchers` module
59
+ class Convention < Solargraph::Convention::Base
60
+ # @return [Config]
61
+ def self.config
62
+ @config ||= Config.new
63
+ end
64
+
65
+ # @param filename [String]
66
+ # @return [Boolean]
67
+ def self.valid_filename?(filename)
68
+ filename.include?('spec/')
69
+ end
70
+
71
+ # @param yard_map [YardMap]
72
+ # @return [Environ]
73
+ def global(_yard_map)
74
+ pins = []
75
+ pins += include_helper_pins
76
+
77
+ if pins.any?
78
+ Solargraph.logger.debug(
79
+ "[RSpec] added global pins #{pins.map(&:inspect)}"
80
+ )
81
+ end
82
+
83
+ Environ.new(pins: pins)
84
+ rescue StandardError => e
85
+ raise e if ENV['SOLARGRAPH_DEBUG']
86
+
87
+ Solargraph.logger.warn(
88
+ "[RSpec] Error processing global pins: #{e.message}\n#{e.backtrace.join("\n")}"
89
+ )
90
+ EMPTY_ENVIRON
91
+ end
92
+
93
+ # @param source_map [SourceMap]
94
+ # @return [Environ]
95
+ def local(source_map)
96
+ Solargraph.logger.debug "[RSpec] processing #{source_map.filename}"
97
+
98
+ return EMPTY_ENVIRON unless self.class.valid_filename?(source_map.filename)
99
+
100
+ # @type [Array<Pin::Base>]
101
+ pins = []
102
+ # @type [Array<Pin::Namespace>]
103
+ namespace_pins = []
104
+
105
+ rspec_walker = SpecWalker.new(source_map: source_map, config: config)
106
+
107
+ Correctors::ContextBlockNamespaceCorrector.new(
108
+ namespace_pins: namespace_pins,
109
+ rspec_walker: rspec_walker
110
+ ).correct(source_map) do |pins_to_add|
111
+ pins += pins_to_add
112
+ end
113
+
114
+ Correctors::ExampleAndHookBlocksBindingCorrector.new(
115
+ namespace_pins: namespace_pins,
116
+ rspec_walker: rspec_walker
117
+ ).correct(source_map) do |pins_to_add|
118
+ pins += pins_to_add
119
+ end
120
+
121
+ # @type [Pin::Method, nil]
122
+ described_class_pin = nil
123
+ Correctors::DescribedClassCorrector.new(namespace_pins: namespace_pins, rspec_walker: rspec_walker).correct(
124
+ source_map
125
+ ) do |pins_to_add|
126
+ described_class_pin = pins_to_add.first
127
+ pins += pins_to_add
128
+ end
129
+
130
+ Correctors::LetMethodsCorrector.new(namespace_pins: namespace_pins, rspec_walker: rspec_walker).correct(
131
+ source_map
132
+ ) do |pins_to_add|
133
+ pins += pins_to_add
134
+ end
135
+
136
+ # @type [Pin::Method, nil]
137
+ subject_pin = nil
138
+ Correctors::SubjectMethodCorrector.new(namespace_pins: namespace_pins, rspec_walker: rspec_walker).correct(
139
+ source_map
140
+ ) do |pins_to_add|
141
+ subject_pin = pins_to_add.first
142
+ pins += pins_to_add
143
+ end
144
+
145
+ Correctors::ContextBlockMethodsCorrector.new(
146
+ namespace_pins: namespace_pins,
147
+ rspec_walker: rspec_walker
148
+ ).correct(source_map) do |pins_to_add|
149
+ pins += pins_to_add
150
+ end
151
+
152
+ Correctors::DslMethodsCorrector.new(
153
+ namespace_pins: namespace_pins,
154
+ rspec_walker: rspec_walker,
155
+ config: config
156
+ ).correct(
157
+ source_map
158
+ ) do |pins_to_add|
159
+ pins += pins_to_add
160
+ end
161
+
162
+ rspec_walker.walk!
163
+ pins += namespace_pins
164
+
165
+ # Implicit subject
166
+ if !subject_pin && described_class_pin
167
+ Correctors::ImplicitSubjectMethodCorrector.new(
168
+ namespace_pins: namespace_pins,
169
+ described_class_pin: described_class_pin
170
+ ).correct(
171
+ source_map
172
+ ) do |pins_to_add|
173
+ subject_pin = pins_to_add.first
174
+ pins += pins_to_add
175
+ end
176
+ end
177
+
178
+ if pins.any?
179
+ Solargraph.logger.debug(
180
+ "[RSpec] added #{pins.map(&:inspect)} to #{source_map.filename}"
181
+ )
182
+ end
183
+
184
+ Environ.new(requires: ['rspec'], pins: pins)
185
+ rescue StandardError => e
186
+ raise e if ENV['SOLARGRAPH_DEBUG']
187
+
188
+ Solargraph.logger.warn(
189
+ "[RSpec] Error processing #{source_map.filename}: #{e.message}\n#{e.backtrace.join("\n")}"
190
+ )
191
+ EMPTY_ENVIRON
192
+ end
193
+
194
+ private
195
+
196
+ # @param helper_modules [Array<String>]
197
+ # @param source_map [SourceMap]
198
+ # @return [Array<Pin::Base>]
199
+ def include_helper_pins(helper_modules: HELPER_MODULES)
200
+ helper_modules.map do |helper_module|
201
+ Util.build_module_include(
202
+ root_example_group_namespace_pin,
203
+ helper_module,
204
+ root_example_group_namespace_pin.location
205
+ )
206
+ end
207
+ end
208
+
209
+ # @return [Config]
210
+ def config
211
+ self.class.config
212
+ end
213
+
214
+ # @return [Pin::Namespace]
215
+ def root_example_group_namespace_pin
216
+ Solargraph::Pin::Namespace.new(
217
+ name: ROOT_NAMESPACE,
218
+ location: Util.dummy_location('lib/rspec/core/example_group.rb')
219
+ )
220
+ end
221
+ end
222
+ end
223
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Solargraph
4
+ module Rspec
5
+ module Correctors
6
+ # A corrector of RSpec parsed pins by Solargraph
7
+ # @abstract
8
+ class Base
9
+ # @return [Array<Solargraph::Pin::Namespace>]
10
+ attr_reader :namespace_pins
11
+
12
+ # @param namespace_pins [Array<Solargraph::Pin::Base>]
13
+ def initialize(namespace_pins:)
14
+ @namespace_pins = namespace_pins
15
+ end
16
+
17
+ # @param _source_map [Solargraph::SourceMap]
18
+ # @yield [Array<Solargraph::Pin::Base>] Pins to be added to the source map
19
+ # @return [void]
20
+ def correct(_source_map)
21
+ raise NotImplementedError
22
+ end
23
+
24
+ private
25
+
26
+ # @return [Solargraph::Pin::Namespace]
27
+ def root_example_group_namespace_pin
28
+ Solargraph::Pin::Namespace.new(
29
+ name: ROOT_NAMESPACE,
30
+ location: Util.dummy_location('lib/rspec/core/example_group.rb')
31
+ )
32
+ end
33
+
34
+ # @param namespace_pins [Array<Pin::Namespace>]
35
+ # @param line [Integer]
36
+ # @return [Pin::Namespace, nil]
37
+ def closest_namespace_pin(namespace_pins, line)
38
+ namespace_pins.min_by do |namespace_pin|
39
+ distance = line - namespace_pin.location.range.start.line
40
+ distance >= 0 ? distance : Float::INFINITY
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base'
4
+
5
+ module Solargraph
6
+ module Rspec
7
+ module Correctors
8
+ # A corrector that corrects pure ruby method blocks namespace defined inside describe/context blocks.
9
+ class ContextBlockMethodsCorrector < WalkerBase
10
+ # @param source_map [Solargraph::SourceMap]
11
+ def correct(source_map)
12
+ rspec_walker.after_walk do
13
+ source_map.pins.each_with_index do |pin, index|
14
+ next unless pin.is_a?(Solargraph::Pin::Method)
15
+
16
+ namespace_pin = closest_namespace_pin(namespace_pins, pin.location.range.start.line)
17
+ next unless namespace_pin
18
+
19
+ source_map.pins[index] = Solargraph::Pin::Method.new(
20
+ visibility: pin.visibility,
21
+ parameters: pin.parameters,
22
+ closure: namespace_pin,
23
+ node: pin.node,
24
+ signatures: pin.signatures,
25
+ location: pin.location,
26
+ name: pin.name,
27
+ scope: pin.scope,
28
+ comments: pin.comments
29
+ )
30
+ end
31
+
32
+ yield [] if block_given?
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end