Skip to content

Commit 503594a

Browse files
authored
Merge pull request #307 from ruby/syntax-update
Syntax update
2 parents 150bc6e + 55fc6db commit 503594a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+3196
-1759
lines changed

Rakefile

+11-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ multitask :default => [:test, :stdlib_test, :rubocop, :validate]
1313

1414
task :validate => :parser do
1515
sh "rbs validate"
16+
17+
FileList["stdlib/*"].each do |path|
18+
next if path =~ %r{stdlib/builtin}
19+
sh "rbs -r#{File.basename(path)} validate"
20+
end
1621
end
1722

1823
FileList["test/stdlib/**/*_test.rb"].each do |test|
@@ -53,8 +58,10 @@ namespace :generate do
5358
def initialize(klass)
5459
@klass = klass
5560

56-
@env = Ruby::Signature::Environment.new
57-
Ruby::Signature::EnvironmentLoader.new.load(env: @env)
61+
@env = Ruby::Signature::Environment.new.yield_self do |env|
62+
Ruby::Signature::EnvironmentLoader.new.load(env: env)
63+
env.resolve_type_names
64+
end
5865
end
5966

6067
def call
@@ -122,13 +129,13 @@ namespace :generate do
122129

123130
def class_methods
124131
@class_methods ||= Ruby::Signature::DefinitionBuilder.new(env: env).build_singleton(type_name).methods.select {|_, definition|
125-
definition.implemented_in.name.absolute! == type_name
132+
definition.implemented_in == type_name
126133
}
127134
end
128135

129136
def instance_methods
130137
@instance_methods ||= Ruby::Signature::DefinitionBuilder.new(env: env).build_instance(type_name).methods.select {|_, definition|
131-
definition.implemented_in.name.absolute! == type_name
138+
definition.implemented_in == type_name
132139
}
133140
end
134141
end

lib/rbs.rb

+2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
require "rbs/prototype/rbi"
3333
require "rbs/prototype/rb"
3434
require "rbs/prototype/runtime"
35+
require "rbs/type_name_resolver"
3536
require "rbs/environment_walker"
3637
require "rbs/vendorer"
38+
require "rbs/validator"
3739

3840
begin
3941
require "rbs/parser"

lib/rbs/ast/declarations.rb

+62-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
module RBS
22
module AST
33
module Declarations
4+
class Base
5+
end
6+
47
class ModuleTypeParams
58
attr_reader :params
69

@@ -77,7 +80,53 @@ def rename_to(names)
7780
end
7881
end
7982

80-
class Class
83+
module NestedDeclarationHelper
84+
def each_member
85+
if block_given?
86+
members.each do |member|
87+
if member.is_a?(Members::Base)
88+
yield member
89+
end
90+
end
91+
else
92+
enum_for :each_member
93+
end
94+
end
95+
96+
def each_decl
97+
if block_given?
98+
members.each do |member|
99+
if member.is_a?(Declarations::Base)
100+
yield member
101+
end
102+
end
103+
else
104+
enum_for :each_decl
105+
end
106+
end
107+
end
108+
109+
module MixinHelper
110+
def each_mixin(&block)
111+
if block_given?
112+
@mixins ||= begin
113+
members.select do |member|
114+
case member
115+
when Members::Include, Members::Extend, Members::Prepend
116+
true
117+
else
118+
false
119+
end
120+
end
121+
end
122+
@mixins.each(&block)
123+
else
124+
enum_for :each_mixin
125+
end
126+
end
127+
end
128+
129+
class Class < Base
81130
class Super
82131
attr_reader :name
83132
attr_reader :args
@@ -105,6 +154,9 @@ def to_json(*a)
105154
end
106155
end
107156

157+
include NestedDeclarationHelper
158+
include MixinHelper
159+
108160
attr_reader :name
109161
attr_reader :type_params
110162
attr_reader :members
@@ -151,7 +203,10 @@ def to_json(*a)
151203
end
152204
end
153205

154-
class Module
206+
class Module < Base
207+
include NestedDeclarationHelper
208+
include MixinHelper
209+
155210
attr_reader :name
156211
attr_reader :type_params
157212
attr_reader :members
@@ -198,7 +253,7 @@ def to_json(*a)
198253
end
199254
end
200255

201-
class Extension
256+
class Extension < Base
202257
attr_reader :name
203258
attr_reader :type_params
204259
attr_reader :extension_name
@@ -245,7 +300,7 @@ def to_json(*a)
245300
end
246301
end
247302

248-
class Interface
303+
class Interface < Base
249304
attr_reader :name
250305
attr_reader :type_params
251306
attr_reader :members
@@ -288,7 +343,7 @@ def to_json(*a)
288343
end
289344
end
290345

291-
class Alias
346+
class Alias < Base
292347
attr_reader :name
293348
attr_reader :type
294349
attr_reader :annotations
@@ -327,7 +382,7 @@ def to_json(*a)
327382
end
328383
end
329384

330-
class Constant
385+
class Constant < Base
331386
attr_reader :name
332387
attr_reader :type
333388
attr_reader :location
@@ -363,7 +418,7 @@ def to_json(*a)
363418
end
364419
end
365420

