solargraph 0.38.6 → 0.39.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/SPONSORS.md +9 -0
- data/lib/solargraph.rb +1 -0
- data/lib/solargraph/api_map.rb +24 -13
- data/lib/solargraph/api_map/bundler_methods.rb +3 -0
- data/lib/solargraph/api_map/source_to_yard.rb +1 -6
- data/lib/solargraph/api_map/store.rb +19 -3
- data/lib/solargraph/complex_type/type_methods.rb +4 -4
- data/lib/solargraph/convention/base.rb +0 -5
- data/lib/solargraph/core_fills.rb +28 -2
- data/lib/solargraph/diagnostics/base.rb +1 -0
- data/lib/solargraph/diagnostics/type_check.rb +13 -13
- data/lib/solargraph/language_server/host.rb +11 -2
- data/lib/solargraph/language_server/host/sources.rb +20 -11
- data/lib/solargraph/language_server/message/text_document/formatting.rb +16 -11
- data/lib/solargraph/language_server/transport/data_reader.rb +2 -0
- data/lib/solargraph/library.rb +3 -3
- data/lib/solargraph/parser.rb +26 -0
- data/lib/solargraph/parser/comment_ripper.rb +52 -0
- data/lib/solargraph/parser/legacy.rb +12 -0
- data/lib/solargraph/parser/legacy/class_methods.rb +105 -0
- data/lib/solargraph/parser/legacy/flawed_builder.rb +16 -0
- data/lib/solargraph/parser/legacy/node_chainer.rb +115 -0
- data/lib/solargraph/parser/legacy/node_methods.rb +289 -0
- data/lib/solargraph/parser/legacy/node_processors.rb +54 -0
- data/lib/solargraph/parser/legacy/node_processors/alias_node.rb +23 -0
- data/lib/solargraph/parser/legacy/node_processors/args_node.rb +35 -0
- data/lib/solargraph/parser/legacy/node_processors/begin_node.rb +15 -0
- data/lib/solargraph/parser/legacy/node_processors/block_node.rb +22 -0
- data/lib/solargraph/parser/legacy/node_processors/casgn_node.rb +25 -0
- data/lib/solargraph/parser/legacy/node_processors/cvasgn_node.rb +23 -0
- data/lib/solargraph/parser/legacy/node_processors/def_node.rb +63 -0
- data/lib/solargraph/parser/legacy/node_processors/defs_node.rb +36 -0
- data/lib/solargraph/parser/legacy/node_processors/gvasgn_node.rb +23 -0
- data/lib/solargraph/parser/legacy/node_processors/ivasgn_node.rb +38 -0
- data/lib/solargraph/parser/legacy/node_processors/lvasgn_node.rb +28 -0
- data/lib/solargraph/parser/legacy/node_processors/namespace_node.rb +39 -0
- data/lib/solargraph/parser/legacy/node_processors/orasgn_node.rb +16 -0
- data/lib/solargraph/parser/legacy/node_processors/resbody_node.rb +36 -0
- data/lib/solargraph/parser/legacy/node_processors/sclass_node.rb +21 -0
- data/lib/solargraph/parser/legacy/node_processors/send_node.rb +232 -0
- data/lib/solargraph/parser/legacy/node_processors/sym_node.rb +18 -0
- data/lib/solargraph/parser/node_methods.rb +43 -0
- data/lib/solargraph/parser/node_processor.rb +43 -0
- data/lib/solargraph/parser/node_processor/base.rb +77 -0
- data/lib/solargraph/{source_map → parser}/region.rb +11 -6
- data/lib/solargraph/parser/rubyvm.rb +40 -0
- data/lib/solargraph/parser/rubyvm/class_methods.rb +146 -0
- data/lib/solargraph/parser/rubyvm/node_chainer.rb +133 -0
- data/lib/solargraph/parser/rubyvm/node_methods.rb +279 -0
- data/lib/solargraph/parser/rubyvm/node_processors.rb +60 -0
- data/lib/solargraph/parser/rubyvm/node_processors/alias_node.rb +23 -0
- data/lib/solargraph/parser/rubyvm/node_processors/args_node.rb +62 -0
- data/lib/solargraph/parser/rubyvm/node_processors/begin_node.rb +15 -0
- data/lib/solargraph/parser/rubyvm/node_processors/block_node.rb +22 -0
- data/lib/solargraph/parser/rubyvm/node_processors/casgn_node.rb +22 -0
- data/lib/solargraph/parser/rubyvm/node_processors/cvasgn_node.rb +23 -0
- data/lib/solargraph/parser/rubyvm/node_processors/def_node.rb +64 -0
- data/lib/solargraph/parser/rubyvm/node_processors/defs_node.rb +57 -0
- data/lib/solargraph/parser/rubyvm/node_processors/gvasgn_node.rb +23 -0
- data/lib/solargraph/parser/rubyvm/node_processors/ivasgn_node.rb +38 -0
- data/lib/solargraph/parser/rubyvm/node_processors/kw_arg_node.rb +34 -0
- data/lib/solargraph/parser/rubyvm/node_processors/lit_node.rb +20 -0
- data/lib/solargraph/parser/rubyvm/node_processors/lvasgn_node.rb +27 -0
- data/lib/solargraph/parser/rubyvm/node_processors/namespace_node.rb +39 -0
- data/lib/solargraph/parser/rubyvm/node_processors/opt_arg_node.rb +26 -0
- data/lib/solargraph/parser/rubyvm/node_processors/orasgn_node.rb +15 -0
- data/lib/solargraph/parser/rubyvm/node_processors/resbody_node.rb +45 -0
- data/lib/solargraph/parser/rubyvm/node_processors/sclass_node.rb +21 -0
- data/lib/solargraph/parser/rubyvm/node_processors/scope_node.rb +15 -0
- data/lib/solargraph/parser/rubyvm/node_processors/send_node.rb +292 -0
- data/lib/solargraph/parser/rubyvm/node_processors/sym_node.rb +18 -0
- data/lib/solargraph/parser/snippet.rb +13 -0
- data/lib/solargraph/pin/attribute.rb +8 -0
- data/lib/solargraph/pin/base.rb +1 -1
- data/lib/solargraph/pin/base_method.rb +26 -4
- data/lib/solargraph/pin/base_variable.rb +7 -8
- data/lib/solargraph/pin/block.rb +1 -1
- data/lib/solargraph/pin/constant.rb +16 -0
- data/lib/solargraph/pin/conversions.rb +2 -2
- data/lib/solargraph/pin/method.rb +26 -14
- data/lib/solargraph/pin/parameter.rb +56 -2
- data/lib/solargraph/pin/reference.rb +1 -0
- data/lib/solargraph/pin/reference/override.rb +2 -0
- data/lib/solargraph/pin/reference/prepend.rb +10 -0
- data/lib/solargraph/pin/yard_pin/constant.rb +2 -4
- data/lib/solargraph/pin/yard_pin/method.rb +34 -18
- data/lib/solargraph/pin/yard_pin/namespace.rb +3 -11
- data/lib/solargraph/pin/yard_pin/yard_mixin.rb +3 -12
- data/lib/solargraph/range.rb +8 -2
- data/lib/solargraph/shell.rb +17 -11
- data/lib/solargraph/source.rb +114 -135
- data/lib/solargraph/source/chain.rb +11 -3
- data/lib/solargraph/source/chain/call.rb +1 -2
- data/lib/solargraph/source/chain/constant.rb +44 -8
- data/lib/solargraph/source/chain/link.rb +1 -0
- data/lib/solargraph/source/cursor.rb +2 -28
- data/lib/solargraph/source/source_chainer.rb +12 -6
- data/lib/solargraph/source/updater.rb +2 -0
- data/lib/solargraph/source_map.rb +0 -2
- data/lib/solargraph/source_map/clip.rb +24 -17
- data/lib/solargraph/source_map/mapper.rb +34 -29
- data/lib/solargraph/type_checker.rb +367 -275
- data/lib/solargraph/type_checker/checks.rb +95 -0
- data/lib/solargraph/type_checker/param_def.rb +2 -17
- data/lib/solargraph/type_checker/rules.rb +53 -0
- data/lib/solargraph/version.rb +1 -1
- data/lib/solargraph/views/environment.erb +5 -3
- data/lib/solargraph/workspace.rb +17 -2
- data/lib/solargraph/workspace/config.rb +15 -7
- data/lib/solargraph/yard_map.rb +89 -49
- data/lib/solargraph/yard_map/core_docs.rb +1 -1
- data/solargraph.gemspec +2 -1
- metadata +88 -32
- data/OVERVIEW.md +0 -37
- data/lib/solargraph/source/flawed_builder.rb +0 -15
- data/lib/solargraph/source/node_chainer.rb +0 -111
- data/lib/solargraph/source/node_methods.rb +0 -240
- data/lib/solargraph/source_map/node_processor.rb +0 -85
- data/lib/solargraph/source_map/node_processor/alias_node.rb +0 -21
- data/lib/solargraph/source_map/node_processor/args_node.rb +0 -24
- data/lib/solargraph/source_map/node_processor/base.rb +0 -103
- data/lib/solargraph/source_map/node_processor/begin_node.rb +0 -13
- data/lib/solargraph/source_map/node_processor/block_node.rb +0 -21
- data/lib/solargraph/source_map/node_processor/casgn_node.rb +0 -21
- data/lib/solargraph/source_map/node_processor/cvasgn_node.rb +0 -21
- data/lib/solargraph/source_map/node_processor/def_node.rb +0 -62
- data/lib/solargraph/source_map/node_processor/defs_node.rb +0 -33
- data/lib/solargraph/source_map/node_processor/gvasgn_node.rb +0 -21
- data/lib/solargraph/source_map/node_processor/ivasgn_node.rb +0 -34
- data/lib/solargraph/source_map/node_processor/lvasgn_node.rb +0 -24
- data/lib/solargraph/source_map/node_processor/namespace_node.rb +0 -35
- data/lib/solargraph/source_map/node_processor/orasgn_node.rb +0 -14
- data/lib/solargraph/source_map/node_processor/resbody_node.rb +0 -32
- data/lib/solargraph/source_map/node_processor/sclass_node.rb +0 -19
- data/lib/solargraph/source_map/node_processor/send_node.rb +0 -217
- data/lib/solargraph/source_map/node_processor/sym_node.rb +0 -16
@@ -1,9 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Solargraph
|
4
|
-
|
5
|
-
# Data used by the
|
6
|
-
#
|
4
|
+
module Parser
|
5
|
+
# Data used by the parser to track context at various locations in a
|
6
|
+
# source.
|
7
7
|
#
|
8
8
|
class Region
|
9
9
|
# @return [Pin::Closure]
|
@@ -18,17 +18,21 @@ module Solargraph
|
|
18
18
|
# @return [Solargraph::Source]
|
19
19
|
attr_reader :source
|
20
20
|
|
21
|
+
# @return [Array<Symbol>]
|
22
|
+
attr_reader :lvars
|
23
|
+
|
21
24
|
# @param source [Source]
|
22
25
|
# @param namespace [String]
|
23
26
|
# @param scope [Symbol]
|
24
27
|
# @param visibility [Symbol]
|
25
28
|
def initialize source: Solargraph::Source.load_string(''), closure: nil,
|
26
|
-
scope: nil, visibility: :public
|
29
|
+
scope: nil, visibility: :public, lvars: []
|
27
30
|
@source = source
|
28
31
|
# @closure = closure
|
29
32
|
@closure = closure || Pin::Namespace.new(name: '', location: source.location)
|
30
33
|
@scope = scope
|
31
34
|
@visibility = visibility
|
35
|
+
@lvars = lvars
|
32
36
|
end
|
33
37
|
|
34
38
|
# @return [String]
|
@@ -42,12 +46,13 @@ module Solargraph
|
|
42
46
|
# @param scope [Symbol, nil]
|
43
47
|
# @param visibility [Symbol, nil]
|
44
48
|
# @return [Region]
|
45
|
-
def update closure: nil, scope: nil, visibility: nil
|
49
|
+
def update closure: nil, scope: nil, visibility: nil, lvars: nil
|
46
50
|
Region.new(
|
47
51
|
source: source,
|
48
52
|
closure: closure || self.closure,
|
49
53
|
scope: scope || self.scope,
|
50
|
-
visibility: visibility || self.visibility
|
54
|
+
visibility: visibility || self.visibility,
|
55
|
+
lvars: lvars || self.lvars
|
51
56
|
)
|
52
57
|
end
|
53
58
|
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Solargraph
|
2
|
+
module Parser
|
3
|
+
module Rubyvm
|
4
|
+
autoload :ClassMethods, 'solargraph/parser/rubyvm/class_methods'
|
5
|
+
autoload :NodeChainer, 'solargraph/parser/rubyvm/node_chainer'
|
6
|
+
autoload :NodeMethods, 'solargraph/parser/rubyvm/node_methods'
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
require 'solargraph/parser/rubyvm/node_processors'
|
12
|
+
|
13
|
+
class RubyVM::AbstractSyntaxTree::Node
|
14
|
+
def to_sexp
|
15
|
+
sexp self
|
16
|
+
end
|
17
|
+
|
18
|
+
def == other
|
19
|
+
return false unless other.is_a?(self.class)
|
20
|
+
here = Solargraph::Range.from_node(self)
|
21
|
+
there = Solargraph::Range.from_node(other)
|
22
|
+
here == there && to_sexp == other.to_sexp
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def sexp node, depth = 0
|
28
|
+
result = ''
|
29
|
+
if node.is_a?(RubyVM::AbstractSyntaxTree::Node)
|
30
|
+
result += "#{' ' * depth}(:#{node.type}"
|
31
|
+
node.children.each do |child|
|
32
|
+
result += "\n" + sexp(child, depth + 1)
|
33
|
+
end
|
34
|
+
result += ")"
|
35
|
+
else
|
36
|
+
result += "#{' ' * depth}#{node.inspect}"
|
37
|
+
end
|
38
|
+
result
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,146 @@
|
|
1
|
+
require 'solargraph/parser/rubyvm/node_processors'
|
2
|
+
|
3
|
+
module Solargraph
|
4
|
+
module Parser
|
5
|
+
module Rubyvm
|
6
|
+
module ClassMethods
|
7
|
+
# @param code [String]
|
8
|
+
# @param filename [String]
|
9
|
+
# @return [Array(Parser::AST::Node, Array<Parser::Source::Comment>)]
|
10
|
+
def parse_with_comments code, filename = nil
|
11
|
+
node = RubyVM::AbstractSyntaxTree.parse(code)
|
12
|
+
comments = CommentRipper.new(code).parse
|
13
|
+
[node, comments]
|
14
|
+
rescue ::SyntaxError => e
|
15
|
+
raise Parser::SyntaxError, e.message
|
16
|
+
end
|
17
|
+
|
18
|
+
# @param code [String]
|
19
|
+
# @param filename [String, nil]
|
20
|
+
# @param line [Integer]
|
21
|
+
# @return [Parser::AST::Node]
|
22
|
+
def parse code, filename = nil, line = 0
|
23
|
+
RubyVM::AbstractSyntaxTree.parse(code)
|
24
|
+
rescue ::SyntaxError => e
|
25
|
+
raise Parser::SyntaxError, e.message
|
26
|
+
end
|
27
|
+
|
28
|
+
def map source
|
29
|
+
NodeProcessor.process(source.node, Region.new(source: source))
|
30
|
+
end
|
31
|
+
|
32
|
+
# def returns_from node
|
33
|
+
# return [] unless Parser.is_ast_node?(node)
|
34
|
+
# if node.type == :SCOPE
|
35
|
+
# # node.children.select { |n| n.is_a?(RubyVM::AbstractSyntaxTree::Node) }.map { |n| DeepInference.get_return_nodes(n) }.flatten
|
36
|
+
# DeepInference.get_return_nodes(node.children[2])
|
37
|
+
# else
|
38
|
+
# DeepInference.get_return_nodes(node)
|
39
|
+
# end
|
40
|
+
# end
|
41
|
+
|
42
|
+
def references source, name
|
43
|
+
inner_node_references(name, source.node).map do |n|
|
44
|
+
rng = Range.from_node(n)
|
45
|
+
offset = Position.to_offset(source.code, rng.start)
|
46
|
+
soff = source.code.index(name, offset)
|
47
|
+
eoff = soff + name.length
|
48
|
+
Location.new(
|
49
|
+
source.filename,
|
50
|
+
Range.new(
|
51
|
+
Position.from_offset(source.code, soff),
|
52
|
+
Position.from_offset(source.code, eoff)
|
53
|
+
)
|
54
|
+
)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# @param name [String]
|
59
|
+
# @param top [AST::Node]
|
60
|
+
# @return [Array<AST::Node>]
|
61
|
+
def inner_node_references name, top
|
62
|
+
result = []
|
63
|
+
if Parser.rubyvm?
|
64
|
+
if Parser.is_ast_node?(top)
|
65
|
+
result.push top if match_rubyvm_node_to_ref(top, name)
|
66
|
+
top.children.each { |c| result.concat inner_node_references(name, c) }
|
67
|
+
end
|
68
|
+
else
|
69
|
+
if Parser.is_ast_node?(top) && top.to_s.include?(":#{name}")
|
70
|
+
result.push top if top.children.any? { |c| c.to_s == name }
|
71
|
+
top.children.each { |c| result.concat inner_node_references(name, c) }
|
72
|
+
end
|
73
|
+
end
|
74
|
+
result
|
75
|
+
end
|
76
|
+
|
77
|
+
def match_rubyvm_node_to_ref(top, name)
|
78
|
+
top.children.select { |c| c.is_a?(Symbol) }.any? { |c| c.to_s == name } ||
|
79
|
+
top.children.select { |c| c.is_a?(Array) }.any? { |c| c.include?(name.to_sym) }
|
80
|
+
end
|
81
|
+
|
82
|
+
def chain *args
|
83
|
+
NodeChainer.chain *args
|
84
|
+
end
|
85
|
+
|
86
|
+
def process_node *args
|
87
|
+
Solargraph::Parser::NodeProcessor.process *args
|
88
|
+
end
|
89
|
+
|
90
|
+
def infer_literal_node_type node
|
91
|
+
# NodeMethods.infer_literal_node_type node
|
92
|
+
end
|
93
|
+
|
94
|
+
def version
|
95
|
+
Ruby::VERSION
|
96
|
+
end
|
97
|
+
|
98
|
+
def is_ast_node? node
|
99
|
+
if Parser.rubyvm?
|
100
|
+
node.is_a?(RubyVM::AbstractSyntaxTree::Node)
|
101
|
+
else
|
102
|
+
node.is_a?(::Parser::AST::Node)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def node_range node
|
107
|
+
st = Position.new(node.first_lineno - 1, node.first_column)
|
108
|
+
en = Position.new(node.last_lineno - 1, node.last_column)
|
109
|
+
Range.new(st, en)
|
110
|
+
end
|
111
|
+
|
112
|
+
def recipient_node tree
|
113
|
+
tree.each_with_index do |node, idx|
|
114
|
+
return tree[idx + 1] if [:ARRAY, :ZARRAY, :LIST].include?(node.type) && tree[idx + 1] && [:FCALL, :VCALL, :CALL].include?(tree[idx + 1].type)
|
115
|
+
end
|
116
|
+
nil
|
117
|
+
end
|
118
|
+
|
119
|
+
def string_ranges node
|
120
|
+
return [] unless is_ast_node?(node)
|
121
|
+
result = []
|
122
|
+
if node.type == :STR
|
123
|
+
result.push Range.from_node(node)
|
124
|
+
elsif node.type == :DSTR
|
125
|
+
here = Range.from_node(node)
|
126
|
+
there = Range.from_node(node.children[1])
|
127
|
+
result.push Range.new(here.start, there.start)
|
128
|
+
end
|
129
|
+
node.children.each do |child|
|
130
|
+
result.concat string_ranges(child)
|
131
|
+
end
|
132
|
+
if node.type == :DSTR && node.children.last.nil?
|
133
|
+
# result.push Range.new(result.last.ending, result.last.ending)
|
134
|
+
last = node.children[-2]
|
135
|
+
unless last.nil?
|
136
|
+
rng = Range.from_node(last)
|
137
|
+
pos = Position.new(rng.ending.line, rng.ending.column - 1)
|
138
|
+
result.push Range.new(pos, pos)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
result
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Solargraph
|
4
|
+
module Parser
|
5
|
+
module Rubyvm
|
6
|
+
# A factory for generating chains from nodes.
|
7
|
+
#
|
8
|
+
class NodeChainer
|
9
|
+
include Rubyvm::NodeMethods
|
10
|
+
|
11
|
+
Chain = Source::Chain
|
12
|
+
|
13
|
+
# @param node [Parser::AST::Node]
|
14
|
+
# @param filename [String]
|
15
|
+
def initialize node, filename = nil, in_block = false
|
16
|
+
@node = node
|
17
|
+
@filename = filename
|
18
|
+
@in_block = in_block
|
19
|
+
end
|
20
|
+
|
21
|
+
# @return [Source::Chain]
|
22
|
+
def chain
|
23
|
+
links = generate_links(@node)
|
24
|
+
Chain.new(links, @node, (Parser.is_ast_node?(@node) && @node.type == :SPLAT))
|
25
|
+
end
|
26
|
+
|
27
|
+
class << self
|
28
|
+
# @param node [Parser::AST::Node]
|
29
|
+
# @param filename [String]
|
30
|
+
# @return [Source::Chain]
|
31
|
+
def chain node, filename = nil, in_block = false
|
32
|
+
NodeChainer.new(node, filename, in_block).chain
|
33
|
+
end
|
34
|
+
|
35
|
+
# @param code [String]
|
36
|
+
# @return [Source::Chain]
|
37
|
+
def load_string(code)
|
38
|
+
node = Parser.parse(code.sub(/\.$/, ''))
|
39
|
+
chain = NodeChainer.new(node).chain
|
40
|
+
chain.links.push(Chain::Link.new) if code.end_with?('.')
|
41
|
+
chain
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
# @param n [Parser::AST::Node]
|
48
|
+
# @return [Array<Chain::Link>]
|
49
|
+
def generate_links n
|
50
|
+
return [] unless Parser.is_ast_node?(n)
|
51
|
+
return generate_links(n.children[2]) if n.type == :SCOPE
|
52
|
+
return generate_links(n.children[0]) if n.type == :SPLAT
|
53
|
+
result = []
|
54
|
+
if n.type == :ITER
|
55
|
+
@in_block = true
|
56
|
+
result.concat generate_links(n.children[0])
|
57
|
+
@in_block = false
|
58
|
+
elsif n.type == :CALL || n.type == :OPCALL
|
59
|
+
n.children[0..-3].each do |c|
|
60
|
+
result.concat generate_links(c)
|
61
|
+
end
|
62
|
+
result.push Chain::Call.new(n.children[-2].to_s, node_to_argchains(n.children.last), @in_block || block_passed?(n))
|
63
|
+
elsif n.type == :ATTRASGN
|
64
|
+
result.concat generate_links(n.children[0])
|
65
|
+
result.push Chain::Call.new(n.children[1].to_s, node_to_argchains(n.children[2]), @in_block || block_passed?(n))
|
66
|
+
elsif n.type == :VCALL
|
67
|
+
result.push Chain::Call.new(n.children[0].to_s, [], @in_block || block_passed?(n))
|
68
|
+
elsif n.type == :FCALL
|
69
|
+
result.push Chain::Call.new(n.children[0].to_s, node_to_argchains(n.children[1]), @in_block || block_passed?(n))
|
70
|
+
elsif n.type == :SELF
|
71
|
+
result.push Chain::Head.new('self')
|
72
|
+
elsif [:SUPER, :ZSUPER].include?(n.type)
|
73
|
+
result.push Chain::Head.new('super')
|
74
|
+
elsif [:COLON2, :COLON3, :CONST].include?(n.type)
|
75
|
+
const = unpack_name(n)
|
76
|
+
result.push Chain::Constant.new(const)
|
77
|
+
elsif [:LVAR, :LASGN, :DVAR].include?(n.type)
|
78
|
+
result.push Chain::Call.new(n.children[0].to_s)
|
79
|
+
elsif [:IVAR, :IASGN].include?(n.type)
|
80
|
+
result.push Chain::InstanceVariable.new(n.children[0].to_s)
|
81
|
+
elsif [:CVAR, :CVASGN].include?(n.type)
|
82
|
+
result.push Chain::ClassVariable.new(n.children[0].to_s)
|
83
|
+
elsif [:GVAR, :GASGN].include?(n.type)
|
84
|
+
result.push Chain::GlobalVariable.new(n.children[0].to_s)
|
85
|
+
elsif n.type == :OP_ASGN_OR
|
86
|
+
result.concat generate_links n.children[2]
|
87
|
+
elsif [:class, :module, :def, :defs].include?(n.type)
|
88
|
+
# @todo Undefined or what?
|
89
|
+
result.push Chain::UNDEFINED_CALL
|
90
|
+
elsif n.type == :AND
|
91
|
+
result.concat generate_links(n.children.last)
|
92
|
+
elsif n.type == :OR
|
93
|
+
result.push Chain::Or.new([NodeChainer.chain(n.children[0], @filename), NodeChainer.chain(n.children[1], @filename)])
|
94
|
+
elsif n.type == :begin
|
95
|
+
result.concat generate_links(n.children[0])
|
96
|
+
elsif n.type == :BLOCK_PASS
|
97
|
+
result.push Chain::BlockVariable.new("&#{n.children[1].children[0].to_s}")
|
98
|
+
else
|
99
|
+
lit = infer_literal_node_type(n)
|
100
|
+
result.push (lit ? Chain::Literal.new(lit) : Chain::Link.new)
|
101
|
+
end
|
102
|
+
result
|
103
|
+
end
|
104
|
+
|
105
|
+
def block_passed? node
|
106
|
+
node.children.last.is_a?(RubyVM::AbstractSyntaxTree::Node) && node.children.last.type == :BLOCK_PASS
|
107
|
+
end
|
108
|
+
|
109
|
+
def node_to_argchains node
|
110
|
+
# @todo Process array, splat, argscat
|
111
|
+
return [] unless Parser.is_ast_node?(node)
|
112
|
+
if [:ZARRAY, :ARRAY, :LIST].include?(node.type)
|
113
|
+
node.children[0..-2].map { |c| NodeChainer.chain(c) }
|
114
|
+
elsif node.type == :SPLAT
|
115
|
+
[NodeChainer.chain(node)]
|
116
|
+
elsif node.type == :ARGSCAT
|
117
|
+
result = node.children[0].children[0..-2].map { |c| NodeChainer.chain(c) }
|
118
|
+
result.push NodeChainer.chain(node.children[1])
|
119
|
+
# @todo Smelly instance variable access
|
120
|
+
result.last.instance_variable_set(:@splat, true)
|
121
|
+
result
|
122
|
+
elsif node.type == :BLOCK_PASS
|
123
|
+
result = node_to_argchains(node.children[0])
|
124
|
+
result.push Chain.new([Chain::BlockVariable.new("&#{node.children[1].children[0].to_s}")])
|
125
|
+
result
|
126
|
+
else
|
127
|
+
[]
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
@@ -0,0 +1,279 @@
|
|
1
|
+
module Solargraph
|
2
|
+
module Parser
|
3
|
+
module Rubyvm
|
4
|
+
module NodeMethods
|
5
|
+
module_function
|
6
|
+
|
7
|
+
# @param node [RubyVM::AbstractSyntaxTree::Node]
|
8
|
+
# @return [String]
|
9
|
+
def unpack_name node
|
10
|
+
pack_name(node).join('::')
|
11
|
+
end
|
12
|
+
|
13
|
+
# @param node [RubyVM::AbstractSyntaxTree::Node]
|
14
|
+
# @return [Array<String>]
|
15
|
+
def pack_name(node)
|
16
|
+
parts = []
|
17
|
+
if node.is_a?(RubyVM::AbstractSyntaxTree::Node)
|
18
|
+
parts.push '' if node.type == :COLON3
|
19
|
+
node.children.each { |n|
|
20
|
+
if n.is_a?(RubyVM::AbstractSyntaxTree::Node)
|
21
|
+
parts += pack_name(n)
|
22
|
+
else
|
23
|
+
parts.push n unless n.nil?
|
24
|
+
end
|
25
|
+
}
|
26
|
+
end
|
27
|
+
parts
|
28
|
+
end
|
29
|
+
|
30
|
+
# @param node [RubyVM::AbstractSyntaxTree::Node]
|
31
|
+
# @return [String, nil]
|
32
|
+
def infer_literal_node_type node
|
33
|
+
return nil unless Parser.is_ast_node?(node)
|
34
|
+
case node.type
|
35
|
+
when :LIT, :STR
|
36
|
+
"::#{node.children.first.class.to_s}"
|
37
|
+
when :DSTR
|
38
|
+
"::String"
|
39
|
+
when :ARRAY, :ZARRAY, :LIST, :ZLIST
|
40
|
+
'::Array'
|
41
|
+
when :HASH
|
42
|
+
'::Hash'
|
43
|
+
when :DOT2, :DOT3
|
44
|
+
'::Range'
|
45
|
+
when :TRUE, :FALSE
|
46
|
+
'::Boolean'
|
47
|
+
when :SCOPE
|
48
|
+
infer_literal_node_type(node.children[2])
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def returns_from node
|
53
|
+
return [] unless Parser.is_ast_node?(node)
|
54
|
+
if node.type == :SCOPE
|
55
|
+
# node.children.select { |n| n.is_a?(RubyVM::AbstractSyntaxTree::Node) }.map { |n| DeepInference.get_return_nodes(n) }.flatten
|
56
|
+
DeepInference.get_return_nodes(node.children[2])
|
57
|
+
else
|
58
|
+
DeepInference.get_return_nodes(node)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def const_nodes_from node
|
63
|
+
return [] unless Parser.is_ast_node?(node)
|
64
|
+
result = []
|
65
|
+
if [:CONST, :COLON2, :COLON3].include?(node.type)
|
66
|
+
result.push node
|
67
|
+
else
|
68
|
+
node.children.each { |child| result.concat const_nodes_from(child) }
|
69
|
+
end
|
70
|
+
result
|
71
|
+
end
|
72
|
+
|
73
|
+
def call_nodes_from node
|
74
|
+
return [] unless Parser.is_ast_node?(node)
|
75
|
+
result = []
|
76
|
+
if node.type == :ITER
|
77
|
+
result.push node.children[0]
|
78
|
+
node.children[1..-1].each { |child| result.concat call_nodes_from(child) }
|
79
|
+
elsif [:CALL, :VCALL, :FCALL, :ATTRASGN, :OPCALL].include?(node.type)
|
80
|
+
result.push node
|
81
|
+
node.children.each { |child| result.concat call_nodes_from(child) }
|
82
|
+
else
|
83
|
+
node.children.each { |child| result.concat call_nodes_from(child) }
|
84
|
+
end
|
85
|
+
result
|
86
|
+
end
|
87
|
+
|
88
|
+
def convert_hash node
|
89
|
+
return {} unless node?(node) && node.type == :HASH && node?(node.children[0])
|
90
|
+
result = {}
|
91
|
+
index = 0
|
92
|
+
until index > node.children[0].children.length - 2
|
93
|
+
k = node.children[0].children[index]
|
94
|
+
v = node.children[0].children[index + 1]
|
95
|
+
result[k.children[0]] = Solargraph::Parser.chain(v)
|
96
|
+
index += 2
|
97
|
+
end
|
98
|
+
result
|
99
|
+
end
|
100
|
+
|
101
|
+
def node? node
|
102
|
+
node.is_a?(RubyVM::AbstractSyntaxTree::Node)
|
103
|
+
end
|
104
|
+
|
105
|
+
# @param cursor [Solargraph::Source::Cursor]
|
106
|
+
def find_recipient_node cursor
|
107
|
+
if cursor.source.synchronized?
|
108
|
+
NodeMethods.synchronized_find_recipient_node cursor
|
109
|
+
else
|
110
|
+
NodeMethods.unsynchronized_find_recipient_node cursor
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
class << self
|
115
|
+
protected
|
116
|
+
|
117
|
+
def synchronized_find_recipient_node cursor
|
118
|
+
source = cursor.source
|
119
|
+
position = cursor.position
|
120
|
+
offset = cursor.offset
|
121
|
+
tree = source.tree_at(position.line, position.column)
|
122
|
+
tree.shift while tree.first && [:FCALL, :VCALL, :CALL].include?(tree.first.type) && !source.code_for(tree.first).strip.end_with?(')')
|
123
|
+
tree.each do |node|
|
124
|
+
if [:FCALL, :VCALL, :CALL].include?(node.type)
|
125
|
+
args = node.children.find { |c| Parser.is_ast_node?(c) && [:ARRAY, :ZARRAY, :LIST].include?(c.type) }
|
126
|
+
if args
|
127
|
+
match = source.code[0..offset-1].match(/,[^\)]*\z/)
|
128
|
+
rng = Solargraph::Range.from_node(args)
|
129
|
+
if match
|
130
|
+
rng = Solargraph::Range.new(rng.start, position)
|
131
|
+
end
|
132
|
+
return node if rng.contain?(position)
|
133
|
+
elsif source.code[0..offset-1] =~ /\(\s*$/
|
134
|
+
break unless source.code_for(node).strip.end_with?(')')
|
135
|
+
return node
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
nil
|
140
|
+
end
|
141
|
+
|
142
|
+
def unsynchronized_find_recipient_node cursor
|
143
|
+
source = cursor.source
|
144
|
+
position = cursor.position
|
145
|
+
offset = cursor.offset
|
146
|
+
if source.code[0..offset-1] =~ /\([A-Zaz0-9_\s]*\z$/ #&& source.code[offset] == ')'
|
147
|
+
tree = source.tree_at(position.line, position.column - 1)
|
148
|
+
if tree.first && [:FCALL, :VCALL, :CALL].include?(tree.first.type)
|
149
|
+
return tree.first
|
150
|
+
else
|
151
|
+
return nil
|
152
|
+
end
|
153
|
+
else
|
154
|
+
match = source.code[0..offset-1].match(/[\(,][A-Zaz0-9_\s]*\z/)
|
155
|
+
if match
|
156
|
+
moved = Position.from_offset(source.code, offset - match[0].length)
|
157
|
+
tree = source.tree_at(moved.line, moved.column)
|
158
|
+
tree.shift if match[0].start_with?(',')
|
159
|
+
tree.shift while tree.first && ![:FCALL, :VCALL, :CALL].include?(tree.first.type)
|
160
|
+
if tree.first && [:FCALL, :VCALL, :CALL].include?(tree.first.type)
|
161
|
+
return tree.first
|
162
|
+
end
|
163
|
+
end
|
164
|
+
return nil
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
module DeepInference
|
170
|
+
class << self
|
171
|
+
CONDITIONAL = [:IF, :UNLESS]
|
172
|
+
REDUCEABLE = [:BLOCK]
|
173
|
+
SKIPPABLE = [:DEFN, :DEFS, :CLASS, :SCLASS, :MODULE]
|
174
|
+
|
175
|
+
# @param node [Parser::AST::Node]
|
176
|
+
# @return [Array<Parser::AST::Node>]
|
177
|
+
def get_return_nodes node
|
178
|
+
return [] unless node.is_a?(RubyVM::AbstractSyntaxTree::Node)
|
179
|
+
result = []
|
180
|
+
if REDUCEABLE.include?(node.type)
|
181
|
+
result.concat get_return_nodes_from_children(node)
|
182
|
+
elsif CONDITIONAL.include?(node.type)
|
183
|
+
result.concat reduce_to_value_nodes(node.children[1..-1])
|
184
|
+
elsif node.type == :RESCUE
|
185
|
+
result.concat reduce_to_value_nodes([node.children[0]])
|
186
|
+
result.concat reduce_to_value_nodes(node.children[1..-2])
|
187
|
+
elsif node.type == :OR
|
188
|
+
result.concat reduce_to_value_nodes(node.children)
|
189
|
+
elsif node.type == :RETURN
|
190
|
+
result.concat reduce_to_value_nodes([node.children[0]])
|
191
|
+
elsif node.type == :ITER
|
192
|
+
result.push node
|
193
|
+
result.concat get_return_nodes_only(node.children[1])
|
194
|
+
elsif node.type == :CASE
|
195
|
+
node.children.each do |cc|
|
196
|
+
result.concat reduce_to_value_nodes(cc.children[1..-1])
|
197
|
+
end
|
198
|
+
else
|
199
|
+
result.push node
|
200
|
+
end
|
201
|
+
result
|
202
|
+
end
|
203
|
+
|
204
|
+
private
|
205
|
+
|
206
|
+
def get_return_nodes_from_children parent
|
207
|
+
result = []
|
208
|
+
nodes = parent.children.select{|n| n.is_a?(RubyVM::AbstractSyntaxTree::Node)}
|
209
|
+
nodes.each_with_index do |node, idx|
|
210
|
+
if node.type == :BLOCK
|
211
|
+
result.concat get_return_nodes_only(node.children[2])
|
212
|
+
elsif SKIPPABLE.include?(node.type)
|
213
|
+
next
|
214
|
+
elsif CONDITIONAL.include?(node.type)
|
215
|
+
result.concat get_return_nodes_only(node)
|
216
|
+
elsif node.type == :RETURN
|
217
|
+
result.concat reduce_to_value_nodes([node.children[0]])
|
218
|
+
# Return the result here because the rest of the code is
|
219
|
+
# unreachable
|
220
|
+
return result
|
221
|
+
else
|
222
|
+
result.concat get_return_nodes_only(node)
|
223
|
+
end
|
224
|
+
result.concat reduce_to_value_nodes([nodes.last]) if idx == nodes.length - 1
|
225
|
+
end
|
226
|
+
result
|
227
|
+
end
|
228
|
+
|
229
|
+
def get_return_nodes_only parent
|
230
|
+
return [] unless parent.is_a?(RubyVM::AbstractSyntaxTree::Node)
|
231
|
+
result = []
|
232
|
+
nodes = parent.children.select{|n| n.is_a?(RubyVM::AbstractSyntaxTree::Node)}
|
233
|
+
nodes.each do |node|
|
234
|
+
next if SKIPPABLE.include?(node.type)
|
235
|
+
if node.type == :RETURN
|
236
|
+
result.concat reduce_to_value_nodes([node.children[0]])
|
237
|
+
# Return the result here because the rest of the code is
|
238
|
+
# unreachable
|
239
|
+
return result
|
240
|
+
else
|
241
|
+
result.concat get_return_nodes_only(node)
|
242
|
+
end
|
243
|
+
end
|
244
|
+
result
|
245
|
+
end
|
246
|
+
|
247
|
+
def reduce_to_value_nodes nodes
|
248
|
+
result = []
|
249
|
+
nodes.each do |node|
|
250
|
+
if !node.is_a?(RubyVM::AbstractSyntaxTree::Node)
|
251
|
+
result.push nil
|
252
|
+
elsif REDUCEABLE.include?(node.type)
|
253
|
+
result.concat get_return_nodes_from_children(node)
|
254
|
+
elsif CONDITIONAL.include?(node.type)
|
255
|
+
result.concat reduce_to_value_nodes(node.children[1..-1])
|
256
|
+
elsif node.type == :RETURN
|
257
|
+
if node.children[0].nil?
|
258
|
+
result.push nil
|
259
|
+
else
|
260
|
+
result.concat get_return_nodes(node.children[0])
|
261
|
+
end
|
262
|
+
elsif node.type == :OR
|
263
|
+
result.concat reduce_to_value_nodes(node.children)
|
264
|
+
elsif node.type == :BLOCK
|
265
|
+
result.concat get_return_nodes_only(node.children[2])
|
266
|
+
elsif node.type == :RESBODY
|
267
|
+
result.concat reduce_to_value_nodes([node.children[1]])
|
268
|
+
else
|
269
|
+
result.push node
|
270
|
+
end
|
271
|
+
end
|
272
|
+
result
|
273
|
+
end
|
274
|
+
end
|
275
|
+
end
|
276
|
+
end
|
277
|
+
end
|
278
|
+
end
|
279
|
+
end
|