solargraph-rspec 0.1.1 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Credits: This file is a copy of the original file from the solargraph-rails gem.
4
-
5
3
  module Solargraph
6
4
  module Rspec
7
5
  class Walker
6
+ class ParsingError < StandardError; end
7
+
8
8
  class Hook
9
9
  attr_reader :node_type
10
10
 
@@ -17,7 +17,7 @@ module Solargraph
17
17
  @proc = Proc.new(&block)
18
18
  end
19
19
 
20
- # @param node [Parser::AST::Node]
20
+ # @param node [RubyVM::AbstractSyntaxTree::Node]
21
21
  # @return [void]
22
22
  def visit(node)
23
23
  return unless matches?(node)
@@ -33,15 +33,15 @@ module Solargraph
33
33
 
34
34
  private
35
35
 
36
- # @param node [Parser::AST::Node]
36
+ # @param node [RubyVM::AbstractSyntaxTree::Node]
37
37
  # @return [Boolean]
38
38
  def matches?(node)
39
39
  return false unless node.type == node_type
40
40
  return false unless node.children
41
41
  return true if @args.empty?
42
42
 
43
- a_child_matches = node.children.first.is_a?(::Parser::AST::Node) && node.children.any? do |child|
44
- child.is_a?(::Parser::AST::Node) &&
43
+ a_child_matches = node.children.first.is_a?(RubyVM::AbstractSyntaxTree::Node) && node.children.any? do |child|
44
+ child.is_a?(RubyVM::AbstractSyntaxTree::Node) &&
45
45
  match_children(child.children, @args[1..])
46
46
  end
47
47
 
@@ -50,12 +50,12 @@ module Solargraph
50
50
  match_children(node.children)
51
51
  end
52
52
 
53
- # @param children [Array<Parser::AST::Node>]
53
+ # @param children [Array<RubyVM::AbstractSyntaxTree::Node>]
54
54
  def match_children(children, args = @args)
55
55
  args.each_with_index.all? do |arg, i|
56
56
  if arg == :any
57
57
  true
58
- elsif children[i].is_a?(::Parser::AST::Node) && arg.is_a?(Symbol)
58
+ elsif children[i].is_a?(RubyVM::AbstractSyntaxTree::Node) && arg.is_a?(Symbol)
59
59
  children[i].type == arg
60
60
  else
61
61
  children[i] == arg
@@ -64,28 +64,9 @@ module Solargraph
64
64
  end
65
65
  end
66
66
 
67
- # https://github.com/castwide/solargraph/issues/522
68
- def self.normalize_ast(source)
69
- ast = source.node
70
-
71
- if ast.is_a?(::Parser::AST::Node)
72
- ast
73
- else
74
- NodeParser.parse_with_comments(source.code, source.filename)
75
- end
76
- end
77
-
78
- # @param source [Solargraph::Source]
79
- def self.from_source(source)
80
- new(*normalize_ast(source))
81
- end
82
-
83
- # @return ast [Parser::AST::Node]
84
- attr_reader :ast
85
- # @return comments [Hash]
86
- attr_reader :comments
67
+ attr_reader :ast, :comments
87
68
 
88
- # @param ast [Parser::AST::Node]
69
+ # @param ast [RubyVM::AbstractSyntaxTree::Node]
89
70
  # @param comments [Hash]
90
71
  def initialize(ast, comments = {})
91
72
  @comments = comments
@@ -93,23 +74,18 @@ module Solargraph
93
74
  @hooks = Hash.new([])
94
75
  end
95
76
 
96
- # @param node_type [Symbol]
97
- # @param args [Array]
98
- # @param block [Proc]
99
77
  def on(node_type, args = [], &block)
100
78
  @hooks[node_type] << Hook.new(node_type, args, &block)
101
79
  end
102
80
 
103
- # @return [void]
104
81
  def walk
105
82
  @ast.is_a?(Array) ? @ast.each { |node| traverse(node) } : traverse(@ast)
106
83
  end
107
84
 
108
85
  private
109
86
 
110
- # @param node [Parser::AST::Node]
111
87
  def traverse(node)
112
- return unless node.is_a?(::Parser::AST::Node)
88
+ return unless node.is_a?(RubyVM::AbstractSyntaxTree::Node)
113
89
 
114
90
  @hooks[node.type].each { |hook| hook.visit(node) }
115
91
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solargraph-rspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lekë Mula
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-05-13 00:00:00.000000000 Z
11
+ date: 2024-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: solargraph
@@ -45,6 +45,7 @@ files:
45
45
  - LICENSE.txt
46
46
  - README.md
47
47
  - Rakefile
48
+ - codecov.yml
48
49
  - lib/solargraph-rspec.rb
49
50
  - lib/solargraph/rspec/config.rb
50
51
  - lib/solargraph/rspec/convention.rb
@@ -54,12 +55,14 @@ files:
54
55
  - lib/solargraph/rspec/correctors/described_class_corrector.rb
55
56
  - lib/solargraph/rspec/correctors/dsl_methods_corrector.rb
56
57
  - lib/solargraph/rspec/correctors/example_and_hook_blocks_binding_corrector.rb
57
- - lib/solargraph/rspec/correctors/implicit_subject_method_corrector.rb
58
58
  - lib/solargraph/rspec/correctors/let_methods_corrector.rb
59
59
  - lib/solargraph/rspec/correctors/subject_method_corrector.rb
