rbs-inline 0.3.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +10 -7
  3. data/Rakefile +12 -0
  4. data/lib/rbs/inline/annotation_parser/tokenizer.rb +361 -0
  5. data/lib/rbs/inline/annotation_parser.rb +548 -326
  6. data/lib/rbs/inline/ast/annotations.rb +446 -136
  7. data/lib/rbs/inline/ast/comment_lines.rb +32 -18
  8. data/lib/rbs/inline/ast/declarations.rb +67 -28
  9. data/lib/rbs/inline/ast/members.rb +137 -140
  10. data/lib/rbs/inline/ast/tree.rb +104 -5
  11. data/lib/rbs/inline/cli.rb +12 -12
  12. data/lib/rbs/inline/node_utils.rb +4 -0
  13. data/lib/rbs/inline/parser.rb +140 -59
  14. data/lib/rbs/inline/version.rb +1 -1
  15. data/lib/rbs/inline/writer.rb +243 -94
  16. data/lib/rbs/inline.rb +4 -0
  17. data/rbs_collection.lock.yaml +3 -7
  18. data/rbs_collection.yaml +2 -0
  19. data/sig/generated/rbs/inline/annotation_parser/tokenizer.rbs +221 -0
  20. data/sig/generated/rbs/inline/annotation_parser.rbs +148 -92
  21. data/sig/generated/rbs/inline/ast/annotations.rbs +142 -36
  22. data/sig/generated/rbs/inline/ast/comment_lines.rbs +35 -0
  23. data/sig/generated/rbs/inline/ast/declarations.rbs +29 -10
  24. data/sig/generated/rbs/inline/ast/members.rbs +33 -24
  25. data/sig/generated/rbs/inline/ast/tree.rbs +132 -0
  26. data/sig/generated/rbs/inline/cli.rbs +3 -3
  27. data/sig/generated/rbs/inline/node_utils.rbs +11 -0
  28. data/sig/generated/rbs/inline/parser.rbs +38 -18
  29. data/sig/generated/rbs/inline/version.rbs +7 -0
  30. data/sig/generated/rbs/inline/writer.rbs +104 -0
  31. data/sig/generated/rbs/inline.rbs +7 -0
  32. metadata +14 -14
  33. data/sig/rbs/inline/annotation_parser.rbs +0 -0
  34. data/sig/rbs/inline/ast/comment_lines.rbs +0 -27
  35. data/sig/rbs/inline/ast/tree.rbs +0 -98
  36. data/sig/rbs/inline/node_utils.rbs +0 -7
  37. data/sig/rbs/inline/writer.rbs +0 -27
  38. data/sig/rbs/inline.rbs +0 -41
  39. data/yard-samples/hello.rb +0 -6
  40. data/yard-samples/sample1.rb +0 -26
@@ -5,11 +5,13 @@ use Prism::*
5
5
  module RBS
6
6
  module Inline
7
7
  class Parser < Prism::Visitor
8
+ type with_members = AST::Declarations::ModuleDecl | AST::Declarations::ClassDecl | AST::Declarations::SingletonClassDecl | AST::Declarations::BlockDecl
9
+
8
10
  # The top level declarations
9
11
  attr_reader decls: Array[AST::Declarations::t]
10
12
 
11
13
  # The surrounding declarations
12
- attr_reader surrounding_decls: Array[AST::Declarations::ModuleDecl | AST::Declarations::ClassDecl]
14
+ attr_reader surrounding_decls: Array[with_members]
13
15
 
14
16
  # ParsingResult associated with the line number at the end
15
17
  #
@@ -30,21 +32,30 @@ module RBS
30
32
 
31
33
  def initialize: () -> void
32
34
 
35
+ # Parses the given Prism result to a three tuple
36
+ #
37
+ # Returns a three tuple of:
38
+ #
39
+ # 1. An array of `use` directives
40
+ # 2. An array of declarations
41
+ # 3. An array of RBS declarations given as `@rbs!` annotation at top-level
42
+ #
43
+ # Note that only RBS declarations are allowed in the top-level `@rbs!` annotations.
44
+ # RBS *members* are ignored in the array.
45
+ #
33
46
  # @rbs result: ParseResult
