Class: DrgDSL::ParenCleaner

Inherits:
Object
  • Object
show all
Includes:
Visitor
Defined in:
lib/drgdsl/paren_cleaner.rb

Class Method Summary collapse

Methods included from Visitor

#cache?, #cache_key, included, #visit, #visit_BasicExpression, #visit_Comparison, #visit_Constant, #visit_DateExpression, #visit_DrgLink, #visit_Empty, #visit_FunctionCall, #visit_NotExpression, #visit_SrglrbTableCondition, #visit_TableCondition, #visit_UnaryCondition, #visit_Variable, #visit_nil

Class Method Details

.clean(ast, remove_outer_parens: true) ⇒ Node

Returns ast without redundant ParenExpressions.

Parameters:

  • ast (Node)

Returns:

  • (Node)

    ast without redundant ParenExpressions



18
19
20
21
22
23
24
25
26
# File 'lib/drgdsl/paren_cleaner.rb', line 18

def self.clean(ast, remove_outer_parens: true)
  # If the whole expression is wrapped in parentheses, we can start
  # visiting the first child.
  if ast.type == 'ParenExpression' && remove_outer_parens
    ast = ast.expression
  end

  ast.accept(new)
end

.remove_redundant_parens(expression) ⇒ String

Returns pretty printed expression without any redundant parentheses.

Parameters:

  • expression (String)

Returns:

  • (String)

    pretty printed expression without any redundant parentheses.



10
11
12
13
14
# File 'lib/drgdsl/paren_cleaner.rb', line 10

def self.remove_redundant_parens(expression)
  ast = Parser.parse(expression)
  reduced_ast = clean(ast)
  reduced_ast.to_s
end