Skip to content

Commit 3f1c26f

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 81e07f3 commit 3f1c26f

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
@@ -3018,9 +3018,9 @@ nodes:
30183018
- name: statements
30193019
type: node?
30203020
kind: StatementsNode
3021-
- name: in_loc
3021+
- name: in_keyword_loc
30223022
type: location
3023-
- name: then_loc
3023+
- name: then_keyword_loc
30243024
type: location?
30253025
comment: |
30263026
Represents the use of the `in` keyword in a case statement.
@@ -3605,7 +3605,7 @@ nodes:
36053605
- name: pattern
36063606
type: node
36073607
kind: pattern expression
3608-
- name: operator_loc
3608+
- name: keyword_loc
36093609
type: location
36103610
comment: |
36113611
Represents the use of the modifier `in` operator.
@@ -4578,7 +4578,7 @@ nodes:
45784578
^^^^^^^^^^^^^^^^^^^^^^
45794579
- name: UnlessNode
45804580
fields:
4581-
- name: keyword_loc
4581+
- name: unless_keyword_loc
45824582
type: location
45834583
comment: |
45844584
The Location of the `unless` keyword.
@@ -4642,11 +4642,11 @@ nodes:
46424642
- name: UntilNode
46434643
flags: LoopFlags
46444644
fields:
4645-
- name: keyword_loc
4645+
- name: until_keyword_loc
46464646
type: location
46474647
- name: do_keyword_loc
46484648
type: location?
4649-
- name: closing_loc
4649+
- name: end_keyword_loc
46504650
type: location?
46514651
- name: predicate
46524652
type: node
@@ -4665,7 +4665,7 @@ nodes:
46654665
^^^^^^^^^^^^^^^^^^^^
46664666
- name: WhenNode
46674667
fields:
4668-
- name: keyword_loc
4668+
- name: when_keyword_loc
46694669
type: location
46704670
- name: conditions
46714671
type: node[]
@@ -4685,11 +4685,11 @@ nodes:
46854685
- name: WhileNode
46864686
flags: LoopFlags
46874687
fields:
4688-
- name: keyword_loc
4688+
- name: while_keyword_loc
46894689
type: location
46904690
- name: do_keyword_loc
46914691
type: location?
4692-
- name: closing_loc
4692+
- name: end_keyword_loc
46934693
type: location?
46944694
- name: predicate
46954695
type: node

lib/prism/node_ext.rb

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

lib/prism/translation/parser/compiler.rb

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