60
- - lib/solargraph/rspec/correctors/walker_base.rb
60
+ - lib/solargraph/rspec/pin_factory.rb
61
61
  - lib/solargraph/rspec/spec_walker.rb
62
- - lib/solargraph/rspec/util.rb
62
+ - lib/solargraph/rspec/spec_walker/fake_let_method.rb
63
+ - lib/solargraph/rspec/spec_walker/full_constant_name.rb
64
+ - lib/solargraph/rspec/spec_walker/node_types.rb
65
+ - lib/solargraph/rspec/spec_walker/rspec_context_namespace.rb
63
66
  - lib/solargraph/rspec/version.rb
64
67
  - lib/solargraph/rspec/walker.rb
65
68
  - lib/solargraph_rspec.rb
@@ -1,48 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'let_methods_corrector'
4
-
5
- module Solargraph
6
- module Rspec
7
- module Correctors
8
- # Defines let-like methods in the example group block
9
- class ImplicitSubjectMethodCorrector < Base
10
- # @return [Pin::Method]
11
- attr_reader :described_class_pin
12
-
13
- # @param namespace_pins [Array<Pin::Namespace>]
14
- # @param described_class_pin [Pin::Method]
15
- def initialize(namespace_pins:, described_class_pin:)
16
- super(namespace_pins: namespace_pins)
17
-
18
- @described_class_pin = described_class_pin
19
- end
20
-
21
- # @param source_map [Solargraph::SourceMap]
22
- # @return [void]
23
- def correct(_source_map)
24
- namespace_pin = closest_namespace_pin(namespace_pins, described_class_pin.location.range.start.line)
25
-
26
- yield [implicit_subject_pin(described_class_pin, namespace_pin)] if block_given? && namespace_pin
27
- end
28
-
29
- private
30
-
31
- # @param described_class_pin [Pin::Method]
32
- # @param namespace_pin [Pin::Namespace]
33
- # @return [Pin::Method]
34
- def implicit_subject_pin(described_class_pin, namespace_pin)
35
- described_class = described_class_pin.return_type.first.subtypes.first.name
36
-
37
- Util.build_public_method(
38
- namespace_pin,
39
- 'subject',
40
- types: [described_class],
41
- location: described_class_pin.location,
42
- scope: :instance
43
- )
44
- end
45
- end
46
- end
47
- end
48
- end
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'base'
4
-
5
- # A corrector that walks through RSpec AST nodes and corrects them
6
- module Solargraph
7
- module Rspec
8
- module Correctors
9
- # A corrector of RSpec parsed pins by Solargraph
10
- # @abstract
11
- class WalkerBase < Base
12
- # @return [Array<Solargraph::Pin::Namespace>]
13
- attr_reader :namespace_pins
14
-
15
- # @return [Solargraph::Rspec::SpecWalker]
16
- attr_reader :rspec_walker
17
-
18
- # @param namespace_pins [Array<Solargraph::Pin::Base>]
19
- # @param rspec_walker [Solargraph::Rspec::SpecWalker]
20
- def initialize(namespace_pins:, rspec_walker:)
21
- super(namespace_pins: namespace_pins)
22
- @rspec_walker = rspec_walker
23
- end
24
- end
25
- end
26
- end
27
- end
@@ -1,77 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Credits: This file is a copy of the file from the solargraph-rspec gem
4
-
5
- # rubocop:disable Naming/MethodParameterName
6
- module Solargraph
7
- module Rspec
8
- # Utility methods for building pins and references.
9
- module Util
10
- def self.build_public_method(
11
- ns,
12
- name,
13
- types: nil,
14
- location: nil,
15
- comments: [],
16
- attribute: false,
17
- scope: :instance
18
- )
19
- opts = {
20
- name: name,
21
- location: location,
22
- closure: ns,
23
- scope: scope,
24
- attribute: attribute,
25
- comments: []
26
- }
27
-
28
- comments << "@return [#{types.join(",")}]" if types
29
-
30
- opts[:comments] = comments.join("\n")
31
-
32
- Solargraph::Pin::Method.new(**opts)
33
- end
34
-
35
- def self.build_module_include(ns, module_name, location)
36
- Solargraph::Pin::Reference::Include.new(
37
- closure: ns,
38
- name: module_name,
39
- location: location
40
- )
41
- end
42
-
43
- def self.build_module_extend(ns, module_name, location)
44
- Solargraph::Pin::Reference::Extend.new(
45
- closure: ns,
46
- name: module_name,
47
- location: location
48
- )
49
- end
50
-
51
- def self.dummy_location(path)
52
- Solargraph::Location.new(
53
- File.expand_path(path),
54
- Solargraph::Range.from_to(0, 0, 0, 0)
55
- )
56
- end
57
-
58
- # @param ast [Parser::AST::Node]
59
- def self.build_location(ast, path)
60
- Solargraph::Location.new(
61
- File.expand_path(path),
62
- Solargraph::Range.from_to(
63
- ast.location.first_line,
64
- ast.location.column,
65
- ast.location.last_line,
66
- ast.location.last_column
67
- )
68
- )
69
- end
70
-
71
- def self.method_return(path, type)
72
- Solargraph::Pin::Reference::Override.method_return(path, type)
73
- end
74
- end
75
- end
76
- end
77
- # rubocop:enable Naming/MethodParameterName