Skip to content

Commit dcae705

Browse files
committed
A little bit of Sorbet
1 parent d23fc71 commit dcae705

File tree

8 files changed

+165
-44
lines changed

8 files changed

+165
-44
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/coverage/
55
/pkg/
66
/rdocs/
7+
/sorbet/
78
/spec/reports/
89
/tmp/
910
/vendor/

Diff for: .rubocop.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ AllCops:
77
SuggestExtensions: false
88
TargetRubyVersion: 2.7
99
Exclude:
10-
- '{.git,.github,bin,coverage,pkg,spec,test/fixtures,vendor,tmp}/**/*'
10+
- '{.git,.github,bin,coverage,pkg,sorbet,spec,test/fixtures,vendor,tmp}/**/*'
1111
- test.rb
1212

1313
Gemspec/DevelopmentDependencies:

Diff for: .ruby-version

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.2.0

Diff for: lib/syntax_tree/node.rb

+64-32
Original file line numberDiff line numberDiff line change
@@ -792,9 +792,10 @@ def arity
792792
private
793793

794794
def trailing_comma?
795+
arguments = self.arguments
795796
return false unless arguments.is_a?(Args)
796-
parts = arguments.parts
797797

798+
parts = arguments.parts
798799
if parts.last.is_a?(ArgBlock)
799800
# If the last argument is a block, then we can't put a trailing comma
800801
# after it without resulting in a syntax error.
@@ -1188,8 +1189,11 @@ def deconstruct_keys(_keys)
11881189
end
11891190

11901191
def format(q)
1191-
if lbracket.comments.empty? && contents && contents.comments.empty? &&
1192-
contents.parts.length > 1
1192+
lbracket = self.lbracket
1193+
contents = self.contents
1194+
1195+
if lbracket.is_a?(LBracket) && lbracket.comments.empty? && contents &&
1196+
contents.comments.empty? && contents.parts.length > 1
11931197
if qwords?
11941198
QWordsFormatter.new(contents).format(q)
11951199
return
@@ -2091,6 +2095,7 @@ def deconstruct_keys(_keys)
20912095
end
20922096

20932097
def format(q)
2098+
left = self.left
20942099
power = operator == :**
20952100