366-
class Global
421+
class Global < Base
367422
attr_reader :name
368423
attr_reader :type
369424
attr_reader :location

lib/rbs/ast/members.rb

+41-17
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,43 @@
11
module RBS
22
module AST
33
module Members
4-
class MethodDefinition
4+
class Base
5+
end
6+
7+
class MethodDefinition < Base
58
attr_reader :name
69
attr_reader :kind
710
attr_reader :types
811
attr_reader :annotations
912
attr_reader :location
1013
attr_reader :comment
1114
attr_reader :attributes
15+
attr_reader :overload
1216

13-
def initialize(name:, kind:, types:, annotations:, location:, comment:, attributes:)
17+
def initialize(name:, kind:, types:, annotations:, location:, comment:, attributes:, overload:)
1418
@name = name
1519
@kind = kind
1620
@types = types
1721
@annotations = annotations
1822
@location = location
1923
@comment = comment
2024
@attributes = attributes
25+
@overload = overload
2126
end
2227

2328
def ==(other)
2429
other.is_a?(MethodDefinition) &&
2530
other.name == name &&
2631
other.kind == kind &&
2732
other.types == types &&
28-
other.attributes == attributes
33+
other.attributes == attributes &&
34+
other.overload == overload
2935
end
3036

3137
alias eql? ==
3238

3339
def hash
34-
self.class.hash ^ name.hash ^ kind.hash ^ types.hash ^ attributes.hash
40+
self.class.hash ^ name.hash ^ kind.hash ^ types.hash ^ attributes.hash ^ overload.hash
3541
end
3642

3743
def instance?
@@ -42,6 +48,23 @@ def singleton?
4248
kind == :singleton || kind == :singleton_instance
4349
end
4450

51+
def overload?
52+
overload
53+
end
54+
55+
def update(name: self.name, kind: self.kind, types: self.types, annotations: self.annotations, location: self.location, comment: self.comment, attributes: self.attributes, overload: self.overload)
56+
self.class.new(
57+
name: name,
58+
kind: kind,
59+
types: types,
60+
annotations: annotations,
61+
location: location,
62+
comment: comment,
63+
attributes: attributes,
64+
overload: overload
65+
)
66+
end
67+
4568
def to_json(*a)
4669
{
4770
member: :method_definition,
@@ -50,7 +73,8 @@ def to_json(*a)
5073
annotations: annotations,
5174
location: location,
5275
comment: comment,
53-
attributes: attributes
76+
attributes: attributes,
77+
overload: overload
5478
}.to_json(*a)
5579
end
5680
end
@@ -79,7 +103,7 @@ def hash
79103
end
80104
end
81105

82-
class InstanceVariable
106+
class InstanceVariable < Base
83107
include Var
84108

85109
def to_json(*a)
@@ -93,7 +117,7 @@ def to_json(*a)
93117
end
94118
end
95119

96-
class ClassInstanceVariable
120+
class ClassInstanceVariable < Base
97121
include Var
98122

99123
def to_json(*a)
@@ -107,7 +131,7 @@ def to_json(*a)
107131
end
108132
end
109133

110-
class ClassVariable
134+
class ClassVariable < Base
111135
include Var
112136

113137
def to_json(*a)
@@ -149,7 +173,7 @@ def hash
149173
end
150174
end
151175

152-
class Include
176+
class Include < Base
153177
include Mixin
154178

155179
def to_json(*a)
@@ -164,7 +188,7 @@ def to_json(*a)
164188
end
165189
end
166190

167-
class Extend
191+
class Extend < Base
168192
include Mixin
169193

170194
def to_json(*a)
@@ -179,7 +203,7 @@ def to_json(*a)
179203
end
180204
end
181205

182-
class Prepend
206+
class Prepend < Base
183207
include Mixin
184208

185209
def to_json(*a)
@@ -225,7 +249,7 @@ def hash
225249
end
226250
end
227251

228-
class AttrReader
252+
class AttrReader < Base
229253
include Attribute
230254

231255
def to_json(*a)
@@ -241,7 +265,7 @@ def to_json(*a)
241265
end
242266
end
243267

244-
class AttrAccessor
268+
class AttrAccessor < Base
245269
include Attribute
246270

247271
def to_json(*a)
@@ -257,7 +281,7 @@ def to_json(*a)
257281
end
258282
end
259283

260-
class AttrWriter
284+
class AttrWriter < Base
261285
include Attribute
262286

263287
def to_json(*a)
@@ -291,23 +315,23 @@ def hash
291315
end
292316
end
293317

294-
class Public
318+
class Public < Base
295319
include LocationOnly
296320

297321
def to_json(*a)
298322
{ member: :public, location: location }.to_json(*a)
299323
end
300324
end
301325

302-
class Private
326+
class Private < Base
303327
include LocationOnly
304328

305329
def to_json(*a)
306330
{ member: :private, location: location }.to_json(*a)
307331
end
308332
end
309333

310-
class Alias
334+
class Alias < Base
311335
attr_reader :new_name
312336
attr_reader :old_name
313337
attr_reader :kind

0 commit comments

Comments
 (0)