Skip to content

Commit 42572ac

Browse files
committed
Generate sorbet types in a rake task
1 parent 45d8c4c commit 42572ac

File tree

8 files changed

+349
-29
lines changed

8 files changed

+349
-29
lines changed

Rakefile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@ task default: :test
1616

1717
configure = ->(task) do
1818
task.source_files =
19-
FileList[%w[Gemfile Rakefile syntax_tree.gemspec lib/**/*.rb test/*.rb]]
19+
FileList[
20+
%w[
21+
Gemfile
22+
Rakefile
23+
syntax_tree.gemspec
24+
lib/**/*.rb
25+
tasks/*.rake
26+
test/*.rb
27+
]
28+
]
2029

2130
# Since Syntax Tree supports back to Ruby 2.7.0, we need to make sure that we
2231
# format our code such that it's compatible with that version. This actually

lib/syntax_tree/dsl.rb

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,17 @@ def RAssign(value, operator, pattern)
210210
end
211211

212212
# Create a new ClassDeclaration node.
213-
def ClassDeclaration(constant, superclass, bodystmt)
213+
def ClassDeclaration(
214+
constant,
215+
superclass,
216+
bodystmt,
217+
location = Location.default
218+
)
214219
ClassDeclaration.new(
215220
constant: constant,
216221
superclass: superclass,
217222
bodystmt: bodystmt,
218-
location: Location.default
223+
location: location
219224
)
220225
end
221226

@@ -225,12 +230,12 @@ def Comma(value)
225230
end
226231

227232
# Create a new Command node.
228-
def Command(message, arguments, block)
233+
def Command(message, arguments, block, location = Location.default)
229234
Command.new(
230235
message: message,
231236
arguments: arguments,
232237
block: block,
233-
location: Location.default
238+
location: location
234239
)
235240
end
236241

@@ -247,8 +252,8 @@ def CommandCall(receiver, operator, message, arguments, block)
247252
end
248253

249254
# Create a new Comment node.
250-
def Comment(value, inline)
251-
Comment.new(value: value, inline: inline, location: Location.default)
255+
def Comment(value, inline, location = Location.default)
256+
Comment.new(value: value, inline: inline, location: location)
252257
end
253258

254259
# Create a new Const node.
@@ -285,14 +290,21 @@ def CVar(value)
285290
end
286291

287292
# Create a new DefNode node.
288-
def DefNode(target, operator, name, params, bodystmt)
293+
def DefNode(
294+
target,
295+
operator,
296+
name,
297+
params,
298+
bodystmt,
299+
location = Location.default
300+
)
289301
DefNode.new(
290302
target: target,
291303
operator: operator,
292304
name: name,
293305
params: params,
294306
bodystmt: bodystmt,
295-
location: Location.default
307+
location: location
296308
)
297309
end
298310

@@ -565,8 +577,8 @@ def MAssign(target, value)
565577
end
566578

567579
# Create a new MethodAddBlock node.
568-
def MethodAddBlock(call, block)
569-
MethodAddBlock.new(call: call, block: block, location: Location.default)
580+
def MethodAddBlock(call, block, location = Location.default)
581+
MethodAddBlock.new(call: call, block: block, location: location)
570582
end
571583

572584
# Create a new MLHS node.

lib/syntax_tree/parser.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,12 @@ def on_bodystmt(statements, rescue_clause, else_clause, ensure_clause)
912912
# in the case that we're inside of an endless method definition. In this
913913
# case we'll wrap it in a Statements node to be consistent.
914914
unless statements.is_a?(Statements)
915-
statements = Statements.new(self, body: [statements], location: statements.location)
915+
statements =
916+
Statements.new(
917+
self,
918+
body: [statements],
919+
location: statements.location
920+
)
916921
end
917922

918923
parts = [statements, rescue_clause, else_clause, ensure_clause].compact
@@ -1894,14 +1899,16 @@ def on_hshptn(constant, keywords, keyword_rest)
18941899
else
18951900
tstring_beg_index =
18961901
tokens.rindex do |token|
1897-
token.is_a?(TStringBeg) && token.location.start_char < label.location.start_char
1902+
token.is_a?(TStringBeg) &&
1903+
token.location.start_char < label.location.start_char
18981904
end
18991905

19001906
tstring_beg = tokens.delete_at(tstring_beg_index)
19011907

19021908
label_end_index =
19031909
tokens.rindex do |token|
1904-
token.is_a?(LabelEnd) && token.location.start_char == label.location.end_char
1910+
token.is_a?(LabelEnd) &&
1911+
token.location.start_char == label.location.end_char
19051912
end
19061913

19071914
label_end = tokens.delete_at(label_end_index)
@@ -1913,7 +1920,7 @@ def on_hshptn(constant, keywords, keyword_rest)
19131920
location: tstring_beg.location.to(label_end.location)
19141921
),
19151922
value
1916-
]
1923+
]
19171924
end
19181925
end
19191926

lib/syntax_tree/reflection.rb

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ class TupleType
3434
def initialize(types)
3535
@types = types
3636
end
37-
37+
3838
def ===(value)
3939
value.is_a?(Array) && value.length == types.length &&
40-
value.zip(types).all? { _2 === _1 }
40+
value.zip(types).all? { |item, type| type === item }
4141
end
4242

4343
def inspect
@@ -64,16 +64,20 @@ def inspect
6464

6565
class << self
6666
def parse(comment)
67+
comment = comment.gsub(/\n/, " ")
68+
6769
unless comment.start_with?("[")
6870
raise "Comment does not start with a bracket: #{comment.inspect}"
6971
end
7072

7173
count = 1
7274
found =
73-
comment.chars[1..].find.with_index(1) do |char, index|
74-
count += { "[" => 1, "]" => -1 }.fetch(char, 0)
75-
break index if count == 0
76-
end
75+
comment.chars[1..]
76+
.find
77+
.with_index(1) do |char, index|
78+
count += { "[" => 1, "]" => -1 }.fetch(char, 0)
79+
break index if count == 0
80+
end
7781

7882
# If we weren't able to find the end of the balanced brackets, then
7983
# the comment is malformed.
@@ -209,7 +213,7 @@ def parse_comments(statements, index)
209213
attribute =
210214
Attribute.new(
211215
statement.arguments.parts.first.value.value.to_sym,
212-
parse_comments(statements, statement_index).join(" ")
216+
"#{parse_comments(statements, statement_index).join("\n")}\n"
213217
)
214218

215219
# Ensure that we don't already have an attribute named the same as
@@ -223,10 +227,11 @@ def parse_comments(statements, index)
223227
end
224228

225229
# Finally, set it up in the hash of nodes so that we can use it later.
230+
comments = parse_comments(main_statements, main_statement_index)
226231
node =
227232
Node.new(
228233
main_statement.constant.constant.value.to_sym,
229-
parse_comments(main_statements, main_statement_index).join("\n"),
234+
"#{comments.join("\n")}\n",
230235
attributes
231236
)
232237

lib/syntax_tree/translation/parser.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2364,8 +2364,7 @@ def visit_statements(node)
23642364

23652365
# Visit a StringConcat node.
23662366
def visit_string_concat(node)
2367-
location =
2368-
source_map_collection(expression: source_range_node(node))
2367+
location = source_map_collection(expression: source_range_node(node))
23692368

23702369
s(:dstr, [visit(node.left), visit(node.right)], location)
23712370
end

lib/syntax_tree/yarv/compiler.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,11 +1050,21 @@ def visit_if_op(node)
10501050
visit_if(
10511051
IfNode.new(
10521052
predicate: node.predicate,
1053-
statements: Statements.new(nil, body: [node.truthy], location: Location.default),
1053+
statements:
1054+
Statements.new(
1055+
nil,
1056+
body: [node.truthy],
1057+
location: Location.default
1058+
),
10541059
consequent:
10551060
Else.new(
10561061
keyword: Kw.new(value: "else", location: Location.default),
1057-
statements: Statements.new(nil, body: [node.falsy], location: Location.default),
1062+
statements:
1063+
Statements.new(
1064+
nil,
1065+
body: [node.falsy],
1066+
location: Location.default
1067+
),
10581068
location: Location.default
10591069
),
10601070
location: Location.default

0 commit comments

Comments
 (0)