34
47
  # @rbs opt_in: bool -- `true` for *opt-out* mode, `false` for *opt-in* mode.
35
- # @rbs returns [Array[AST::Annotations::Use], Array[AST::Declarations::t]]?
36
- def self.parse: (ParseResult result, opt_in: bool) -> [ Array[AST::Annotations::Use], Array[AST::Declarations::t] ]?
48
+ # @rbs return: [Array[AST::Annotations::Use], Array[AST::Declarations::t], Array[RBS::AST::Declarations::t]]?
49
+ def self.parse: (ParseResult result, opt_in: bool) -> [ Array[AST::Annotations::Use], Array[AST::Declarations::t], Array[RBS::AST::Declarations::t] ]?
37
50
 
38
- # @rbs returns AST::Declarations::ModuleDecl | AST::Declarations::ClassDecl | nil
39
- def current_class_module_decl: () -> (AST::Declarations::ModuleDecl | AST::Declarations::ClassDecl | nil)
51
+ # @rbs return: with_members?
52
+ def current_class_module_decl: () -> with_members?
40
53
 
41
- # @rbs returns AST::Declarations::ModuleDecl | AST::Declarations::ClassDecl
42
- def current_class_module_decl!: () -> (AST::Declarations::ModuleDecl | AST::Declarations::ClassDecl)
54
+ # @rbs return: with_members
55
+ def current_class_module_decl!: () -> with_members
43
56
 
44
- # :: (AST::Declarations::ModuleDecl | AST::Declarations::ClassDecl) { () -> void } -> void
45
- # :: (AST::Declarations::ConstantDecl) -> void
46
- def push_class_module_decl: (AST::Declarations::ModuleDecl | AST::Declarations::ClassDecl) { () -> void } -> void
47
- | (AST::Declarations::ConstantDecl) -> void
57
+ # : (with_members) { () -> void } -> void
58
+ def push_class_module_decl: (with_members) { () -> void } -> void
48
59
 
49
60
  # Load inner declarations and delete them from `#comments` hash
50
61
  #
@@ -60,6 +71,9 @@ module RBS
60
71
  # @rbs override
61
72
  def visit_class_node: ...
62
73
 
74
+ # @rbs override
75
+ def visit_singleton_class_node: ...
76
+
63
77
  # @rbs override
64
78
  def visit_module_node: ...
65
79
 
@@ -86,12 +100,15 @@ module RBS
86
100
  def visit_call_node: ...
87
101
 
88
102
  # @rbs new_visibility: RBS::AST::Members::visibility?
89
- # @rbs block: ^() -> void
90
- # @rbs returns void
103
+ # @rbs &block: () -> void
104
+ # @rbs return: void
91
105
  def push_visibility: (RBS::AST::Members::visibility? new_visibility) { () -> void } -> void
92
106
 
107
+ # @rbs [A] (Node) { () -> A } -> A?
108
+ def process_nesting_node: [A] (Node) { () -> A } -> A?
109
+
93
110
  # @rbs node: Node
94
- # @rbs returns bool
111
+ # @rbs return: bool
95
112
  def ignored_node?: (Node node) -> bool
96
113
 
97
114
  # Fetch Application annotation which is associated to `node`
@@ -99,19 +116,22 @@ module RBS
99
116
  # The application annotation is removed from `comments`.
100
117
  #
101
118
  # @rbs node: Node
102
- # @rbs returns AST::Annotations::Application?
119
+ # @rbs return: AST::Annotations::Application?
103
120
  def application_annotation: (Node node) -> AST::Annotations::Application?
104
121
 
105
- # Fetch Assertion annotation which is associated to `node`
122
+ # Fetch TypeAssertion annotation which is associated to `node`
106
123
  #
107
124
  # The assertion annotation is removed from `comments`.
108
125
  #
109
126
  # @rbs node: Node | Location
