Esempio n. 1
0
 def test_argument(code, pattern_key):
     json_program = esprima.parse(code)
     ast_program = visitor.objectify(json_program.to_dict())
     call_expression = ast_program.body[0].expression
     is_matched = p_utils.match_argument_with_object_key(
         call_expression, pattern_key)
     return is_matched
Esempio n. 2
0
        def test_name_space(code, name_list):
            json_program = esprima.parse(code)
            ast_program = visitor.objectify(json_program.to_dict())

            call_expression = ast_program.body[0].expression
            name_space = p_utils.extract_name_space(call_expression)
            self.assertSequenceEqual(name_space, name_list)
Esempio n. 3
0
        def test_name_space(code, pattern_list):
            json_program = esprima.parse(code)
            ast_program = visitor.objectify(json_program.to_dict())

            call_expression = ast_program.body[0].expression
            is_matched = p_utils.match_name_space(call_expression,
                                                  pattern_list)
            return is_matched
Esempio n. 4
0
 def create_program_cache(self, file_path):
     '''Create an AST of a program from a file path
     and saves to cache with file_path as key.
     '''
     with open(file_path, 'r') as f:
         code = f.read()
     json_program = esprima.parse(code, {'loc': True})
     ast_program = visitor.objectify(json_program.to_dict())
     self.program_cache[file_path] = ast_program
Esempio n. 5
0
def linerange(node):
    """Get line number range from a node."""
    strip = {"body": None, "orelse": None, "handlers": None, "finalbody": None}
    for key in strip.keys():
        if hasattr(node, key):
            strip[key] = getattr(node, key)
            setattr(node, key, [])

    lines_min = 9999999999
    lines_max = -1
    for n in visitor.objectify(node).traverse():
        if hasattr(n, 'lineno'):
            lines_min = min(lines_min, n.lineno)
            lines_max = max(lines_max, n.lineno)

    for key in strip.keys():
        if strip[key] is not None:
            setattr(node, key, strip[key])

    if lines_max > -1:
        return list(range(lines_min, lines_max + 1))
    return [0, 1]