From 8ce9ce763c428294f4b91361b8b0f7cb89346604 Mon Sep 17 00:00:00 2001 From: Steven Willis Date: Mon, 1 Apr 2019 10:32:51 -0400 Subject: [PATCH] Simplify assignment_expression? --- lib/irb.rb | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/lib/irb.rb b/lib/irb.rb index 94bf23859..17af27085 100644 --- a/lib/irb.rb +++ b/lib/irb.rb @@ -736,22 +736,15 @@ def inspect end def assignment_expression?(line) - # Try to parse the line and return false if it doesn't seem valid - s = Ripper.sexp(line) - return false if !s.is_a?(Array) - - # The first element of the returned array should just be :program, the - # second element should be an array of the top level expressions - expressions = s[1] - return false if !expressions.is_a?(Array) - - # We only consider the type of the last expression - last_expression = expressions.last - return false if !last_expression.is_a?(Array) - - # The first element of the last expression should be its type, return - # whether it's one of our assignment types - ASSIGNMENT_NODE_TYPES.include?(last_expression[0]) + # Try to parse the line and check if the last of possibly multiple + # expressions is an assignment type. + + # If the expression is invalid, Ripper.sexp should return nil which will + # result in false being returned. Any valid expression should return an + # s-expression where the second selement of the top level array is an + # array of parsed expressions. The first element of each expression is the + # expression's type. + ASSIGNMENT_NODE_TYPES.include?(Ripper.sexp(line)&.dig(1,-1,0)) end ATTR_TTY = "\e[%sm"