110
- # @rbs returns AST::Annotations::Assertion?
111
- def assertion_annotation: (Node | Location node) -> AST::Annotations::Assertion?
127
+ # @rbs return: AST::Annotations::TypeAssertion?
128
+ def assertion_annotation: (Node | Location node) -> AST::Annotations::TypeAssertion?
112
129
 
113
130
  # @rbs override
114
131
  def visit_constant_write_node: ...
132
+
133
+ # @rbs override
134
+ def visit_block_node: ...
115
135
  end
116
136
  end
117
137
  end
@@ -0,0 +1,7 @@
1
+ # Generated from lib/rbs/inline/version.rb with RBS::Inline
2
+
3
+ module RBS
4
+ module Inline
5
+ VERSION: ::String
6
+ end
7
+ end
@@ -0,0 +1,104 @@
1
+ # Generated from lib/rbs/inline/writer.rb with RBS::Inline
2
+
3
+ module RBS
4
+ module Inline
5
+ class Writer
6
+ interface _Content
7
+ def <<: (RBS::AST::Declarations::t | RBS::AST::Members::t) -> void
8
+
9
+ def concat: (Array[RBS::AST::Declarations::t | RBS::AST::Members::t]) -> void
10
+ end
11
+
12
+ attr_reader output: String
13
+
14
+ attr_reader writer: RBS::Writer
15
+
16
+ # @rbs buffer: String
17
+ def initialize: (?String buffer) -> void
18
+
19
+ # @rbs uses: Array[AST::Annotations::Use]
20
+ # @rbs decls: Array[AST::Declarations::t]
21
+ # @rbs rbs_decls: Array[RBS::AST::Declarations::t]
22
+ def self.write: (Array[AST::Annotations::Use] uses, Array[AST::Declarations::t] decls, Array[RBS::AST::Declarations::t] rbs_decls) -> void
23
+
24
+ # @rbs *lines: String
25
+ # @rbs return: void
26
+ def header: (*String lines) -> void
27
+
28
+ # @rbs uses: Array[AST::Annotations::Use]
29
+ # @rbs decls: Array[AST::Declarations::t]
30
+ # @rbs rbs_decls: Array[RBS::AST::Declarations::t] --
31
+ # Top level `rbs!` declarations
32
+ # @rbs return: void
33
+ def write: (Array[AST::Annotations::Use] uses, Array[AST::Declarations::t] decls, Array[RBS::AST::Declarations::t] rbs_decls) -> void
34
+
35
+ # @rbs decl: AST::Declarations::t
36
+ # @rbs rbs: _Content
37
+ # @rbs return: void
38
+ def translate_decl: (AST::Declarations::t decl, _Content rbs) -> void
39
+
40
+ # @rbs decl: AST::Declarations::ClassDecl
41
+ # @rbs rbs: _Content
42
+ # @rbs return: void
43
+ def translate_class_decl: (AST::Declarations::ClassDecl decl, _Content rbs) -> void
44
+
45
+ # @rbs members: Array[AST::Declarations::t | AST::Members::t]
46
+ # @rbs decl: AST::Declarations::SingletonClassDecl?
47
+ # @rbs rbs: _Content
48
+ # @rbs return: void
49
+ def translate_members: (Array[AST::Declarations::t | AST::Members::t] members, AST::Declarations::SingletonClassDecl? decl, _Content rbs) -> void
50
+
51
+ # @rbs decl: AST::Declarations::ModuleDecl
52
+ # @rbs rbs: _Content
53
+ # @rbs return: void
54
+ def translate_module_decl: (AST::Declarations::ModuleDecl decl, _Content rbs) -> void
55
+
56
+ # @rbs decl: AST::Declarations::ConstantDecl
57
+ # @rbs rbs: _Content
58
+ # @rbs return: void
59
+ def translate_constant_decl: (AST::Declarations::ConstantDecl decl, _Content rbs) -> void
60
+
61
+ # @rbs decl: AST::Declarations::SingletonClassDecl
62
+ # @rbs rbs: _Content
63
+ # @rbs return: void
64
+ def translate_singleton_decl: (AST::Declarations::SingletonClassDecl decl, _Content rbs) -> void
65
+
66
+ # @rbs member: AST::Members::t
67
+ # @rbs decl: AST::Declarations::SingletonClassDecl? --
68
+ # The surrouding singleton class definition
69
+ # @rbs rbs: _Content
70
+ # @rbs return void
71
+ def translate_member: (AST::Members::t member, AST::Declarations::SingletonClassDecl? decl, _Content rbs) -> void
72
+
73
+ private
74
+
75
+ # Returns the `kind` of the method definition
76
+ #
77
+ # ```rb
78
+ # def self.foo = () # :singleton
79
+ # class A
80
+ # class << self
81
+ # def bar = () # :singleton
82
+ # end
83
+ # end
84
+ #
85
+ # def object.foo = () # Not supported (returns :instance)
86
+ # ```
87
+ #
88
+ # @rbs member: AST::Members::RubyDef
89
+ # @rbs decl: AST::Declarations::SingletonClassDecl?
90
+ # @rbs return: RBS::AST::Members::MethodDefinition::kind
91
+ def method_kind: (AST::Members::RubyDef member, AST::Declarations::SingletonClassDecl? decl) -> RBS::AST::Members::MethodDefinition::kind
92
+
93
+ # @rbs block: AST::Declarations::BlockDecl
94
+ # @rbs rbs: _Content
95
+ # @rbs return: void
96
+ def translate_module_block_decl: (AST::Declarations::BlockDecl block, _Content rbs) -> void
97
+
98
+ # @rbs block: AST::Declarations::BlockDecl
99
+ # @rbs rbs: _Content
100
+ # @rbs return: void
101
+ def translate_class_block_decl: (AST::Declarations::BlockDecl block, _Content rbs) -> void
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,7 @@
1
+ # Generated from lib/rbs/inline.rb with RBS::Inline
2
+
3
+ module RBS
4
+ module Inline
5
+ type token = [ Symbol, String ]
6
+ end
7
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbs-inline
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Soutaro Matsumoto
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-05-11 00:00:00.000000000 Z
11
+ date: 2024-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: prism
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '0.29'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '0.30'
22
+ version: '0.31'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,21 +29,21 @@ dependencies:
29
29
  version: '0.29'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '0.30'
