MCPcopy Create free account
hub / github.com/scottrogowski/code2flow / Ruby

Class Ruby

code2flow/ruby.py:194–345  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

192
193
194class Ruby(BaseLanguage):
195 @staticmethod
196 def assert_dependencies():
197 """Assert that ruby-parse is installed"""
198 assert is_installed('ruby-parse'), "The 'parser' gem is requred to " \
199 "parse ruby files but was not found " \
200 "on the path. Install it from gem " \
201 "and try again."
202
203 @staticmethod
204 def get_tree(filename, lang_params):
205 """
206 Get the entire AST for this file
207
208 :param filename str:
209 :param lang_params LanguageParams:
210 :rtype: ast
211 """
212 version_flag = "--" + lang_params.ruby_version
213 cmd = ["ruby-parse", "--emit-json", version_flag, filename]
214 output = subprocess.check_output(cmd, stderr=subprocess.PIPE)
215 try:
216 tree = json.loads(output)
217 except json.decoder.JSONDecodeError:
218 raise AssertionError(
219 "Ruby-parse could not parse file %r. You may have a syntax error. "
220 "For more detail, try running the command `ruby-parse %s`. " %
221 (filename, filename)) from None
222 assert isinstance(tree, list)
223
224 if tree[0] not in ('module', 'begin'):
225 # one-line files
226 tree = [tree]
227 return tree
228
229 @staticmethod
230 def separate_namespaces(tree):
231 """
232 Given a tree element, recursively separate that AST into lists of ASTs for the
233 subgroups, nodes, and body. This is an intermediate step to allow for
234 cleaner processing downstream
235
236 :param tree ast:
237 :returns: tuple of group, node, and body trees. These are processed
238 downstream into real Groups and Nodes.
239 :rtype: (list[ast], list[ast], list[ast])
240 """
241 groups = []
242 nodes = []
243 body = []
244 for el in as_lines(tree):
245 if el[0] in ('def', 'defs'):
246 nodes.append(el)
247 elif el[0] in ('class', 'module'):
248 groups.append(el)
249 else:
250 body.append(el)
251 return groups, nodes, body

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected