Skip to content

Commit d589d30

Browse files
committed
Rename some keyword locations for consistency
Prism is generally good about their naming but these had some inconsistencies compared to others: * `InNode` was missing the `keyword` in the name * `MatchPredicateNode` `in` was named `operator` instead * `UnlessNode`/`UntilNode`/`WhenNode`/`WhileNode` has more than one keyword, with one generic `keyword_loc` * `UntilNode`/`WhileNode` named their `end` keyword `closing_loc` Left the old names around for compatibility
1 parent 0808b31 commit d589d30

113 files changed

Lines changed: 1157 additions & 923 deletions

File tree

Some content is hidden

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

config.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,9 +3016,9 @@ nodes:
30163016
- name: statements
30173017
type: node?
30183018
kind: StatementsNode
3019-
- name: in_loc
3019+
- name: in_keyword_loc
30203020
type: location
3021-
- name: then_loc
3021+
- name: then_keyword_loc
30223022
type: location?
30233023
comment: |
30243024
Represents the use of the `in` keyword in a case statement.
@@ -3603,7 +3603,7 @@ nodes:
36033603
- name: pattern
36043604
type: node
36053605
kind: pattern expression
3606-
- name: operator_loc
3606+
- name: keyword_loc
36073607
type: location
36083608
comment: |
36093609
Represents the use of the modifier `in` operator.
@@ -4576,7 +4576,7 @@ nodes:
45764576
^^^^^^^^^^^^^^^^^^^^^^
45774577
- name: UnlessNode
45784578
fields:
4579-
- name: keyword_loc
4579+
- name: unless_keyword_loc
45804580
type: location
45814581
comment: |
45824582
The Location of the `unless` keyword.
@@ -4640,11 +4640,11 @@ nodes:
46404640
- name: UntilNode
46414641
flags: LoopFlags
46424642
fields:
4643-
- name: keyword_loc
4643+
- name: until_keyword_loc
46444644
type: location
46454645
- name: do_keyword_loc
46464646
type: location?
4647-
- name: closing_loc
4647+
- name: end_keyword_loc
46484648
type: location?
46494649
- name: predicate
46504650
type: node
@@ -4663,7 +4663,7 @@ nodes:
46634663
^^^^^^^^^^^^^^^^^^^^
46644664
- name: WhenNode
46654665
fields:
4666-
- name: keyword_loc
4666+
- name: when_keyword_loc
46674667
type: location
46684668
- name: conditions
46694669
type: node[]
@@ -4683,11 +4683,11 @@ nodes:
46834683
- name: WhileNode
46844684
flags: LoopFlags
46854685
fields:
4686-
- name: keyword_loc
4686+
- name: while_keyword_loc
46874687
type: location
46884688
- name: do_keyword_loc
46894689
type: location?
4690-
- name: closing_loc
4690+
- name: end_keyword_loc
46914691
type: location?
46924692
- name: predicate
46934693
type: node

lib/prism/node_ext.rb

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,4 +388,106 @@ def full_message_loc
388388
attribute_write? ? message_loc&.adjoin("=") : message_loc
389389
end
390390
end
391+
392+
class InNode < Node
393+
#: () -> String
394+
def in # :nodoc
395+
in_keyword
396+
end
397+
398+
#: () -> Location
399+
def in_loc # :nodoc
400+
in_keyword_loc
401+
end
402+
403+
#: () -> String?
404+
def then # :nodoc
405+
then_keyword
406+
end
407+
408+
#: () -> Location?
409+
def then_loc # :nodoc
410+
then_keyword_loc
411+
end
412+
end
413+
414+
class MatchPredicateNode < Node
415+
#: () -> String
416+
def operator # :nodoc
417+
keyword
418+
end
419+
420+
#: () -> Location
421+
def operator_loc # :nodoc
422+
keyword_loc
423+
end
424+
end
425+
426+
class UnlessNode < Node
427+
#: () -> String
428+
def keyword # :nodoc
429+
unless_keyword
430+
end
431+
432+
#: () -> Location
433+
def keyword_loc # :nodoc
434+
unless_keyword_loc
435+
end
436+
end
437+
438+
class UntilNode < Node
439+
#: () -> String
440+
def keyword # :nodoc
441+
until_keyword
442+
end
443+
444+
#: () -> Location
445+
def keyword_loc # :nodoc
446+
until_keyword_loc
447+
end
448+
449+
#: () -> String?
450+
def closing # :nodoc
451+
end_keyword
452+
end
453+
454+
#: () -> Location?
455+
def closing_loc # :nodoc
456+
end_keyword_loc
457+
end
458+
end
459+
460+
class WhenNode < Node
461+
#: () -> String
462+
def keyword # :nodoc
463+
when_keyword
464+
end
465+
466+
#: () -> Location
467+
def keyword_loc # :nodoc
468+
when_keyword_loc
469+
end
470+
end
471+
472+
class WhileNode < Node
473+
#: () -> String
474+
def keyword # :nodoc
475+
while_keyword
476+
end
477+
478+
#: () -> Location
479+
def keyword_loc # :nodoc
480+
while_keyword_loc
481+
end
482+
483+
#: () -> String?
484+
def closing # :nodoc
485+
end_keyword
486+
end
487+
488+
#: () -> Location?
489+
def closing_loc # :nodoc
490+
end_keyword_loc
491+
end
492+
end
391493
end