32
+ version: '0.31'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: rbs
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: 3.5.0.pre
39
+ version: 3.5.0
40
40
  type: :runtime
41
41
  prerelease: false
42
42
  version_requirements: !ruby/object:Gem::Requirement
43
43
  requirements:
44
44
  - - "~>"
45
45
  - !ruby/object:Gem::Version
46
- version: 3.5.0.pre
46
+ version: 3.5.0
47
47
  description: Inline RBS type declaration.
48
48
  email:
49
49
  - matsumoto@soutaro.com
@@ -61,6 +61,7 @@ files:
61
61
  - exe/rbs-inline
62
62
  - lib/rbs/inline.rb
63
63
  - lib/rbs/inline/annotation_parser.rb
64
+ - lib/rbs/inline/annotation_parser/tokenizer.rb
64
65
  - lib/rbs/inline/ast/annotations.rb
65
66
  - lib/rbs/inline/ast/comment_lines.rb
66
67
  - lib/rbs/inline/ast/declarations.rb
@@ -73,21 +74,20 @@ files:
73
74
  - lib/rbs/inline/writer.rb
74
75
  - rbs_collection.lock.yaml
75
76
  - rbs_collection.yaml
77
+ - sig/generated/rbs/inline.rbs
76
78
  - sig/generated/rbs/inline/annotation_parser.rbs
79
+ - sig/generated/rbs/inline/annotation_parser/tokenizer.rbs
77
80
  - sig/generated/rbs/inline/ast/annotations.rbs
81
+ - sig/generated/rbs/inline/ast/comment_lines.rbs
78
82
  - sig/generated/rbs/inline/ast/declarations.rbs
79
83
  - sig/generated/rbs/inline/ast/members.rbs
