From c78f7425815ec230186099e8071a32a5b05a64b2 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Tue, 27 Jan 2026 15:42:24 -0500 Subject: [PATCH] Mark STATES as a private constant --- lib/prism/lex_compat.rb | 6 +++--- lib/prism/translation/ripper/lexer.rb | 11 ++++++----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/prism/lex_compat.rb b/lib/prism/lex_compat.rb index 4960230bcf..e69f4109dc 100644 --- a/lib/prism/lex_compat.rb +++ b/lib/prism/lex_compat.rb @@ -639,7 +639,7 @@ def result event = RIPPER.fetch(token.type) value = token.value - lex_state = Translation::Ripper::Lexer::State.cached(lex_state) + lex_state = Translation::Ripper::Lexer::State[lex_state] token = case event @@ -691,7 +691,7 @@ def result counter += { on_embexpr_beg: -1, on_embexpr_end: 1 }[current_event] || 0 end - Translation::Ripper::Lexer::State.cached(result_value[current_index][1]) + Translation::Ripper::Lexer::State[result_value[current_index][1]] else previous_state end @@ -828,7 +828,7 @@ def result def add_on_sp_tokens(tokens, source, data_loc, bom, eof_token) new_tokens = [] - prev_token_state = Translation::Ripper::Lexer::State.cached(Translation::Ripper::EXPR_BEG) + prev_token_state = Translation::Ripper::Lexer::State[Translation::Ripper::EXPR_BEG] prev_token_end = bom ? 3 : 0 tokens.each do |token| diff --git a/lib/prism/translation/ripper/lexer.rb b/lib/prism/translation/ripper/lexer.rb index bed863af08..cbcdcd47cc 100644 --- a/lib/prism/translation/ripper/lexer.rb +++ b/lib/prism/translation/ripper/lexer.rb @@ -9,7 +9,6 @@ class Ripper class Lexer < Ripper # :nodoc: # :stopdoc: class State - attr_reader :to_int, :to_s def initialize(i) @@ -39,10 +38,12 @@ def allbits?(i) to_int.allbits?(i) end def anybits?(i) to_int.anybits?(i) end def nobits?(i) to_int.nobits?(i) end - # Instances are frozen and there are only a handful of them so we cache them here. - STATES = Hash.new { |h,k| h[k] = State.new(k) } + # Instances are frozen and there are only a handful of them so we + # cache them here. + STATES = Hash.new { |hash, key| hash[key] = State.new(key) } + private_constant :STATES - def self.cached(i) + def self.[](i) STATES[i] end end @@ -54,7 +55,7 @@ def initialize(pos, event, tok, state, message = nil) @pos = pos @event = event @tok = tok - @state = State.cached(state) + @state = State[state] @message = message end