lib/prism/translation/parser/compiler.rb

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -975,17 +975,17 @@ def visit_in_node(node)
975975
guard = builder.if_guard(token(node.pattern.if_keyword_loc), visit(node.pattern.predicate))
976976
when UnlessNode
977977
pattern = within_pattern { |compiler| node.pattern.statements.accept(compiler) }
978-
guard = builder.unless_guard(token(node.pattern.keyword_loc), visit(node.pattern.predicate))
978+
guard = builder.unless_guard(token(node.pattern.unless_keyword_loc), visit(node.pattern.predicate))
979979
else
980980
pattern = within_pattern { |compiler| node.pattern.accept(compiler) }
981981
end
982982

983983
builder.in_pattern(
984-
token(node.in_loc),
984+
token(node.in_keyword_loc),
985985
pattern,
986986
guard,
987-
if (then_loc = node.then_loc)
988-
token(then_loc)
987+
if (then_keyword_loc = node.then_keyword_loc)
988+
token(then_keyword_loc)
989989
else
990990
srange_semicolon(node.pattern.location.end_offset, node.statements&.location&.start_offset)
991991
end,
@@ -1296,7 +1296,7 @@ def visit_local_variable_target_node(node)
12961296
def visit_match_predicate_node(node)
12971297
builder.match_pattern_p(
12981298
visit(node.value),
1299-
token(node.operator_loc),
1299+
token(node.keyword_loc),
13001300
within_pattern { |compiler| node.pattern.accept(compiler) }
13011301
)
13021302
end
@@ -1807,9 +1807,9 @@ def visit_undef_node(node)
18071807
# bar unless foo
18081808
# ^^^^^^^^^^^^^^
18091809
def visit_unless_node(node)
1810-
if node.keyword_loc.start_offset == node.location.start_offset
1810+
if node.unless_keyword_loc.start_offset == node.location.start_offset
18111811
builder.condition(
1812-
token(node.keyword_loc),
1812+
token(node.unless_keyword_loc),
18131813
visit(node.predicate),
18141814
if (then_keyword_loc = node.then_keyword_loc)
18151815
token(then_keyword_loc)
@@ -1825,7 +1825,7 @@ def visit_unless_node(node)
18251825
builder.condition_mod(
18261826
visit(node.else_clause),
18271827
visit(node.statements),
1828-
token(node.keyword_loc),
1828+
token(node.unless_keyword_loc),
18291829
visit(node.predicate)
18301830
)
18311831
end
@@ -1837,24 +1837,24 @@ def visit_unless_node(node)
18371837
# bar until foo
18381838
# ^^^^^^^^^^^^^
18391839
def visit_until_node(node)
1840-
if node.location.start_offset == node.keyword_loc.start_offset
1840+
if node.location.start_offset == node.until_keyword_loc.start_offset
18411841
builder.loop(
18421842
:until,
1843-
token(node.keyword_loc),
1843+
token(node.until_keyword_loc),
18441844
visit(node.predicate),
18451845
if (do_keyword_loc = node.do_keyword_loc)
18461846
token(do_keyword_loc)
18471847
else
1848-
srange_semicolon(node.predicate.location.end_offset, (node.statements&.location || node.closing_loc).start_offset)
1848+
srange_semicolon(node.predicate.location.end_offset, (node.statements&.location || node.end_keyword_loc).start_offset)
18491849
end,
18501850
visit(node.statements),
1851-
token(node.closing_loc)
1851+
token(node.end_keyword_loc)
18521852
)
18531853
else
18541854
builder.loop_mod(
18551855
:until,
18561856
visit(node.statements),
1857-
token(node.keyword_loc),
1857+
token(node.until_keyword_loc),
18581858
visit(node.predicate)
18591859
)
18601860
end
@@ -1864,7 +1864,7 @@ def visit_until_node(node)
18641864
# ^^^^^^^^^^^^^
18651865
def visit_when_node(node)
18661866
builder.when(
1867-
token(node.keyword_loc),
1867+
token(node.when_keyword_loc),
18681868
visit_all(node.conditions),
18691869
if (then_keyword_loc = node.then_keyword_loc)
18701870
token(then_keyword_loc)
@@ -1881,24 +1881,24 @@ def visit_when_node(node)
18811881
# bar while foo
18821882
# ^^^^^^^^^^^^^
18831883
def visit_while_node(node)
1884-
if node.location.start_offset == node.keyword_loc.start_offset
1884+
if node.location.start_offset == node.while_keyword_loc.start_offset
18851885
builder.loop(
18861886
:while,
1887-
token(node.keyword_loc),
1887+
token(node.while_keyword_loc),
18881888
visit(node.predicate),
18891889
if (do_keyword_loc = node.do_keyword_loc)
18901890
token(do_keyword_loc)
18911891
else
1892-
srange_semicolon(node.predicate.location.end_offset, (node.statements&.location || node.closing_loc).start_offset)
1892+
srange_semicolon(node.predicate.location.end_offset, (node.statements&.location || node.end_keyword_loc).start_offset)
18931893
end,
18941894
visit(node.statements),
1895-
token(node.closing_loc)
1895+
token(node.end_keyword_loc)
18961896
)
18971897
else
18981898
builder.loop_mod(
18991899
:while,
19001900
visit(node.statements),
1901-
token(node.keyword_loc),
1901+
token(node.while_keyword_loc),
19021902
visit(node.predicate)
19031903
)
19041904
end

lib/prism/translation/ripper.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,12 +2169,12 @@ def visit_in_node(node)
21692169
# This is a special case where we're not going to call on_in directly
21702170
# because we don't have access to the subsequent. Instead, we'll return
21712171
# the component parts and let the parent node handle it.
2172-
bounds(node.in_loc)
2172+
bounds(node.in_keyword_loc)
21732173
on_kw("in")
21742174

21752175
pattern = visit_pattern_node(node.pattern)
2176-
if node.then_loc
2177-
bounds(node.then_loc)
2176+
if node.then_keyword_loc
2177+
bounds(node.then_keyword_loc)
21782178
on_kw("then")
21792179
end
21802180
statements =
@@ -2633,7 +2633,7 @@ def visit_match_last_line_node(node)
26332633
# ^^^^^^^^^^
26342634
def visit_match_predicate_node(node)
26352635
value = visit(node.value)
2636-
bounds(node.operator_loc)
2636+
bounds(node.keyword_loc)
26372637
on_kw("in")
26382638
pattern = on_in(visit_pattern_node(node.pattern), nil, nil)
26392639

@@ -3430,7 +3430,7 @@ def visit_undef_node(node)
34303430
# ^^^^^^^^^^^^^^
34313431
def visit_unless_node(node)
34323432
if node.statements.nil? || (node.predicate.location.start_offset < node.statements.location.start_offset)
3433-
bounds(node.keyword_loc)
3433+
bounds(node.unless_keyword_loc)
34343434
on_kw("unless")
34353435
predicate = visit(node.predicate)
34363436
if node.then_keyword_loc
@@ -3455,7 +3455,7 @@ def visit_unless_node(node)
34553455
on_unless(predicate, statements, else_clause)
34563456
else
34573457
statements = visit(node.statements.body.first)
3458-
bounds(node.keyword_loc)
3458+
bounds(node.unless_keyword_loc)
34593459
on_kw("unless")
34603460
predicate = visit(node.predicate)
34613461

@@ -3470,7 +3470,7 @@ def visit_unless_node(node)
34703470
# bar until foo
34713471
# ^^^^^^^^^^^^^
34723472
def visit_until_node(node)
3473-
bounds(node.keyword_loc)
3473+
bounds(node.until_keyword_loc)
34743474
on_kw("until")
34753475

34763476
if node.statements.nil? || (node.predicate.location.start_offset < node.statements.location.start_offset)
@@ -3487,8 +3487,8 @@ def visit_until_node(node)
34873487
visit(node.statements)
34883488
end
34893489

3490-
if node.closing_loc
3491-
bounds(node.closing_loc)
3490+
if node.end_keyword_loc
3491+
bounds(node.end_keyword_loc)
34923492
on_kw("end")
34933493
end
34943494

@@ -3509,7 +3509,7 @@ def visit_when_node(node)
35093509
# This is a special case where we're not going to call on_when directly
35103510
# because we don't have access to the subsequent. Instead, we'll return
35113511
# the component parts and let the parent node handle it.
3512-
bounds(node.keyword_loc)
3512+
bounds(node.when_keyword_loc)
35133513
on_kw("when")
35143514

35153515
conditions = visit_arguments(node.conditions)
@@ -3535,15 +3535,15 @@ def visit_when_node(node)
35353535
# ^^^^^^^^^^^^^
35363536
def visit_while_node(node)
35373537
if node.statements.nil? || (node.predicate.location.start_offset < node.statements.location.start_offset)
3538-
bounds(node.keyword_loc)
3538+
bounds(node.while_keyword_loc)
35393539
on_kw("while")
35403540
if node.do_keyword_loc
35413541
bounds(node.do_keyword_loc)
35423542
on_kw("do")
35433543
end
35443544
predicate = visit(node.predicate)
3545-
if node.closing_loc
3546-
bounds(node.closing_loc)
3545+
if node.end_keyword_loc
3546+
bounds(node.end_keyword_loc)
35473547
on_kw("end")
35483548
end
35493549
statements =
@@ -3558,7 +3558,7 @@ def visit_while_node(node)
35583558
on_while(predicate, statements)
35593559
else
35603560
statements = visit(node.statements.body.first)
3561-
bounds(node.keyword_loc)
3561+
bounds(node.while_keyword_loc)
35623562
on_kw("while")
35633563
predicate = visit(node.predicate)
35643564

0 commit comments

Comments
 (0)