84
+ - sig/generated/rbs/inline/ast/tree.rbs
80
85
  - sig/generated/rbs/inline/cli.rbs
86
+ - sig/generated/rbs/inline/node_utils.rbs
81
87
  - sig/generated/rbs/inline/parser.rbs
82
- - sig/rbs/inline.rbs
83
- - sig/rbs/inline/annotation_parser.rbs
84
- - sig/rbs/inline/ast/comment_lines.rbs
88
+ - sig/generated/rbs/inline/version.rbs
89
+ - sig/generated/rbs/inline/writer.rbs
85
90
  - sig/rbs/inline/ast/members.rbs
86
- - sig/rbs/inline/ast/tree.rbs
87
- - sig/rbs/inline/node_utils.rbs
88
- - sig/rbs/inline/writer.rbs
89
- - yard-samples/hello.rb
90
- - yard-samples/sample1.rb
91
91
  homepage: https://github.com/soutaro/rbs-inline
92
92
  licenses:
93
93
  - MIT
File without changes
@@ -1,27 +0,0 @@
1
- module RBS
2
- module Inline
3
- module AST
4
- # CommentLines represents consecutive comments
5
- #
6
- # The comments construct one String.
7
- #
8
- # ```ruby
9
- # # Hello <-- Comment1
10
- # # World <-- Comment2
11
- # ```
12
- #
13
- # We want to get a String of comment1 and comment2, `"Hello\nWorld".
14
- # And want to translate a location in the string into the location in comment1 and comment2.
15
- #
16
- class CommentLines
17
- attr_reader comments: Array[[Prism::Comment, Integer]]
18
-
19
- def initialize: (Array[Prism::Comment]) -> void
20
-
21
- def string: () -> String
22
-
23
- def comment_location: (Integer index) -> [Prism::Comment, Integer]?
24
- end
25
- end
26
- end
27
- end
@@ -1,98 +0,0 @@
1
- module RBS
2
- module Inline
3
- module AST
4
- class Tree
5
- type token = [Symbol, String]
6
-
7
- type tree = token | Tree | Types::t | MethodType | nil
8
-
9
- attr_reader trees: Array[tree]
10
-
11
- # Children but without `tWHITESPACE` tokens
12
- #
13
- attr_reader non_trivia_trees: Array[tree]
14
-
15
- attr_reader type: Symbol
16
-
17
- def initialize: (Symbol type) -> void
18
-
19
- def <<: (tree) -> self
20
-
21
- # Returns n-th token from the children
22
- #
23
- # Raises if the value is not a token or nil.
24
- #
25
- def nth_token: (Integer) -> token?
26
-
27
- # Returns n-th token from the children
28
- #
29
- # Returns `nil` if the value is not a token.
30
- #
31
- def nth_token?: (Integer) -> token?
32
-
33
- # Returns n-th token from the children
34
- #
35
- # Raises if the value is not token.
36
- #
37
- def nth_token!: (Integer) -> token
38
-
39
- # Returns n-th tree from the children
40
- #
41
- # Raises if the value is not a tree or nil.
42
- #
43
- def nth_tree: (Integer) -> Tree?
44
-
45
- # Returns n-th tree from the children
46
- #
47
- # Returns `nil` if the value is not a tree or nil.
48
- #
49
- def nth_tree?: (Integer) -> Tree?
50
-
51
- # Returns n-th tree from the children
52
- #
53
- # Raises if the value is not a tree.
54
- #
55
- def nth_tree!: (Integer) -> Tree
56
-
57
- # Returns n-th type from the children
58
- #
59
- # Raises if the value is not a tree or nil.
60
- #
61
- def nth_type: (Integer) -> Types::t?
62
-
63
- # Returns n-th type from the children
64
- #
65
- # Returns `nil` if the value is not a type.
66
- #
67
- def nth_type?: (Integer) -> Types::t?
68
-
69
- # Returns n-th type from the children
70
- #
71
- # Raises if the value is not a type.
72
- #
73
- def nth_type!: (Integer) -> Types::t
74
-
75
- # Returns n-th method type from the children
76
- #
77
- # Raises if the value is not a method type or `nil`.
78
- #
79
- def nth_method_type: (Integer) -> MethodType?
80
-
81
- # Returns n-th method type from the children
82
- #
83
- # Returns `nil` if the value is not a method type.
84
- #
85
- def nth_method_type?: (Integer) -> MethodType?
86
-
87
- # Returns n-th method tree from the children
88
- #
89
- # Raises if the value is not a method tree.
90
- #
91
- def nth_method_type!: (Integer) -> MethodType
92
-
93
- # Returns the source code associated to the tree
94
- def to_s: () -> String
95
- end
96
- end
97
- end
98
- end
@@ -1,7 +0,0 @@
1
- module RBS
2
- module Inline
3
- module NodeUtils
4
- def type_name: (Prism::Node) -> TypeName?
5
- end
6
- end
7
- end
@@ -1,27 +0,0 @@
1
- module RBS
2
- module Inline
3
- class Writer
4
- attr_reader output: String
5
-
6
- attr_reader writer: RBS::Writer
7
-
8
- def self.write: (Array[AST::Annotations::Use], Array[AST::Declarations::t]) -> String
9
-
10
- def initialize: (?String) -> void
11
-
12
- def write: (Array[AST::Annotations::Use], Array[AST::Declarations::t]) -> void
13
-
14
- def header: (*String) -> void
15
-
16
- def translate_decl: (AST::Declarations::t) -> RBS::AST::Declarations::t?
17
-
18
- def translate_class_decl: (AST::Declarations::ClassDecl) -> RBS::AST::Declarations::Class?
19
-
20
- def translate_module_decl: (AST::Declarations::ModuleDecl) -> RBS::AST::Declarations::Module?
21
-
22
- def translate_constant_decl: (AST::Declarations::ConstantDecl) -> RBS::AST::Declarations::Constant?
23
-
24
- def translate_member: (AST::Members::t) -> Array[RBS::AST::Members::t | RBS::AST::Declarations::t]?
25
- end
26
- end
27
- end
data/sig/rbs/inline.rbs DELETED
@@ -1,41 +0,0 @@
1
- module RBS
2
- module Inline
3
- type token = [Symbol, String]
4
-
5
- VERSION: String
6
-
7
- module AST
8
- module Declarations
9
- end
10
-
11
- module Annotations
12
- type t = VarType
13
- | ReturnType
14
- | Use
15
- | Inherits
16
- | Generic
17
- | ModuleSelf
18
- | Skip
19
- | Assertion
20
- | Application
21
- | RBSAnnotation
22
- | Override
23
- | IvarType
24
- | Yields
25
- | Embedded
26
- # | Def
27
- # | AttrReader | AttrWriter | AttrAccessor
28
- # | Include | Extend | Prepend
29
- # | Alias
30
- end
31
-
32
- module Members
33
- type ruby = RubyDef | RubyAlias | RubyMixin | RubyAttr | RubyPublic | RubyPrivate
34
-
35
- type rbs = RBSIvar | RBSEmbedded
36
-
37
- type t = ruby | rbs
38
- end
39
- end
40
- end
41
- end
@@ -1,6 +0,0 @@
1
- require "prism"
2
-
3
- ast = Prism.parse_file("yard-samples/sample1.rb")
4
-
5
- pp ast.value
6
- pp ast.comments
@@ -1,26 +0,0 @@
1
- module Foo
2
- # This is `Foo#foo` method
3
- #
4
- # @param i [Integer] Size of something
5
- # @param j [Symbol,Integer] Something doing meaningful
6
- # @return [String?] Returns a string or nil
7
- #
8
- #
9
- # @rbs.method (Integer, String) -> void
10
- # | [A] () { () [self: String] -> A } -> A?
11
- #
12
- def foo(i, j)
13
-
14
- end
15
-
16
- # @rbs.inline
17
- # attr_reader hoge: String
18
- # attr_reader name: String?
19
- def hoge
20
-
21
- end
22
-
23
- class Foo
24
- # @rbs.inline include Foo[String]
25
- end
26
- end