Skip to content

Commit

Permalink
Simplify assignment_expression?
Browse files Browse the repository at this point in the history
  • Loading branch information
onlynone committed Jun 17, 2019
1 parent 06507fa commit 8ce9ce7
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions lib/irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 8ce9ce7

Please sign in to comment.