[ruby/prism] 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`

The old names are still around, but you have to require prism/deprecated

https://github.com/ruby/prism/commit/d5a505372b
This commit is contained in:
Earlopain 2026-04-02 08:33:38 +02:00 committed by git
parent a4a197922d
commit 7ac76f6b84
5 changed files with 152 additions and 42 deletions

108
lib/prism/deprecated.rb Normal file
View File

@ -0,0 +1,108 @@
# frozen_string_literal: true
# :markup: markdown
#--
# rbs_inline: enabled
module Prism
class InNode < Node
#: () -> String
def in # :nodoc
in_keyword
end
#: () -> Location
def in_loc # :nodoc
in_keyword_loc
end
#: () -> String?
def then # :nodoc
then_keyword
end
#: () -> Location?
def then_loc # :nodoc
then_keyword_loc
end
end
class MatchPredicateNode < Node
#: () -> String
def operator # :nodoc
keyword
end
#: () -> Location
def operator_loc # :nodoc
keyword_loc
end
end
class UnlessNode < Node
#: () -> String
def keyword # :nodoc
unless_keyword
end
#: () -> Location
def keyword_loc # :nodoc
unless_keyword_loc
end
end
class UntilNode < Node
#: () -> String
def keyword # :nodoc
until_keyword
end
#: () -> Location
def keyword_loc # :nodoc
until_keyword_loc
end
#: () -> String?
def closing # :nodoc
end_keyword
end
#: () -> Location?
def closing_loc # :nodoc
end_keyword_loc
end
end
class WhenNode < Node
#: () -> String
def keyword # :nodoc
when_keyword
end
#: () -> Location
def keyword_loc # :nodoc
when_keyword_loc
end
end
class WhileNode < Node
#: () -> String
def keyword # :nodoc
while_keyword
end
#: () -> Location
def keyword_loc # :nodoc
while_keyword_loc
end
#: () -> String?
def closing # :nodoc
end_keyword
end
#: () -> Location?
def closing_loc # :nodoc
end_keyword_loc
end
end
end

View File

@ -109,6 +109,7 @@ Gem::Specification.new do |spec|
"include/prism/version.h",
"lib/prism.rb",
"lib/prism/compiler.rb",
"lib/prism/deprecated.rb",
"lib/prism/desugar_compiler.rb",
"lib/prism/dispatcher.rb",
"lib/prism/dot_visitor.rb",
@ -178,6 +179,7 @@ Gem::Specification.new do |spec|
"rbi/rubyvm/node_find.rbi",
"sig/generated/prism.rbs",
"sig/generated/prism/compiler.rbs",
"sig/generated/prism/deprecated.rbs",
"sig/generated/prism/desugar_compiler.rbs",
"sig/generated/prism/dispatcher.rbs",
"sig/generated/prism/dot_visitor.rbs",

View File

@ -970,17 +970,17 @@ module Prism
guard = builder.if_guard(token(node.pattern.if_keyword_loc), visit(node.pattern.predicate))
when UnlessNode
pattern = within_pattern { |compiler| node.pattern.statements.accept(compiler) }
guard = builder.unless_guard(token(node.pattern.keyword_loc), visit(node.pattern.predicate))
guard = builder.unless_guard(token(node.pattern.unless_keyword_loc), visit(node.pattern.predicate))
else
pattern = within_pattern { |compiler| node.pattern.accept(compiler) }
end
builder.in_pattern(
token(node.in_loc),
token(node.in_keyword_loc),
pattern,
guard,
if (then_loc = node.then_loc)
token(then_loc)
if (then_keyword_loc = node.then_keyword_loc)
token(then_keyword_loc)
else
srange_semicolon(node.pattern.location.end_offset, node.statements&.location&.start_offset)
end,
@ -1291,7 +1291,7 @@ module Prism
def visit_match_predicate_node(node)
builder.match_pattern_p(
visit(node.value),
token(node.operator_loc),
token(node.keyword_loc),
within_pattern { |compiler| node.pattern.accept(compiler) }
)
end
@ -1802,9 +1802,9 @@ module Prism
# bar unless foo
# ^^^^^^^^^^^^^^
def visit_unless_node(node)
if node.keyword_loc.start_offset == node.location.start_offset
if node.unless_keyword_loc.start_offset == node.location.start_offset
builder.condition(
token(node.keyword_loc),
token(node.unless_keyword_loc),
visit(node.predicate),
if (then_keyword_loc = node.then_keyword_loc)
token(then_keyword_loc)
@ -1820,7 +1820,7 @@ module Prism
builder.condition_mod(
visit(node.else_clause),
visit(node.statements),
token(node.keyword_loc),
token(node.unless_keyword_loc),
visit(node.predicate)
)
end
@ -1832,24 +1832,24 @@ module Prism
# bar until foo
# ^^^^^^^^^^^^^
def visit_until_node(node)
if node.location.start_offset == node.keyword_loc.start_offset
if node.location.start_offset == node.until_keyword_loc.start_offset
builder.loop(
:until,
token(node.keyword_loc),
token(node.until_keyword_loc),
visit(node.predicate),
if (do_keyword_loc = node.do_keyword_loc)
token(do_keyword_loc)
else
srange_semicolon(node.predicate.location.end_offset, (node.statements&.location || node.closing_loc).start_offset)
srange_semicolon(node.predicate.location.end_offset, (node.statements&.location || node.end_keyword_loc).start_offset)
end,
visit(node.statements),
token(node.closing_loc)
token(node.end_keyword_loc)
)
else
builder.loop_mod(
:until,
visit(node.statements),
token(node.keyword_loc),
token(node.until_keyword_loc),
visit(node.predicate)
)
end
@ -1859,7 +1859,7 @@ module Prism
# ^^^^^^^^^^^^^
def visit_when_node(node)
builder.when(
token(node.keyword_loc),
token(node.when_keyword_loc),
visit_all(node.conditions),
if (then_keyword_loc = node.then_keyword_loc)
token(then_keyword_loc)
@ -1876,24 +1876,24 @@ module Prism
# bar while foo
# ^^^^^^^^^^^^^
def visit_while_node(node)
if node.location.start_offset == node.keyword_loc.start_offset
if node.location.start_offset == node.while_keyword_loc.start_offset
builder.loop(
:while,
token(node.keyword_loc),
token(node.while_keyword_loc),
visit(node.predicate),
if (do_keyword_loc = node.do_keyword_loc)
token(do_keyword_loc)
else
srange_semicolon(node.predicate.location.end_offset, (node.statements&.location || node.closing_loc).start_offset)
srange_semicolon(node.predicate.location.end_offset, (node.statements&.location || node.end_keyword_loc).start_offset)
end,
visit(node.statements),
token(node.closing_loc)
token(node.end_keyword_loc)
)
else
builder.loop_mod(
:while,
visit(node.statements),
token(node.keyword_loc),
token(node.while_keyword_loc),
visit(node.predicate)
)
end

View File

@ -2472,12 +2472,12 @@ module Prism
# This is a special case where we're not going to call on_in directly
# because we don't have access to the subsequent. Instead, we'll return
# the component parts and let the parent node handle it.
bounds(node.in_loc)
bounds(node.in_keyword_loc)
on_kw("in")
pattern = visit_pattern_node(node.pattern)
if node.then_loc
bounds(node.then_loc)
if node.then_keyword_loc
bounds(node.then_keyword_loc)
on_kw("then")
end
statements =
@ -2984,7 +2984,7 @@ module Prism
# ^^^^^^^^^^
def visit_match_predicate_node(node)
value = visit(node.value)
bounds(node.operator_loc)
bounds(node.keyword_loc)
on_kw("in")
pattern = on_in(visit_pattern_node(node.pattern), nil, nil)
@ -3887,7 +3887,7 @@ module Prism
# ^^^^^^^^^^^^^^
def visit_unless_node(node)
if node.statements.nil? || (node.predicate.location.start_offset < node.statements.location.start_offset)
bounds(node.keyword_loc)
bounds(node.unless_keyword_loc)
on_kw("unless")
predicate = visit(node.predicate)
if node.then_keyword_loc
@ -3912,7 +3912,7 @@ module Prism
on_unless(predicate, statements, else_clause)
else
statements = visit(node.statements.body.first)
bounds(node.keyword_loc)
bounds(node.unless_keyword_loc)
on_kw("unless")
predicate = visit(node.predicate)
@ -3927,7 +3927,7 @@ module Prism
# bar until foo
# ^^^^^^^^^^^^^
def visit_until_node(node)
bounds(node.keyword_loc)
bounds(node.until_keyword_loc)
on_kw("until")
if node.statements.nil? || (node.predicate.location.start_offset < node.statements.location.start_offset)
@ -3944,8 +3944,8 @@ module Prism
visit(node.statements)
end
if node.closing_loc
bounds(node.closing_loc)
if node.end_keyword_loc
bounds(node.end_keyword_loc)
on_kw("end")
end
@ -3966,7 +3966,7 @@ module Prism
# This is a special case where we're not going to call on_when directly
# because we don't have access to the subsequent. Instead, we'll return
# the component parts and let the parent node handle it.
bounds(node.keyword_loc)
bounds(node.when_keyword_loc)
on_kw("when")
conditions = visit_arguments(node.conditions)
@ -3992,15 +3992,15 @@ module Prism
# ^^^^^^^^^^^^^
def visit_while_node(node)
if node.statements.nil? || (node.predicate.location.start_offset < node.statements.location.start_offset)
bounds(node.keyword_loc)
bounds(node.while_keyword_loc)
on_kw("while")
if node.do_keyword_loc
bounds(node.do_keyword_loc)
on_kw("do")
end
predicate = visit(node.predicate)
if node.closing_loc
bounds(node.closing_loc)
if node.end_keyword_loc
bounds(node.end_keyword_loc)
on_kw("end")
end
statements =
@ -4015,7 +4015,7 @@ module Prism
on_while(predicate, statements)
else
statements = visit(node.statements.body.first)
bounds(node.keyword_loc)
bounds(node.while_keyword_loc)
on_kw("while")
predicate = visit(node.predicate)

View File

@ -3018,9 +3018,9 @@ nodes:
- name: statements
type: node?
kind: StatementsNode
- name: in_loc
- name: in_keyword_loc
type: location
- name: then_loc
- name: then_keyword_loc
type: location?
comment: |
Represents the use of the `in` keyword in a case statement.
@ -3605,7 +3605,7 @@ nodes:
- name: pattern
type: node
kind: pattern expression
- name: operator_loc
- name: keyword_loc
type: location
comment: |
Represents the use of the modifier `in` operator.
@ -4578,7 +4578,7 @@ nodes:
^^^^^^^^^^^^^^^^^^^^^^
- name: UnlessNode
fields:
- name: keyword_loc
- name: unless_keyword_loc
type: location
comment: |
The Location of the `unless` keyword.
@ -4642,11 +4642,11 @@ nodes:
- name: UntilNode
flags: LoopFlags
fields:
- name: keyword_loc
- name: until_keyword_loc
type: location
- name: do_keyword_loc
type: location?
- name: closing_loc
- name: end_keyword_loc
type: location?
- name: predicate
type: node
@ -4665,7 +4665,7 @@ nodes:
^^^^^^^^^^^^^^^^^^^^
- name: WhenNode
fields:
- name: keyword_loc
- name: when_keyword_loc
type: location
- name: conditions
type: node[]
@ -4685,11 +4685,11 @@ nodes:
- name: WhileNode
flags: LoopFlags
fields:
- name: keyword_loc
- name: while_keyword_loc
type: location
- name: do_keyword_loc
type: location?
- name: closing_loc
- name: end_keyword_loc
type: location?
- name: predicate
type: node