978978
builder.in_pattern(
979-
token(node.in_loc),
979+
token(node.in_keyword_loc),
980980
pattern,
981981
guard,
982-
if (then_loc = node.then_loc)
983-
token(then_loc)
982+
if (then_keyword_loc = node.then_keyword_loc)
983+
token(then_keyword_loc)
984984
else
985985
srange_semicolon(node.pattern.location.end_offset, node.statements&.location&.start_offset)
986986
end,
@@ -1291,7 +1291,7 @@ def visit_local_variable_target_node(node)
12911291
def visit_match_predicate_node(node)
12921292
builder.match_pattern_p(
12931293
visit(node.value),
1294-
token(node.operator_loc),
1294+
token(node.keyword_loc),
12951295
within_pattern { |compiler| node.pattern.accept(compiler) }
12961296
)
12971297
end
@@ -1802,9 +1802,9 @@ def visit_undef_node(node)
18021802
# bar unless foo
18031803
# ^^^^^^^^^^^^^^
18041804
def visit_unless_node(node)
1805-
if node.keyword_loc.start_offset == node.location.start_offset
1805+
if node.unless_keyword_loc.start_offset == node.location.start_offset
18061806
builder.condition(
1807-
token(node.keyword_loc),
1807+
token(node.unless_keyword_loc),
18081808
visit(node.predicate),
18091809
if (then_keyword_loc = node.then_keyword_loc)
18101810
token(then_keyword_loc)
@@ -1820,7 +1820,7 @@ def visit_unless_node(node)
18201820
builder.condition_mod(
18211821
visit(node.else_clause),
18221822
visit(node.statements),
1823-
token(node.keyword_loc),
1823+
token(node.unless_keyword_loc),
18241824
visit(node.predicate)
18251825
)
18261826
end
@@ -1832,24 +1832,24 @@ def visit_unless_node(node)
18321832
# bar until foo
18331833
# ^^^^^^^^^^^^^
18341834
def visit_until_node(node)
1835-
if node.location.start_offset == node.keyword_loc.start_offset
1835+
if node.location.start_offset == node.until_keyword_loc.start_offset
18361836
builder.loop(
18371837
:until,
1838-
token(node.keyword_loc),
1838+
token(node.until_keyword_loc),
18391839
visit(node.predicate),
18401840
if (do_keyword_loc = node.do_keyword_loc)
18411841
token(do_keyword_loc)
18421842
else
1843-
srange_semicolon(node.predicate.location.end_offset, (node.statements&.location || node.closing_loc).start_offset)
1843+
srange_semicolon(node.predicate.location.end_offset, (node.statements&.location || node.end_keyword_loc).start_offset)
18441844
end,
18451845
visit(node.statements),
1846-
token(node.closing_loc)
1846+
token(node.end_keyword_loc)
18471847
)
18481848
else
18491849
builder.loop_mod(
18501850
:until,
18511851
visit(node.statements),
1852-
token(node.keyword_loc),
1852+
token(node.until_keyword_loc),
18531853
visit(node.predicate)
18541854
)
18551855
end
@@ -1859,7 +1859,7 @@ def visit_until_node(node)
18591859
# ^^^^^^^^^^^^^
18601860
def visit_when_node(node)
18611861
builder.when(
1862-
token(node.keyword_loc),
1862+
token(node.when_keyword_loc),
18631863
visit_all(node.conditions),
18641864
if (then_keyword_loc = node.then_keyword_loc)
18651865
token(then_keyword_loc)
@@ -1876,24 +1876,24 @@ def visit_when_node(node)
18761876
# bar while foo
18771877
# ^^^^^^^^^^^^^
18781878
def visit_while_node(node)
1879-
if node.location.start_offset == node.keyword_loc.start_offset
1879+
if node.location.start_offset == node.while_keyword_loc.start_offset
18801880
builder.loop(
18811881
:while,
1882-
token(node.keyword_loc),
1882+
token(node.while_keyword_loc),
18831883
visit(node.predicate),
18841884
if (do_keyword_loc = node.do_keyword_loc)
18851885
token(do_keyword_loc)
18861886
else
1887-
srange_semicolon(node.predicate.location.end_offset, (node.statements&.location || node.closing_loc).start_offset)
1887+
srange_semicolon(node.predicate.location.end_offset, (node.statements&.location || node.end_keyword_loc).start_offset)
18881888
end,
18891889
visit(node.statements),
1890-
token(node.closing_loc)
1890+
token(node.end_keyword_loc)
18911891
)
18921892
else
18931893
builder.loop_mod(
18941894
:while,
18951895
visit(node.statements),
1896-
token(node.keyword_loc),
1896+
token(node.while_keyword_loc),
18971897
visit(node.predicate)
18981898
)
18991899
end

lib/prism/translation/ripper.rb

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

24682468
pattern = visit_pattern_node(node.pattern)
2469-
if node.then_loc
2470-
bounds(node.then_loc)
2469+
if node.then_keyword_loc
2470+
bounds(node.then_keyword_loc)
24712471
on_kw("then")
24722472
end
24732473
statements =
@@ -2974,7 +2974,7 @@ def visit_match_last_line_node(node)
29742974
# ^^^^^^^^^^
29752975
def visit_match_predicate_node(node)
29762976
value = visit(node.value)
2977-
bounds(node.operator_loc)
2977+
bounds(node.keyword_loc)
29782978
on_kw("in")
29792979
pattern = on_in(visit_pattern_node(node.pattern), nil, nil)
29802980

@@ -3877,7 +3877,7 @@ def visit_undef_node(node)
38773877
# ^^^^^^^^^^^^^^
38783878
def visit_unless_node(node)
38793879
if node.statements.nil? || (node.predicate.location.start_offset < node.statements.location.start_offset)
3880-
bounds(node.keyword_loc)
3880+
bounds(node.unless_keyword_loc)
38813881
on_kw("unless")
38823882
predicate = visit(node.predicate)
38833883
if node.then_keyword_loc
@@ -3902,7 +3902,7 @@ def visit_unless_node(node)
39023902
on_unless(predicate, statements, else_clause)
39033903
else
39043904
statements = visit(node.statements.body.first)
3905-
bounds(node.keyword_loc)
3905+
bounds(node.unless_keyword_loc)
39063906
on_kw("unless")
39073907
predicate = visit(node.predicate)
39083908

@@ -3917,7 +3917,7 @@ def visit_unless_node(node)
39173917
# bar until foo
39183918
# ^^^^^^^^^^^^^
39193919
def visit_until_node(node)
3920-
bounds(node.keyword_loc)
3920+
bounds(node.until_keyword_loc)
39213921
on_kw("until")
39223922

39233923
if node.statements.nil? || (node.predicate.location.start_offset < node.statements.location.start_offset)
@@ -3934,8 +3934,8 @@ def visit_until_node(node)
39343934
visit(node.statements)
39353935
end
39363936

3937-
if node.closing_loc
3938-
bounds(node.closing_loc)
3937+
if node.end_keyword_loc
3938+
bounds(node.end_keyword_loc)
39393939
on_kw("end")
39403940
end
39413941

@@ -3956,7 +3956,7 @@ def visit_when_node(node)
39563956
# This is a special case where we're not going to call on_when directly
39573957
# because we don't have access to the subsequent. Instead, we'll return
39583958
# the component parts and let the parent node handle it.
3959-
bounds(node.keyword_loc)
3959+
bounds(node.when_keyword_loc)
39603960
on_kw("when")
39613961

39623962
conditions = visit_arguments(node.conditions)
@@ -3982,15 +3982,15 @@ def visit_when_node(node)
39823982
# ^^^^^^^^^^^^^
39833983
def visit_while_node(node)
39843984
if node.statements.nil? || (node.predicate.location.start_offset < node.statements.location.start_offset)
3985-
bounds(node.keyword_loc)
3985+
bounds(node.while_keyword_loc)
39863986
on_kw("while")
39873987
if node.do_keyword_loc
39883988
bounds(node.do_keyword_loc)
39893989
on_kw("do")
39903990
end
39913991
predicate = visit(node.predicate)
3992-
if node.closing_loc
3993-
bounds(node.closing_loc)
3992+
if node.end_keyword_loc
3993+
bounds(node.end_keyword_loc)
39943994
on_kw("end")
39953995
end
39963996
statements =
@@ -4005,7 +4005,7 @@ def visit_while_node(node)
40054005
on_while(predicate, statements)
40064006
else
40074007
statements = visit(node.statements.body.first)
4008-
bounds(node.keyword_loc)
4008+
bounds(node.while_keyword_loc)
40094009
on_kw("while")
40104010
predicate = visit(node.predicate)
40114011

0 commit comments

Comments
 (0)