20962101
q.group do
@@ -2307,6 +2312,8 @@ def initialize(
23072312
end
23082313

23092314
def bind(parser, start_char, start_column, end_char, end_column)
2315+
rescue_clause = self.rescue_clause
2316+
23102317
@location =
23112318
Location.new(
23122319
start_line: location.start_line,
@@ -2330,6 +2337,7 @@ def bind(parser, start_char, start_column, end_char, end_column)
23302337
# Next we're going to determine the rescue clause if there is one
23312338
if rescue_clause
23322339
consequent = else_clause || ensure_clause
2340+
23332341
rescue_clause.bind_end(
23342342
consequent ? consequent.location.start_char : end_char,
23352343
consequent ? consequent.location.start_column : end_column
@@ -2735,7 +2743,7 @@ def format(q)
27352743
children << receiver
27362744
end
27372745
when MethodAddBlock
2738-
if receiver.call.is_a?(CallNode) && !receiver.call.receiver.nil?
2746+
if (call = receiver.call).is_a?(CallNode) && !call.receiver.nil?
27392747
children << receiver
27402748
else
27412749
break
@@ -2744,8 +2752,8 @@ def format(q)
27442752
break
27452753
end
27462754
when MethodAddBlock
2747-
if child.call.is_a?(CallNode) && !child.call.receiver.nil?
2748-
children << child.call
2755+
if (call = child.call).is_a?(CallNode) && !call.receiver.nil?
2756+
children << call
27492757
else
27502758
break
27512759
end
@@ -2767,8 +2775,8 @@ def format(q)
27672775
# of just Statements nodes.
27682776
parent = parents[3] if parent.is_a?(BlockNode) && parent.keywords?
27692777

2770-
if parent.is_a?(MethodAddBlock) && parent.call.is_a?(CallNode) &&
2771-
parent.call.message.value == "sig"
2778+
if parent.is_a?(MethodAddBlock) &&
2779+
(call = parent.call).is_a?(CallNode) && call.message.value == "sig"
27722780
threshold = 2
27732781
end
27742782
end
@@ -2813,10 +2821,10 @@ def format_chain(q, children)
28132821

28142822
while (child = children.pop)
28152823
if child.is_a?(CallNode)
2816-
if child.receiver.is_a?(CallNode) &&
2817-
(child.receiver.message != :call) &&
2818-
(child.receiver.message.value == "where") &&
2819-
(child.message.value == "not")
2824+
if (receiver = child.receiver).is_a?(CallNode) &&
2825+
(receiver.message != :call) &&
2826+
(receiver.message.value == "where") &&
2827+
(message.value == "not")
28202828
# This is very specialized behavior wherein we group
28212829
# .where.not calls together because it looks better. For more
28222830
# information, see
@@ -2872,7 +2880,8 @@ def self.chained?(node)
28722880
when CallNode
28732881
!node.receiver.nil?
28742882
when MethodAddBlock
2875-
node.call.is_a?(CallNode) && !node.call.receiver.nil?
2883+
call = node.call
2884+
call.is_a?(CallNode) && !call.receiver.nil?
28762885
else
28772886
false
28782887
end
@@ -3629,6 +3638,10 @@ def deconstruct_keys(_keys)
36293638
end
36303639

36313640
def format(q)
3641+
message = self.message
3642+
arguments = self.arguments
3643+
block = self.block
3644+
36323645
q.group do
36333646
doc =
36343647
q.nest(0) do
@@ -3637,7 +3650,7 @@ def format(q)
36373650
# If there are leading comments on the message then we know we have
36383651
# a newline in the source that is forcing these things apart. In
36393652
# this case we will have to use a trailing operator.
3640-
if message.comments.any?(&:leading?)
3653+
if message != :call && message.comments.any?(&:leading?)
36413654
q.format(CallOperatorFormatter.new(operator), stackable: false)
36423655
q.indent do
36433656
q.breakable_empty
@@ -4153,6 +4166,9 @@ def deconstruct_keys(_keys)
41534166
end
41544167

41554168
def format(q)
4169+
params = self.params
4170+
bodystmt = self.bodystmt
4171+
41564172
q.group do
41574173
q.group do
41584174
q.text("def")
@@ -4209,6 +4225,8 @@ def endless?
42094225
end
42104226

42114227
def arity
4228+
params = self.params
4229+
42124230
case params
42134231
when Params
42144232
params.arity
@@ -5293,6 +5311,7 @@ def accept(visitor)
52935311
end
52945312

52955313
def child_nodes
5314+
operator = self.operator
52965315
[parent, (operator if operator != :"::"), name]
52975316
end
52985317

@@ -5674,7 +5693,7 @@ def accept(visitor)
56745693
end
56755694

56765695
def child_nodes
5677-
[lbrace] + assocs
5696+
[lbrace].concat(assocs)
56785697
end
56795698

56805699
def copy(lbrace: nil, assocs: nil, location: nil)
@@ -5766,7 +5785,7 @@ class Heredoc < Node
57665785
# [Array[ Comment | EmbDoc ]] the comments attached to this node
57675786
attr_reader :comments
57685787

5769-
def initialize(beginning:, ending: nil, dedent: 0, parts: [], location:)
5788+
def initialize(beginning:, location:, ending: nil, dedent: 0, parts: [])
57705789
@beginning = beginning
57715790
@ending = ending
57725791
@dedent = dedent
@@ -6134,6 +6153,8 @@ def ===(other)
61346153
private
61356154

61366155
def format_contents(q, parts, nested)
6156+
keyword_rest = self.keyword_rest
6157+
61376158
q.group { q.seplist(parts) { |part| q.format(part, stackable: false) } }
61386159

61396160
# If there isn't a constant, and there's a blank keyword_rest, then we
@@ -6763,6 +6784,8 @@ def deconstruct_keys(_keys)
67636784

67646785
def format(q)
67656786
keyword = "in "
6787+
pattern = self.pattern
6788+
consequent = self.consequent
67666789

67676790
q.group do
67686791
q.text(keyword)
@@ -7165,6 +7188,8 @@ def deconstruct_keys(_keys)
71657188
end
71667189

71677190
def format(q)
7191+
params = self.params
7192+
71687193
q.text("->")
71697194
q.group do
71707195
if params.is_a?(Paren)
@@ -7643,7 +7668,7 @@ class MLHS < Node
76437668
# [Array[ Comment | EmbDoc ]] the comments attached to this node
76447669
attr_reader :comments
76457670

7646-
def initialize(parts:, comma: false, location:)
7671+
def initialize(parts:, location:, comma: false)
76477672
@parts = parts
76487673
@comma = comma
76497674
@location = location
@@ -7704,7 +7729,7 @@ class MLHSParen < Node
77047729
# [Array[ Comment | EmbDoc ]] the comments attached to this node
77057730
attr_reader :comments
77067731

7707-
def initialize(contents:, comma: false, location:)
7732+
def initialize(contents:, location:, comma: false)
77087733
@contents = contents
77097734
@comma = comma
77107735
@location = location
@@ -8287,14 +8312,14 @@ def format(q)
82878312
attr_reader :comments
82888313

82898314
def initialize(
8315+
location:,
82908316
requireds: [],
82918317
optionals: [],
82928318
rest: nil,
82938319
posts: [],
82948320
keywords: [],
82958321
keyword_rest: nil,
8296-
block: nil,
8297-
location:
8322+
block: nil
82988323
)
82998324
@requireds = requireds
83008325
@optionals = optionals
@@ -8321,6 +8346,8 @@ def accept(visitor)
83218346
end
83228347

83238348
def child_nodes
8349+
keyword_rest = self.keyword_rest
8350+
83248351
[
83258352
*requireds,
83268353
*optionals.flatten(1),
@@ -8375,16 +8402,19 @@ def deconstruct_keys(_keys)
83758402
end
83768403

83778404
def format(q)
8405+
rest = self.rest
8406+
keyword_rest = self.keyword_rest
8407+
83788408
parts = [
83798409
*requireds,
83808410
*optionals.map { |(name, value)| OptionalFormatter.new(name, value) }
83818411
]
83828412

83838413
parts << rest if rest && !rest.is_a?(ExcessedComma)
8384-
parts += [
8385-
*posts,
8386-
*keywords.map { |(name, value)| KeywordFormatter.new(name, value) }
8387-
]
8414+
parts.concat(posts)
8415+
parts.concat(
8416+
keywords.map { |(name, value)| KeywordFormatter.new(name, value) }
8417+
)
83888418

83898419
parts << KeywordRestFormatter.new(keyword_rest) if keyword_rest
83908420
parts << block if block
@@ -8511,6 +8541,8 @@ def deconstruct_keys(_keys)
85118541
end
85128542

85138543
def format(q)
8544+
contents = self.contents
8545+
85148546
q.group do
85158547
q.format(lparen)
85168548

@@ -9425,11 +9457,11 @@ def bind_end(end_char, end_column)
94259457
end_column: end_column
94269458
)
94279459

9428-
if consequent
9429-
consequent.bind_end(end_char, end_column)
9460+
if (next_node = consequent)
9461+
next_node.bind_end(end_char, end_column)
94309462
statements.bind_end(
9431-
consequent.location.start_char,
9432-
consequent.location.start_column
9463+
next_node.location.start_char,
9464+
next_node.location.start_column
94339465
)
94349466
else
94359467
statements.bind_end(end_char, end_column)
@@ -9872,8 +9904,8 @@ def bind(parser, start_char, start_column, end_char, end_column)
98729904
end_column: end_column
98739905
)
98749906

9875-
if body[0].is_a?(VoidStmt)
9876-
location = body[0].location
9907+
if (void_stmt = body[0]).is_a?(VoidStmt)
9908+
location = void_stmt.location
98779909
location =
98789910
Location.new(
98799911
start_line: location.start_line,
@@ -10352,7 +10384,7 @@ def format(q)
1035210384
opening_quote, closing_quote =
1035310385
if !Quotes.locked?(self, q.quote)
1035410386
[q.quote, q.quote]
10355-
elsif quote.start_with?("%")
10387+
elsif quote&.start_with?("%")
1035610388
[quote, Quotes.matching(quote[/%[qQ]?(.)/, 1])]
1035710389
else
1035810390
[quote, quote]
@@ -11521,7 +11553,7 @@ def accept(visitor)
1152111553
end
1152211554

1152311555
def child_nodes
11524-
[value]
11556+
value == :nil ? [] : [value]
1152511557
end
1152611558

1152711559
def copy(value: nil, location: nil)

Diff for: lib/syntax_tree/reflection.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,11 @@ def parse_comments(statements, index)
183183
next unless main_statement.is_a?(SyntaxTree::ClassDeclaration)
184184

185185
# Ensure we're looking at class declarations with superclasses.
186-
next unless main_statement.superclass.is_a?(SyntaxTree::VarRef)
186+
superclass = main_statement.superclass
187+
next unless superclass.is_a?(SyntaxTree::VarRef)
187188

188189
# Ensure we're looking at class declarations that inherit from Node.
189-
next unless main_statement.superclass.value.value == "Node"
190+
next unless superclass.value.value == "Node"
190191

191192
# All child nodes inherit the location attr_reader from Node, so we'll add
192193
# that to the list of attributes first.

Diff for: lib/syntax_tree/yarv/instruction_sequence.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def initialize
5050
@tail_node = nil
5151
end
5252

53-
def each
53+
def each(&_blk)
5454
return to_enum(__method__) unless block_given?
5555
each_node { |node| yield node.value }
5656
end

0 commit comments

Comments
 (0)