rbs_activesupport 1.1.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +9 -0
- data/.vscode/settings.json +3 -1
- data/README.md +7 -0
- data/lib/generators/rbs_activesupport/install_generator.rb +2 -0
- data/lib/rbs_activesupport/ast.rb +4 -2
- data/lib/rbs_activesupport/attribute_accessor.rb +13 -10
- data/lib/rbs_activesupport/class_attribute.rb +12 -9
- data/lib/rbs_activesupport/declaration_builder.rb +31 -13
- data/lib/rbs_activesupport/delegate.rb +11 -6
- data/lib/rbs_activesupport/generator.rb +18 -8
- data/lib/rbs_activesupport/include.rb +14 -8
- data/lib/rbs_activesupport/method_searcher.rb +28 -9
- data/lib/rbs_activesupport/parser/comment_parser.rb +5 -3
- data/lib/rbs_activesupport/parser.rb +26 -9
- data/lib/rbs_activesupport/rake_task.rb +21 -12
- data/lib/rbs_activesupport/version.rb +1 -1
- data/rbs_collection.lock.yaml +20 -20
- data/sig/generators/rbs_activesupport/install_generator.rbs +7 -0
- data/sig/rbs_activesupport/ast.rbs +8 -2
- data/sig/rbs_activesupport/attribute_accessor.rbs +13 -0
- data/sig/rbs_activesupport/class_attribute.rbs +12 -0
- data/sig/rbs_activesupport/declaration_builder.rbs +32 -4
- data/sig/rbs_activesupport/delegate.rbs +11 -0
- data/sig/rbs_activesupport/generator.rbs +14 -0
- data/sig/rbs_activesupport/include.rbs +14 -0
- data/sig/rbs_activesupport/method_searcher.rbs +14 -1
- data/sig/rbs_activesupport/parser/comment_parser.rbs +5 -0
- data/sig/rbs_activesupport/parser.rbs +25 -2
- data/sig/rbs_activesupport/rake_task.rbs +10 -0
- data/sig/rbs_activesupport/version.rbs +5 -0
- data/sig/rbs_activesupport.rbs +4 -2
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ccecb9129edd479a2f56c541fc90b0a6a2d97f72e757716767ff48c8d54b2400
|
4
|
+
data.tar.gz: df1a171c089788a1fd9e63c5cde999e92513973344943148baa39e8f29ea06ce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f9f43ad74fe99dcff010b3beed25ffbf58e923ae5d7d0a4aeaa7777f167eb2a0bdcd2ad215cf6c7b593948f975e326007d8b03ce22472a0cb153119a56fb1a7b
|
7
|
+
data.tar.gz: 5bbaefec682d46203cda74d49bd6e9eab1c7461afc6d13448d0056f93da5d1cd485ac790a4e415790e11abd57532fdf0bf8fcb61a48cc6189146539118ac0038
|
data/.rubocop.yml
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
AllCops:
|
2
2
|
TargetRubyVersion: 2.7
|
3
3
|
|
4
|
+
Layout/LeadingCommentSpace:
|
5
|
+
Enabled: false
|
6
|
+
|
4
7
|
Metrics/AbcSize:
|
5
8
|
Max: 25
|
6
9
|
Exclude:
|
@@ -22,6 +25,12 @@ Metrics/MethodLength:
|
|
22
25
|
Exclude:
|
23
26
|
- "lib/rbs_activesupport/ast.rb"
|
24
27
|
|
28
|
+
Style/AccessorGrouping:
|
29
|
+
Enabled: false
|
30
|
+
|
31
|
+
Style/CommentedKeyword:
|
32
|
+
Enabled: false
|
33
|
+
|
25
34
|
Style/Documentation:
|
26
35
|
Enabled: false
|
27
36
|
|
data/.vscode/settings.json
CHANGED
data/README.md
CHANGED
@@ -14,6 +14,13 @@ After the installation, please run rake task generator:
|
|
14
14
|
|
15
15
|
bundle exec rails g rbs_activesupport:install
|
16
16
|
|
17
|
+
And then, please modify `lib/tasks/rbs_activesupport.rake` to fit your application.
|
18
|
+
For example, set it up like this if you're using Rails configuration:
|
19
|
+
|
20
|
+
RbsActivesupport::RakeTask.new do |task|
|
21
|
+
task.target_directories = [Rails.root / "app", Rails.root / "lib"]
|
22
|
+
end
|
23
|
+
|
17
24
|
## Usage
|
18
25
|
|
19
26
|
Run `rbs:activesupport:setup` task:
|
@@ -12,6 +12,8 @@ module RbsActivesupport
|
|
12
12
|
require 'rbs_activesupport/rake_task'
|
13
13
|
|
14
14
|
RbsActivesupport::RakeTask.new do |task|
|
15
|
+
# The target directories
|
16
|
+
# task.target_directories = [Rails.root / "app"]
|
15
17
|
end
|
16
18
|
rescue LoadError
|
17
19
|
# failed to load rbs_activesupport. Skip to load rbs_activesupport tasks.
|
@@ -2,13 +2,15 @@
|
|
2
2
|
|
3
3
|
module RbsActivesupport
|
4
4
|
module AST
|
5
|
-
|
5
|
+
# @rbs node: Array[untyped]
|
6
|
+
def eval_args(node) #: Array[Array[Symbol?]]
|
6
7
|
# @type var args: Array[Array[Symbol?]]
|
7
8
|
*args, _ = eval_node(node)
|
8
9
|
args
|
9
10
|
end
|
10
11
|
|
11
|
-
|
12
|
+
# @rbs node: Array[untyped]
|
13
|
+
def eval_args_with_options(node) #: [Array[Symbol], Hash[Symbol, untyped]]
|
12
14
|
# @type var methods: Array[Symbol]
|
13
15
|
# @type var options: Hash[Symbol, untyped]
|
14
16
|
*args, _ = eval_node(node)
|
@@ -2,14 +2,17 @@
|
|
2
2
|
|
3
3
|
module RbsActivesupport
|
4
4
|
class AttributeAccessor
|
5
|
-
attr_reader :name
|
5
|
+
attr_reader :name #: Symbol
|
6
|
+
attr_reader :options #: Hash[untyped, untyped]
|
6
7
|
|
7
|
-
|
8
|
+
# @rbs name: Symbol
|
9
|
+
# @rbs options: Hash[untyped, untyped]
|
10
|
+
def initialize(name, options) #: void
|
8
11
|
@name = name
|
9
12
|
@options = options
|
10
13
|
end
|
11
14
|
|
12
|
-
def type
|
15
|
+
def type #: String
|
13
16
|
# @type var trailng_comment: String?
|
14
17
|
trailing_comment = options[:trailing_comment]
|
15
18
|
if trailing_comment&.start_with?("#:")
|
@@ -19,31 +22,31 @@ module RbsActivesupport
|
|
19
22
|
end
|
20
23
|
end
|
21
24
|
|
22
|
-
def singleton_reader?
|
25
|
+
def singleton_reader? #: bool
|
23
26
|
options.fetch(:singleton_reader, true)
|
24
27
|
end
|
25
28
|
|
26
|
-
def singleton_writer?
|
29
|
+
def singleton_writer? #: bool
|
27
30
|
options.fetch(:singleton_writer, true)
|
28
31
|
end
|
29
32
|
|
30
|
-
def instance_accessor?
|
33
|
+
def instance_accessor? #: bool
|
31
34
|
options.fetch(:instance_accessor, true)
|
32
35
|
end
|
33
36
|
|
34
|
-
def instance_reader?
|
37
|
+
def instance_reader? #: bool
|
35
38
|
options.fetch(:instance_reader, instance_accessor?)
|
36
39
|
end
|
37
40
|
|
38
|
-
def instance_writer?
|
41
|
+
def instance_writer? #: bool
|
39
42
|
options.fetch(:instance_writer, instance_accessor?)
|
40
43
|
end
|
41
44
|
|
42
|
-
def public?
|
45
|
+
def public? #: bool
|
43
46
|
!private?
|
44
47
|
end
|
45
48
|
|
46
|
-
def private?
|
49
|
+
def private? #: bool
|
47
50
|
options.fetch(:private, false)
|
48
51
|
end
|
49
52
|
end
|
@@ -2,14 +2,17 @@
|
|
2
2
|
|
3
3
|
module RbsActivesupport
|
4
4
|
class ClassAttribute
|
5
|
-
attr_reader :name
|
5
|
+
attr_reader :name #: Symbol
|
6
|
+
attr_reader :options #: Hash[untyped, untyped]
|
6
7
|
|
7
|
-
|
8
|
+
# @rbs name: Symbol
|
9
|
+
# @rbs options: Hash[untyped, untyped]
|
10
|
+
def initialize(name, options) #: void
|
8
11
|
@name = name
|
9
12
|
@options = options
|
10
13
|
end
|
11
14
|
|
12
|
-
def type
|
15
|
+
def type #: String
|
13
16
|
# @type var trailng_comment: String?
|
14
17
|
trailing_comment = options[:trailing_comment]
|
15
18
|
if trailing_comment&.start_with?("#:")
|
@@ -19,27 +22,27 @@ module RbsActivesupport
|
|
19
22
|
end
|
20
23
|
end
|
21
24
|
|
22
|
-
def instance_accessor?
|
25
|
+
def instance_accessor? #: bool
|
23
26
|
options.fetch(:instance_accessor, true)
|
24
27
|
end
|
25
28
|
|
26
|
-
def instance_reader?
|
29
|
+
def instance_reader? #: bool
|
27
30
|
options.fetch(:instance_reader, instance_accessor?)
|
28
31
|
end
|
29
32
|
|
30
|
-
def instance_writer?
|
33
|
+
def instance_writer? #: bool
|
31
34
|
options.fetch(:instance_writer, instance_accessor?)
|
32
35
|
end
|
33
36
|
|
34
|
-
def instance_predicate?
|
37
|
+
def instance_predicate? #: bool
|
35
38
|
options.fetch(:instance_predicate, true)
|
36
39
|
end
|
37
40
|
|
38
|
-
def public?
|
41
|
+
def public? #: bool
|
39
42
|
!private?
|
40
43
|
end
|
41
44
|
|
42
|
-
def private?
|
45
|
+
def private? #: bool
|
43
46
|
options.fetch(:private, false)
|
44
47
|
end
|
45
48
|
end
|
@@ -2,15 +2,20 @@
|
|
2
2
|
|
3
3
|
module RbsActivesupport
|
4
4
|
class DeclarationBuilder
|
5
|
+
# @rbs! type t = AttributeAccessor | ClassAttribute | Delegate | Include
|
6
|
+
|
5
7
|
include AST
|
6
8
|
|
7
|
-
attr_reader :method_searcher
|
9
|
+
attr_reader :method_searcher #: MethodSearcher
|
8
10
|
|
9
|
-
|
11
|
+
# @rbs method_searcher: MethodSearcher
|
12
|
+
def initialize(method_searcher) #: void
|
10
13
|
@method_searcher = method_searcher
|
11
14
|
end
|
12
15
|
|
13
|
-
|
16
|
+
# @rbs namespace: RBS::Namespace
|
17
|
+
# @rbs method_calls: Array[Parser::MethodCall]
|
18
|
+
def build(namespace, method_calls) #: [Array[String], Array[String]]
|
14
19
|
public_decls, private_decls = build_method_calls(namespace, method_calls).partition(&:public?)
|
15
20
|
[public_decls.map(&method(:render)).compact, # steep:ignore BlockTypeMismatch
|
16
21
|
private_decls.map(&method(:render)).compact] # steep:ignore BlockTypeMismatch
|
@@ -18,7 +23,9 @@ module RbsActivesupport
|
|
18
23
|
|
19
24
|
private
|
20
25
|
|
21
|
-
|
26
|
+
# @rbs namespace: RBS::Namespace
|
27
|
+
# @rbs method_calls: Array[Parser::MethodCall]
|
28
|
+
def build_method_calls(namespace, method_calls) #: Array[t]
|
22
29
|
method_calls.flat_map do |method_call|
|
23
30
|
case method_call.name
|
24
31
|
when :class_attribute
|
@@ -36,7 +43,8 @@ module RbsActivesupport
|
|
36
43
|
end.compact
|
37
44
|
end
|
38
45
|
|
39
|
-
|
46
|
+
# @rbs method_call: Parser::MethodCall
|
47
|
+
def build_attribute_accessor(method_call) #: Array[AttributeAccessor]
|
40
48
|
methods, options = eval_args_with_options(method_call.args)
|
41
49
|
options[:singleton_reader] = false if %i[cattr_writer mattr_writer].include?(method_call.name)
|
42
50
|
options[:singleton_writer] = false if %i[cattr_reader mattr_reader].include?(method_call.name)
|
@@ -49,7 +57,8 @@ module RbsActivesupport
|
|
49
57
|
end
|
50
58
|
end
|
51
59
|
|
52
|
-
|
60
|
+
# @rbs method_call: Parser::MethodCall
|
61
|
+
def build_class_attribute(method_call) #: Array[ClassAttribute]
|
53
62
|
methods, options = eval_args_with_options(method_call.args)
|
54
63
|
options[:private] = true if method_call.private?
|
55
64
|
options[:trailing_comment] = method_call.trailing_comment
|
@@ -58,7 +67,9 @@ module RbsActivesupport
|
|
58
67
|
end
|
59
68
|
end
|
60
69
|
|
61
|
-
|
70
|
+
# @rbs namespace: RBS::Namespace
|
71
|
+
# @rbs method_call: Parser::MethodCall
|
72
|
+
def build_delegate(namespace, method_call) #: Array[Delegate]
|
62
73
|
methods, options = eval_args_with_options(method_call.args)
|
63
74
|
options[:private] = true if method_call.private?
|
64
75
|
methods.map do |method|
|
@@ -66,14 +77,17 @@ module RbsActivesupport
|
|
66
77
|
end
|
67
78
|
end
|
68
79
|
|
69
|
-
|
80
|
+
# @rbs namespace: RBS::Namespace
|
81
|
+
# @rbs method_call: Parser::MethodCall
|
82
|
+
def build_include(namespace, method_call) #: Array[Include]
|
70
83
|
module_paths = eval_args(method_call.args)
|
71
84
|
module_paths.map do |module_path|
|
72
85
|
Include.new(namespace, module_path, { private: method_call.private? })
|
73
86
|
end
|
74
87
|
end
|
75
88
|
|
76
|
-
|
89
|
+
# @rbs decl: t
|
90
|
+
def render(decl) #: String?
|
77
91
|
case decl
|
78
92
|
when AttributeAccessor
|
79
93
|
render_attribute_accessor(decl)
|
@@ -86,7 +100,8 @@ module RbsActivesupport
|
|
86
100
|
end
|
87
101
|
end
|
88
102
|
|
89
|
-
|
103
|
+
# @rbs decl: AttributeAccessor
|
104
|
+
def render_attribute_accessor(decl) #: String
|
90
105
|
methods = []
|
91
106
|
methods << "def self.#{decl.name}: () -> (#{decl.type})" if decl.singleton_reader?
|
92
107
|
methods << "def self.#{decl.name}=: (#{decl.type}) -> (#{decl.type})" if decl.singleton_writer?
|
@@ -95,7 +110,8 @@ module RbsActivesupport
|
|
95
110
|
methods.join("\n")
|
96
111
|
end
|
97
112
|
|
98
|
-
|
113
|
+
# @rbs decl: ClassAttribute
|
114
|
+
def render_class_attribute(decl) #: String
|
99
115
|
methods = []
|
100
116
|
methods << "def self.#{decl.name}: () -> (#{decl.type})"
|
101
117
|
methods << "def self.#{decl.name}=: (#{decl.type}) -> (#{decl.type})"
|
@@ -106,13 +122,15 @@ module RbsActivesupport
|
|
106
122
|
methods.join("\n")
|
107
123
|
end
|
108
124
|
|
109
|
-
|
125
|
+
# @rbs decl: Delegate
|
126
|
+
def render_delegate(decl) #: String
|
110
127
|
method_types = method_searcher.method_types_for(decl)
|
111
128
|
|
112
129
|
"def #{decl.method_name}: #{method_types.join(" | ")}"
|
113
130
|
end
|
114
131
|
|
115
|
-
|
132
|
+
# @rbs decl: Include
|
133
|
+
def render_include(decl) #: String?
|
116
134
|
return unless decl.concern? && decl.classmethods?
|
117
135
|
|
118
136
|
<<~RBS
|
@@ -2,19 +2,24 @@
|
|
2
2
|
|
3
3
|
module RbsActivesupport
|
4
4
|
class Delegate
|
5
|
-
attr_reader :namespace
|
5
|
+
attr_reader :namespace #: RBS::Namespace
|
6
|
+
attr_reader :method #: Symbol
|
7
|
+
attr_reader :options #: Hash[Symbol, untyped]
|
6
8
|
|
7
|
-
|
9
|
+
# @rbs namespace: RBS::Namespace
|
10
|
+
# @rbs method: Symbol
|
11
|
+
# @rbs options: Hash[Symbol, untyped]
|
12
|
+
def initialize(namespace, method, options) #: void
|
8
13
|
@namespace = namespace
|
9
14
|
@method = method
|
10
15
|
@options = options
|
11
16
|
end
|
12
17
|
|
13
|
-
def to
|
18
|
+
def to #: Symbol
|
14
19
|
options[:to]
|
15
20
|
end
|
16
21
|
|
17
|
-
def method_name
|
22
|
+
def method_name #: Symbol
|
18
23
|
case options[:prefix]
|
19
24
|
when true
|
20
25
|
:"#{to}_#{method}"
|
@@ -25,11 +30,11 @@ module RbsActivesupport
|
|
25
30
|
end
|
26
31
|
end
|
27
32
|
|
28
|
-
def public?
|
33
|
+
def public? #: bool
|
29
34
|
!private?
|
30
35
|
end
|
31
36
|
|
32
|
-
def private?
|
37
|
+
def private? #: bool
|
33
38
|
options.fetch(:private, false)
|
34
39
|
end
|
35
40
|
end
|
@@ -2,20 +2,27 @@
|
|
2
2
|
|
3
3
|
module RbsActivesupport
|
4
4
|
class Generator
|
5
|
-
|
5
|
+
# @rbs pathname: Pathname
|
6
|
+
# @rbs rbs_builder: RBS::DefinitionBuilder
|
7
|
+
def self.generate(pathname, rbs_builder) #: String?
|
6
8
|
new(pathname, rbs_builder).generate
|
7
9
|
end
|
8
10
|
|
9
|
-
|
11
|
+
include AST
|
10
12
|
|
11
|
-
|
13
|
+
attr_reader :pathname #: Pathname
|
14
|
+
attr_reader :declaration_builder #: DeclarationBuilder
|
15
|
+
|
16
|
+
# @rbs pathname: Pathname
|
17
|
+
# @rbs rbs_builder: RBS::DefinitionBuilder
|
18
|
+
def initialize(pathname, rbs_builder) #: void
|
12
19
|
@pathname = pathname
|
13
20
|
|
14
21
|
method_searcher = MethodSearcher.new(rbs_builder)
|
15
22
|
@declaration_builder = DeclarationBuilder.new(method_searcher)
|
16
23
|
end
|
17
24
|
|
18
|
-
def generate
|
25
|
+
def generate #: String?
|
19
26
|
declarations = parse_source_code
|
20
27
|
return if declarations.empty?
|
21
28
|
|
@@ -42,20 +49,22 @@ module RbsActivesupport
|
|
42
49
|
|
43
50
|
private
|
44
51
|
|
45
|
-
|
52
|
+
# @rbs rbs: String
|
53
|
+
def format(rbs) #: String
|
46
54
|
parsed = RBS::Parser.parse_signature(rbs)
|
47
55
|
StringIO.new.tap do |out|
|
48
56
|
RBS::Writer.new(out: out).write(parsed[1] + parsed[2])
|
49
57
|
end.string
|
50
58
|
end
|
51
59
|
|
52
|
-
def parse_source_code
|
60
|
+
def parse_source_code #: Hash[RBS::Namespace, Array[Parser::MethodCall]]
|
53
61
|
parser = Parser.new
|
54
62
|
parser.parse(pathname.read)
|
55
63
|
parser.method_calls
|
56
64
|
end
|
57
65
|
|
58
|
-
|
66
|
+
# @rbs namespace: RBS::Namespace
|
67
|
+
def header(namespace) #: String
|
59
68
|
context = +""
|
60
69
|
namespace.path.map do |mod_name|
|
61
70
|
context += "::#{mod_name}"
|
@@ -75,7 +84,8 @@ module RbsActivesupport
|
|
75
84
|
end.join("\n")
|
76
85
|
end
|
77
86
|
|
78
|
-
|
87
|
+
# @rbs namespace: RBS::Namespace
|
88
|
+
def footer(namespace) #: String
|
79
89
|
"end\n" * namespace.path.size
|
80
90
|
end
|
81
91
|
end
|
@@ -4,15 +4,20 @@ require "active_support/concern"
|
|
4
4
|
|
5
5
|
module RbsActivesupport
|
6
6
|
class Include
|
7
|
-
attr_reader :context
|
7
|
+
attr_reader :context #: RBS::Namespace
|
8
|
+
attr_reader :module_path #: Array[Symbol?]
|
9
|
+
attr_reader :options #: Hash[Symbol, untyped]
|
8
10
|
|
9
|
-
|
11
|
+
# @rbs context: RBS::Namespace
|
12
|
+
# @rbs module_path: Array[Symbol?]
|
13
|
+
# @rbs options: Hash[Symbol, untyped]
|
14
|
+
def initialize(context, module_path, options) #: void
|
10
15
|
@context = context
|
11
16
|
@module_path = module_path
|
12
17
|
@options = options
|
13
18
|
end
|
14
19
|
|
15
|
-
def argument
|
20
|
+
def argument #: RBS::Namespace
|
16
21
|
if module_path.first.nil?
|
17
22
|
RBS::Namespace.new(path: module_path[1...], absolute: true) # steep:ignore ArgumentTypeMismatch
|
18
23
|
else
|
@@ -20,7 +25,8 @@ module RbsActivesupport
|
|
20
25
|
end
|
21
26
|
end
|
22
27
|
|
23
|
-
|
28
|
+
# @rbs %a{pure}
|
29
|
+
def module_name #: RBS::Namespace?
|
24
30
|
namespace = @context
|
25
31
|
|
26
32
|
loop do
|
@@ -33,7 +39,7 @@ module RbsActivesupport
|
|
33
39
|
end
|
34
40
|
end
|
35
41
|
|
36
|
-
def concern?
|
42
|
+
def concern? #: bool
|
37
43
|
return false unless module_name
|
38
44
|
|
39
45
|
modname = module_name.to_s.delete_suffix("::")
|
@@ -43,18 +49,18 @@ module RbsActivesupport
|
|
43
49
|
mod&.singleton_class&.include?(ActiveSupport::Concern)
|
44
50
|
end
|
45
51
|
|
46
|
-
def classmethods?
|
52
|
+
def classmethods? #: bool
|
47
53
|
return false unless module_name
|
48
54
|
|
49
55
|
modname = module_name.append(:ClassMethods).to_s.delete_suffix("::")
|
50
56
|
Object.const_defined?(modname)
|
51
57
|
end
|
52
58
|
|
53
|
-
def public?
|
59
|
+
def public? #: bool
|
54
60
|
!private?
|
55
61
|
end
|
56
62
|
|
57
|
-
def private?
|
63
|
+
def private? #: bool
|
58
64
|
options.fetch(:private, false)
|
59
65
|
end
|
60
66
|
end
|
@@ -4,28 +4,47 @@ require "rbs"
|
|
4
4
|
|
5
5
|
module RbsActivesupport
|
6
6
|
class MethodSearcher
|
7
|
-
attr_reader :rbs_builder
|
7
|
+
attr_reader :rbs_builder #: RBS::DefinitionBuilder
|
8
8
|
|
9
|
-
|
9
|
+
# @rbs rbs_builder: RBS::DefinitionBuilder
|
10
|
+
def initialize(rbs_builder) #: void
|
10
11
|
@rbs_builder = rbs_builder
|
11
12
|
end
|
12
13
|
|
13
|
-
|
14
|
+
# @rbs delegate: Delegate
|
15
|
+
def method_types_for(delegate) #: Array[String]
|
14
16
|
delegate_to = lookup_method_types(delegate.namespace.to_type_name, delegate.to)
|
15
17
|
return ["() -> untyped"] if delegate_to.any? { |t| t.type.return_type.is_a?(RBS::Types::Bases::Any) }
|
16
18
|
|
17
|
-
return_types = delegate_to
|
18
|
-
|
19
|
-
|
20
|
-
.flat_map { |t| lookup_method_types(t, delegate.method) }
|
21
|
-
.map(&:to_s)
|
19
|
+
return_types = return_type_names_for(delegate_to).uniq
|
20
|
+
.flat_map { |t| lookup_method_types(t, delegate.method) }
|
21
|
+
.map(&:to_s)
|
22
22
|
return_types << "() -> untyped" if return_types.empty?
|
23
23
|
return_types
|
24
24
|
end
|
25
25
|
|
26
26
|
private
|
27
27
|
|
28
|
-
|
28
|
+
# @rbs delegate_to: Array[RBS::MethodType]
|
29
|
+
def return_type_names_for(delegate_to) #: Array[RBS::TypeName]
|
30
|
+
delegate_to.filter_map do |t|
|
31
|
+
type_name_for(t.type.return_type)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# @rbs type: RBS::Types::t
|
36
|
+
def type_name_for(type) #: RBS::TypeName?
|
37
|
+
case type
|
38
|
+
when RBS::Types::Optional
|
39
|
+
type_name_for(type.type)
|
40
|
+
when RBS::Types::ClassSingleton, RBS::Types::ClassInstance, RBS::Types::Interface, RBS::Types::Alias
|
41
|
+
type.name
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# @rbs type_name: RBS::TypeName
|
46
|
+
# @rbs method: Symbol
|
47
|
+
def lookup_method_types(type_name, method) #: Array[RBS::MethodType]
|
29
48
|
instance = rbs_builder.build_instance(type_name)
|
30
49
|
method_def = instance.methods[method]
|
31
50
|
return [] unless method_def
|
@@ -3,14 +3,16 @@
|
|
3
3
|
module RbsActivesupport
|
4
4
|
class Parser
|
5
5
|
class CommentParser
|
6
|
-
attr_reader :line_comments,
|
6
|
+
attr_reader :line_comments #: Hash[Integer, String]
|
7
|
+
attr_reader :trailing_comments #: Hash[Integer, String]
|
7
8
|
|
8
|
-
def initialize
|
9
|
+
def initialize #: void
|
9
10
|
@line_comments = {}
|
10
11
|
@trailing_comments = {}
|
11
12
|
end
|
12
13
|
|
13
|
-
|
14
|
+
# @rbs string: String
|
15
|
+
def parse(string) #: self
|
14
16
|
# @type var code_lines: Hash[Integer, bool]
|
15
17
|
code_lines = {}
|
16
18
|
Ripper.lex(string).each do |(line, _), type, token, _|
|
@@ -8,39 +8,54 @@ require "rbs/prototype/rb"
|
|
8
8
|
module RbsActivesupport
|
9
9
|
class Parser < ::RBS::Prototype::RB
|
10
10
|
class MethodCall
|
11
|
-
attr_reader :name
|
11
|
+
attr_reader :name #: t
|
12
|
+
attr_reader :args #: Array[RubyVM::AbstractSyntaxTree::Node]
|
13
|
+
attr_reader :trailing_comment #: String?
|
12
14
|
|
13
|
-
|
15
|
+
# @rbs @private: bool
|
16
|
+
|
17
|
+
# @rbs name: t
|
18
|
+
# @rbs args: Array[RubyVM::AbstractSyntaxTree::Node]
|
19
|
+
# @rbs private: bool
|
20
|
+
# @rbs trailing_comment: String?
|
21
|
+
def initialize(name, args, private, trailing_comment: nil) #: void
|
14
22
|
@name = name
|
15
23
|
@args = args
|
16
24
|
@private = private
|
17
25
|
@trailing_comment = trailing_comment
|
18
26
|
end
|
19
27
|
|
20
|
-
def private?
|
28
|
+
def private? #: bool
|
21
29
|
@private
|
22
30
|
end
|
23
31
|
end
|
24
32
|
|
33
|
+
# @rbs!
|
34
|
+
# type t = :class_attribute | :delegate | :cattr_accessor | :mattr_accessor | :cattr_reader | :mattr_reader |
|
35
|
+
# :cattr_writer | :mattr_writer | :include
|
36
|
+
|
25
37
|
METHODS = %i[
|
26
38
|
class_attribute delegate cattr_accessor mattr_accessor cattr_reader mattr_reader cattr_writer mattr_writer include
|
27
|
-
].freeze # steep:ignore IncompatibleAssignment
|
39
|
+
].freeze #: Array[t] # steep:ignore IncompatibleAssignment
|
28
40
|
|
29
41
|
alias process_orig process
|
30
42
|
|
31
|
-
attr_reader :comment_parser
|
43
|
+
attr_reader :comment_parser #: CommentParser
|
44
|
+
attr_reader :method_calls #: Hash[RBS::Namespace, Array[MethodCall]]
|
32
45
|
|
33
|
-
def initialize
|
46
|
+
def initialize #: void
|
34
47
|
super
|
35
48
|
@comment_parser = CommentParser.new
|
36
49
|
@method_calls = Hash.new { |hash, key| hash[key] = [] }
|
37
50
|
end
|
38
51
|
|
39
|
-
|
52
|
+
# @rbs string: String
|
53
|
+
def parse(string) #: void
|
40
54
|
comment_parser.parse(string)
|
41
55
|
super
|
42
56
|
end
|
43
57
|
|
58
|
+
# @rbs override
|
44
59
|
def process(node, decls:, comments:, context:)
|
45
60
|
case node.type
|
46
61
|
when :FCALL, :VCALL
|
@@ -57,11 +72,13 @@ module RbsActivesupport
|
|
57
72
|
end
|
58
73
|
end
|
59
74
|
|
60
|
-
|
75
|
+
# @rbs node: RubyVM::AbstractSyntaxTree::Node
|
76
|
+
def trailing_comment_for(node) #: String?
|
61
77
|
comment_parser.trailing_comments[node.last_lineno]
|
62
78
|
end
|
63
79
|
|
64
|
-
|
80
|
+
# @rbs decls: Array[RBS::AST::Declarations::t | RBS::AST::Members::t]
|
81
|
+
def private?(decls) #: bool
|
65
82
|
current_accessibility(decls) == private
|
66
83
|
end
|
67
84
|
end
|