syntax_tree 4.3.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -421,11 +421,11 @@ module SyntaxTree
421
421
  # on_alias: (
422
422
  # (DynaSymbol | SymbolLiteral) left,
423
423
  # (DynaSymbol | SymbolLiteral) right
424
- # ) -> Alias
424
+ # ) -> AliasNode
425
425
  def on_alias(left, right)
426
426
  keyword = consume_keyword(:alias)
427
427
 
428
- Alias.new(
428
+ AliasNode.new(
429
429
  left: left,
430
430
  right: right,
431
431
  location: keyword.location.to(right.location)
@@ -575,7 +575,7 @@ module SyntaxTree
575
575
  def on_args_forward
576
576
  op = consume_operator(:"...")
577
577
 
578
- ArgsForward.new(value: op.value, location: op.location)
578
+ ArgsForward.new(location: op.location)
579
579
  end
580
580
 
581
581
  # :call-seq:
@@ -920,7 +920,7 @@ module SyntaxTree
920
920
  # on_brace_block: (
921
921
  # (nil | BlockVar) block_var,
922
922
  # Statements statements
923
- # ) -> BraceBlock
923
+ # ) -> BlockNode
924
924
  def on_brace_block(block_var, statements)
925
925
  lbrace = consume_token(LBrace)
926
926
  rbrace = consume_token(RBrace)
@@ -947,10 +947,10 @@ module SyntaxTree
947
947
  end_column: rbrace.location.end_column
948
948
  )
949
949
 
950
- BraceBlock.new(
951
- lbrace: lbrace,
950
+ BlockNode.new(
951
+ opening: lbrace,
952
952
  block_var: block_var,
953
- statements: statements,
953
+ bodystmt: statements,
954
954
  location: location
955
955
  )
956
956
  end
@@ -971,7 +971,7 @@ module SyntaxTree
971
971
  # untyped receiver,
972
972
  # (:"::" | Op | Period) operator,
973
973
  # (:call | Backtick | Const | Ident | Op) message
974
- # ) -> Call
974
+ # ) -> CallNode
975
975
  def on_call(receiver, operator, message)
976
976
  ending =
977
977
  if message != :call
@@ -982,7 +982,7 @@ module SyntaxTree
982
982
  receiver
983
983
  end
984
984
 
985
- Call.new(
985
+ CallNode.new(
986
986
  receiver: receiver,
987
987
  operator: operator,
988
988
  message: message,
@@ -1076,6 +1076,7 @@ module SyntaxTree
1076
1076
  Command.new(
1077
1077
  message: message,
1078
1078
  arguments: arguments,
1079
+ block: nil,
1079
1080
  location: message.location.to(arguments.location)
1080
1081
  )
1081
1082
  end
@@ -1095,6 +1096,7 @@ module SyntaxTree
1095
1096
  operator: operator,
1096
1097
  message: message,
1097
1098
  arguments: arguments,
1099
+ block: nil,
1098
1100
  location: receiver.location.to(ending.location)
1099
1101
  )
1100
1102
  end
@@ -1181,7 +1183,7 @@ module SyntaxTree
1181
1183
  # (Backtick | Const | Ident | Kw | Op) name,
1182
1184
  # (nil | Params | Paren) params,
1183
1185
  # untyped bodystmt
1184
- # ) -> Def | DefEndless
1186
+ # ) -> DefNode
1185
1187
  def on_def(name, params, bodystmt)
1186
1188
  # Make sure to delete this token in case you're defining something like
1187
1189
  # def class which would lead to this being a kw and causing all kinds of
@@ -1223,7 +1225,9 @@ module SyntaxTree
1223
1225
  ending.location.start_column
1224
1226
  )
1225
1227
 
1226
- Def.new(
1228
+ DefNode.new(
1229
+ target: nil,
1230
+ operator: nil,
1227
1231
  name: name,
1228
1232
  params: params,
1229
1233
  bodystmt: bodystmt,
@@ -1234,12 +1238,12 @@ module SyntaxTree
1234
1238
  # the statements list. Before, it was just the individual statement.
1235
1239
  statement = bodystmt.is_a?(BodyStmt) ? bodystmt.statements : bodystmt
1236
1240
 
1237
- DefEndless.new(
1241
+ DefNode.new(
1238
1242
  target: nil,
1239
1243
  operator: nil,
1240
1244
  name: name,
1241
- paren: params,
1242
- statement: statement,
1245
+ params: params,
1246
+ bodystmt: statement,
1243
1247
  location: beginning.location.to(bodystmt.location)
1244
1248
  )
1245
1249
  end
@@ -1270,7 +1274,7 @@ module SyntaxTree
1270
1274
  # (Backtick | Const | Ident | Kw | Op) name,
1271
1275
  # (Params | Paren) params,
1272
1276
  # BodyStmt bodystmt
1273
- # ) -> Defs
1277
+ # ) -> DefNode
1274
1278
  def on_defs(target, operator, name, params, bodystmt)
1275
1279
  # Make sure to delete this token in case you're defining something
1276
1280
  # like def class which would lead to this being a kw and causing all kinds
@@ -1309,7 +1313,7 @@ module SyntaxTree
1309
1313
  ending.location.start_column
1310
1314
  )
1311
1315
 
1312
- Defs.new(
1316
+ DefNode.new(
1313
1317
  target: target,
1314
1318
  operator: operator,
1315
1319
  name: name,
@@ -1322,19 +1326,19 @@ module SyntaxTree
1322
1326
  # the statements list. Before, it was just the individual statement.
1323
1327
  statement = bodystmt.is_a?(BodyStmt) ? bodystmt.statements : bodystmt
1324
1328
 
1325
- DefEndless.new(
1329
+ DefNode.new(
1326
1330
  target: target,
1327
1331
  operator: operator,
1328
1332
  name: name,
1329
- paren: params,
1330
- statement: statement,
1333
+ params: params,
1334
+ bodystmt: statement,
1331
1335
  location: beginning.location.to(bodystmt.location)
1332
1336
  )
1333
1337
  end
1334
1338
  end
1335
1339
 
1336
1340
  # :call-seq:
1337
- # on_do_block: (BlockVar block_var, BodyStmt bodystmt) -> DoBlock
1341
+ # on_do_block: (BlockVar block_var, BodyStmt bodystmt) -> BlockNode
1338
1342
  def on_do_block(block_var, bodystmt)
1339
1343
  beginning = consume_keyword(:do)
1340
1344
  ending = consume_keyword(:end)
@@ -1348,8 +1352,8 @@ module SyntaxTree
1348
1352
  ending.location.start_column
1349
1353
  )
1350
1354
 
1351
- DoBlock.new(
1352
- keyword: beginning,
1355
+ BlockNode.new(
1356
+ opening: beginning,
1353
1357
  block_var: block_var,
1354
1358
  bodystmt: bodystmt,
1355
1359
  location: beginning.location.to(ending.location)
@@ -1357,30 +1361,32 @@ module SyntaxTree
1357
1361
  end
1358
1362
 
1359
1363
  # :call-seq:
1360
- # on_dot2: ((nil | untyped) left, (nil | untyped) right) -> Dot2
1364
+ # on_dot2: ((nil | untyped) left, (nil | untyped) right) -> RangeNode
1361
1365
  def on_dot2(left, right)
1362
1366
  operator = consume_operator(:"..")
1363
1367
 
1364
1368
  beginning = left || operator
1365
1369
  ending = right || operator
1366
1370
 
1367
- Dot2.new(
1371
+ RangeNode.new(
1368
1372
  left: left,
1373
+ operator: operator,
1369
1374
  right: right,
1370
1375
  location: beginning.location.to(ending.location)
1371
1376
  )
1372
1377
  end
1373
1378
 
1374
1379
  # :call-seq:
1375
- # on_dot3: ((nil | untyped) left, (nil | untyped) right) -> Dot3
1380
+ # on_dot3: ((nil | untyped) left, (nil | untyped) right) -> RangeNode
1376
1381
  def on_dot3(left, right)
1377
1382
  operator = consume_operator(:"...")
1378
1383
 
1379
1384
  beginning = left || operator
1380
1385
  ending = right || operator
1381
1386
 
1382
- Dot3.new(
1387
+ RangeNode.new(
1383
1388
  left: left,
1389
+ operator: operator,
1384
1390
  right: right,
1385
1391
  location: beginning.location.to(ending.location)
1386
1392
  )
@@ -1608,9 +1614,15 @@ module SyntaxTree
1608
1614
  end
1609
1615
 
1610
1616
  # :call-seq:
1611
- # on_fcall: ((Const | Ident) value) -> FCall
1617
+ # on_fcall: ((Const | Ident) value) -> CallNode
1612
1618
  def on_fcall(value)
1613
- FCall.new(value: value, arguments: nil, location: value.location)
1619
+ CallNode.new(
1620
+ receiver: nil,
1621
+ operator: nil,
1622
+ message: value,
1623
+ arguments: nil,
1624
+ location: value.location
1625
+ )
1614
1626
  end
1615
1627
 
1616
1628
  # :call-seq:
@@ -1878,7 +1890,7 @@ module SyntaxTree
1878
1890
  # untyped predicate,
1879
1891
  # Statements statements,
1880
1892
  # (nil | Elsif | Else) consequent
1881
- # ) -> If
1893
+ # ) -> IfNode
1882
1894
  def on_if(predicate, statements, consequent)
1883
1895
  beginning = consume_keyword(:if)
1884
1896
  ending = consequent || consume_keyword(:end)
@@ -1891,7 +1903,7 @@ module SyntaxTree
1891
1903
  ending.location.start_column
1892
1904
  )
1893
1905
 
1894
- If.new(
1906
+ IfNode.new(
1895
1907
  predicate: predicate,
1896
1908
  statements: statements,
1897
1909
  consequent: consequent,
@@ -1911,13 +1923,15 @@ module SyntaxTree
1911
1923
  end
1912
1924
 
1913
1925
  # :call-seq:
1914
- # on_if_mod: (untyped predicate, untyped statement) -> IfMod
1926
+ # on_if_mod: (untyped predicate, untyped statement) -> IfNode
1915
1927
  def on_if_mod(predicate, statement)
1916
1928
  consume_keyword(:if)
1917
1929
 
1918
- IfMod.new(
1919
- statement: statement,
1930
+ IfNode.new(
1920
1931
  predicate: predicate,
1932
+ statements:
1933
+ Statements.new(self, body: [statement], location: statement.location),
1934
+ consequent: nil,
1921
1935
  location: statement.location.to(predicate.location)
1922
1936
  )
1923
1937
  end
@@ -2104,17 +2118,20 @@ module SyntaxTree
2104
2118
  location = params.contents.location
2105
2119
  location = location.to(locals.last.location) if locals.any?
2106
2120
 
2107
- Paren.new(
2108
- lparen: params.lparen,
2109
- contents:
2110
- LambdaVar.new(
2111
- params: params.contents,
2112
- locals: locals,
2113
- location: location
2114
- ),
2115
- location: params.location,
2116
- comments: params.comments
2117
- )
2121
+ node =
2122
+ Paren.new(
2123
+ lparen: params.lparen,
2124
+ contents:
2125
+ LambdaVar.new(
2126
+ params: params.contents,
2127
+ locals: locals,
2128
+ location: location
2129
+ ),
2130
+ location: params.location
2131
+ )
2132
+
2133
+ node.comments.concat(params.comments)
2134
+ node
2118
2135
  when Params
2119
2136
  # In this case we've gotten to the <3.2 plain set of parameters. In
2120
2137
  # this case there cannot be lambda locals, so we will wrap the
@@ -2302,37 +2319,42 @@ module SyntaxTree
2302
2319
 
2303
2320
  # :call-seq:
2304
2321
  # on_method_add_arg: (
2305
- # (Call | FCall) call,
2322
+ # CallNode call,
2306
2323
  # (ArgParen | Args) arguments
2307
- # ) -> Call | FCall
2324
+ # ) -> CallNode
2308
2325
  def on_method_add_arg(call, arguments)
2309
2326
  location = call.location
2310
2327
  location = location.to(arguments.location) if arguments.is_a?(ArgParen)
2311
2328
 
2312
- if call.is_a?(FCall)
2313
- FCall.new(value: call.value, arguments: arguments, location: location)
2314
- else
2315
- Call.new(
2316
- receiver: call.receiver,
2317
- operator: call.operator,
2318
- message: call.message,
2319
- arguments: arguments,
2320
- location: location
2321
- )
2322
- end
2329
+ CallNode.new(
2330
+ receiver: call.receiver,
2331
+ operator: call.operator,
2332
+ message: call.message,
2333
+ arguments: arguments,
2334
+ location: location
2335
+ )
2323
2336
  end
2324
2337
 
2325
2338
  # :call-seq:
2326
2339
  # on_method_add_block: (
2327
- # (Call | Command | CommandCall | FCall) call,
2328
- # (BraceBlock | DoBlock) block
2340
+ # (Call | Command | CommandCall) call,
2341
+ # Block block
2329
2342
  # ) -> MethodAddBlock
2330
2343
  def on_method_add_block(call, block)
2331
- MethodAddBlock.new(
2332
- call: call,
2333
- block: block,
2334
- location: call.location.to(block.location)
2335
- )
2344
+ location = call.location.to(block.location)
2345
+
2346
+ case call
2347
+ when Command, CommandCall
2348
+ node = call.copy(block: block, location: location)
2349
+ node.comments.concat(call.comments)
2350
+ node
2351
+ else
2352
+ MethodAddBlock.new(
2353
+ call: call,
2354
+ block: block,
2355
+ location: call.location.to(block.location)
2356
+ )
2357
+ end
2336
2358
  end
2337
2359
 
2338
2360
  # :call-seq:
@@ -2900,7 +2922,7 @@ module SyntaxTree
2900
2922
  def on_redo
2901
2923
  keyword = consume_keyword(:redo)
2902
2924
 
2903
- Redo.new(value: keyword.value, location: keyword.location)
2925
+ Redo.new(location: keyword.location)
2904
2926
  end
2905
2927
 
2906
2928
  # :call-seq:
@@ -3066,26 +3088,26 @@ module SyntaxTree
3066
3088
  def on_retry
3067
3089
  keyword = consume_keyword(:retry)
3068
3090
 
3069
- Retry.new(value: keyword.value, location: keyword.location)
3091
+ Retry.new(location: keyword.location)
3070
3092
  end
3071
3093
 
3072
3094
  # :call-seq:
3073
- # on_return: (Args arguments) -> Return
3095
+ # on_return: (Args arguments) -> ReturnNode
3074
3096
  def on_return(arguments)
3075
3097
  keyword = consume_keyword(:return)
3076
3098
 
3077
- Return.new(
3099
+ ReturnNode.new(
3078
3100
  arguments: arguments,
3079
3101
  location: keyword.location.to(arguments.location)
3080
3102
  )
3081
3103
  end
3082
3104
 
3083
3105
  # :call-seq:
3084
- # on_return0: () -> Return0
3106
+ # on_return0: () -> ReturnNode
3085
3107
  def on_return0
3086
3108
  keyword = consume_keyword(:return)
3087
3109
 
3088
- Return0.new(value: keyword.value, location: keyword.location)
3110
+ ReturnNode.new(arguments: nil, location: keyword.location)
3089
3111
  end
3090
3112
 
3091
3113
  # :call-seq:
@@ -3562,7 +3584,7 @@ module SyntaxTree
3562
3584
  # untyped predicate,
3563
3585
  # Statements statements,
3564
3586
  # ((nil | Elsif | Else) consequent)
3565
- # ) -> Unless
3587
+ # ) -> UnlessNode
3566
3588
  def on_unless(predicate, statements, consequent)
3567
3589
  beginning = consume_keyword(:unless)
3568
3590
  ending = consequent || consume_keyword(:end)
@@ -3575,7 +3597,7 @@ module SyntaxTree
3575
3597
  ending.location.start_column
3576
3598
  )
3577
3599
 
3578
- Unless.new(
3600
+ UnlessNode.new(
3579
3601
  predicate: predicate,
3580
3602
  statements: statements,
3581
3603
  consequent: consequent,
@@ -3584,19 +3606,21 @@ module SyntaxTree
3584
3606
  end
3585
3607
 
3586
3608
  # :call-seq:
3587
- # on_unless_mod: (untyped predicate, untyped statement) -> UnlessMod
3609
+ # on_unless_mod: (untyped predicate, untyped statement) -> UnlessNode
3588
3610
  def on_unless_mod(predicate, statement)
3589
3611
  consume_keyword(:unless)
3590
3612
 
3591
- UnlessMod.new(
3592
- statement: statement,
3613
+ UnlessNode.new(
3593
3614
  predicate: predicate,
3615
+ statements:
3616
+ Statements.new(self, body: [statement], location: statement.location),
3617
+ consequent: nil,
3594
3618
  location: statement.location.to(predicate.location)
3595
3619
  )
3596
3620
  end
3597
3621
 
3598
3622
  # :call-seq:
3599
- # on_until: (untyped predicate, Statements statements) -> Until
3623
+ # on_until: (untyped predicate, Statements statements) -> UntilNode
3600
3624
  def on_until(predicate, statements)
3601
3625
  beginning = consume_keyword(:until)
3602
3626
  ending = consume_keyword(:end)
@@ -3618,7 +3642,7 @@ module SyntaxTree
3618
3642
  ending.location.start_column
3619
3643
  )
3620
3644
 
3621
- Until.new(
3645
+ UntilNode.new(
3622
3646
  predicate: predicate,
3623
3647
  statements: statements,
3624
3648
  location: beginning.location.to(ending.location)
@@ -3626,23 +3650,24 @@ module SyntaxTree
3626
3650
  end
3627
3651
 
3628
3652
  # :call-seq:
3629
- # on_until_mod: (untyped predicate, untyped statement) -> UntilMod
3653
+ # on_until_mod: (untyped predicate, untyped statement) -> UntilNode
3630
3654
  def on_until_mod(predicate, statement)
3631
3655
  consume_keyword(:until)
3632
3656
 
3633
- UntilMod.new(
3634
- statement: statement,
3657
+ UntilNode.new(
3635
3658
  predicate: predicate,
3659
+ statements:
3660
+ Statements.new(self, body: [statement], location: statement.location),
3636
3661
  location: statement.location.to(predicate.location)
3637
3662
  )
3638
3663
  end
3639
3664
 
3640
3665
  # :call-seq:
3641
- # on_var_alias: (GVar left, (Backref | GVar) right) -> VarAlias
3666
+ # on_var_alias: (GVar left, (Backref | GVar) right) -> AliasNode
3642
3667
  def on_var_alias(left, right)
3643
3668
  keyword = consume_keyword(:alias)
3644
3669
 
3645
- VarAlias.new(
3670
+ AliasNode.new(
3646
3671
  left: left,
3647
3672
  right: right,
3648
3673
  location: keyword.location.to(right.location)
@@ -3722,7 +3747,7 @@ module SyntaxTree
3722
3747
  end
3723
3748
 
3724
3749
  # :call-seq:
3725
- # on_while: (untyped predicate, Statements statements) -> While
3750
+ # on_while: (untyped predicate, Statements statements) -> WhileNode
3726
3751
  def on_while(predicate, statements)
3727
3752
  beginning = consume_keyword(:while)
3728
3753
  ending = consume_keyword(:end)
@@ -3744,7 +3769,7 @@ module SyntaxTree
3744
3769
  ending.location.start_column
3745
3770
  )
3746
3771
 
3747
- While.new(
3772
+ WhileNode.new(
3748
3773
  predicate: predicate,
3749
3774
  statements: statements,
3750
3775
  location: beginning.location.to(ending.location)
@@ -3752,13 +3777,14 @@ module SyntaxTree
3752
3777
  end
3753
3778
 
3754
3779
  # :call-seq:
3755
- # on_while_mod: (untyped predicate, untyped statement) -> WhileMod
3780
+ # on_while_mod: (untyped predicate, untyped statement) -> WhileNode
3756
3781
  def on_while_mod(predicate, statement)
3757
3782
  consume_keyword(:while)
3758
3783
 
3759
- WhileMod.new(
3760
- statement: statement,
3784
+ WhileNode.new(
3761
3785
  predicate: predicate,
3786
+ statements:
3787
+ Statements.new(self, body: [statement], location: statement.location),
3762
3788
  location: statement.location.to(predicate.location)
3763
3789
  )
3764
3790
  end
@@ -3881,22 +3907,22 @@ module SyntaxTree
3881
3907
  end
3882
3908
 
3883
3909
  # :call-seq:
3884
- # on_yield: ((Args | Paren) arguments) -> Yield
3910
+ # on_yield: ((Args | Paren) arguments) -> YieldNode
3885
3911
  def on_yield(arguments)
3886
3912
  keyword = consume_keyword(:yield)
3887
3913
 
3888
- Yield.new(
3914
+ YieldNode.new(
3889
3915
  arguments: arguments,
3890
3916
  location: keyword.location.to(arguments.location)
3891
3917
  )
3892
3918
  end
3893
3919
 
3894
3920
  # :call-seq:
3895
- # on_yield0: () -> Yield0
3921
+ # on_yield0: () -> YieldNode
3896
3922
  def on_yield0
3897
3923
  keyword = consume_keyword(:yield)
3898
3924
 
3899
- Yield0.new(value: keyword.value, location: keyword.location)
3925
+ YieldNode.new(arguments: nil, location: keyword.location)
3900
3926
  end
3901
3927
 
3902
3928
  # :call-seq:
@@ -3904,7 +3930,7 @@ module SyntaxTree
3904
3930
  def on_zsuper
3905
3931
  keyword = consume_keyword(:super)
3906
3932
 
3907
- ZSuper.new(value: keyword.value, location: keyword.location)
3933
+ ZSuper.new(location: keyword.location)
3908
3934
  end
3909
3935
  end
3910
3936
  end
@@ -142,11 +142,11 @@ module SyntaxTree
142
142
  def compile_const(node)
143
143
  value = node.value
144
144
 
145
- if SyntaxTree.const_defined?(value)
145
+ if SyntaxTree.const_defined?(value, false)
146
146
  clazz = SyntaxTree.const_get(value)
147
147
 
148
148
  ->(other) { clazz === other }
149
- elsif Object.const_defined?(value)
149
+ elsif Object.const_defined?(value, false)
150
150
  clazz = Object.const_get(value)
151
151
 
152
152
  ->(other) { clazz === other }
@@ -179,7 +179,7 @@ module SyntaxTree
179
179
 
180
180
  ->(other) { symbol === other }
181
181
  else
182
- compile_error(root)
182
+ compile_error(node)
183
183
  end
184
184
  end
185
185
 
@@ -1,3 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- SyntaxTree::Formatter::OPTIONS[:quote] = "'"
3
+ module SyntaxTree
4
+ class Formatter
5
+ SINGLE_QUOTES = true
6
+ end
7
+ end
@@ -1,3 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- SyntaxTree::Formatter::OPTIONS[:trailing_comma] = true
3
+ module SyntaxTree
4
+ class Formatter
5
+ TRAILING_COMMA = true
6
+ end
7
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SyntaxTree
4
- VERSION = "4.3.0"
4
+ VERSION = "5.0.0"
5
5
  end
@@ -4,10 +4,6 @@ module SyntaxTree
4
4
  # The environment class is used to keep track of local variables and arguments
5
5
  # inside a particular scope
6
6
  class Environment
7
- # [Array[Local]] The local variables and arguments defined in this
8
- # environment
9
- attr_reader :locals
10
-
11
7
  # This class tracks the occurrences of a local variable or argument
12
8
  class Local
13
9
  # [Symbol] The type of the local (e.g. :argument, :variable)
@@ -38,6 +34,13 @@ module SyntaxTree
38
34
  end
39
35
  end
40
36
 
37
+ # [Array[Local]] The local variables and arguments defined in this
38
+ # environment
39
+ attr_reader :locals
40
+
41
+ # [Environment | nil] The parent environment
42
+ attr_reader :parent
43
+
41
44
  # initialize: (Environment | nil parent) -> void
42
45
  def initialize(parent = nil)
43
46
  @locals = {}