gazayas / masamune-ast

A covenience wrapper around Prism, a Ruby source code parser
MIT License
13 stars 1 forks source link

Refactor class extraction #13

Closed gazayas closed 1 year ago

gazayas commented 1 year ago
x = 1

The output from Ripper.sexp for this code:

[:program,
  [
    [:assign,
      [:var_field, [:@ident, "x", [1, 0]]],
      [:@int, "1", [1, 4]]
    ]
  ]
]

Since nodes start with a type (a Symbol that represents what operation the node is performing), I wanted to expose that layer of abstraction explicitly in the code instead of just writing what I had before when grabbing the appropriate node class:

class_name = "Masamune::AbstractSyntaxTree::#{tree_node.first.to_s.camelize}"
klass = class_name.constantize
msmn_node = klass.new(tree_node, self.__id__)

For whoever's reading the code, this doesn't explicitly say what tree_node.first is, but with this new patch we show them that the type is originally defined as a symbol, we show them that this value represents a type as can be seen in the argument for the get_node_class method, and we find the class via that symbol. I think this will make the code easier to read, especially if developers want to check their results against Ripper.sexp's output itself.