Ejemplo n.º 1
0
        def enter_handler(node):
            node_type = NodeType(node['type'])
            if node_type is not NodeType.CALL:
                return

            called_function_identifier = node['left']

            # Name node of the "map" or "filter" functions are always IDENTIFIER.
            if NodeType(called_function_identifier['type']
                        ) is not NodeType.IDENTIFIER:
                return

            is_map_or_function_call = called_function_identifier.get(
                'value') in {
                    'map': True,
                    'filter': True,
                }

            if not is_map_or_function_call:
                return

            string_expr_node = node['rlist'][1]

            # We can analyze only STRING nodes by static analyzing.
            if NodeType(string_expr_node['type']) is not NodeType.STRING:
                return

            parser = Parser()
            string_expr_content_nodes = parser.parse_string_expr(
                string_expr_node)
            node[STRING_EXPR_CONTENT] = string_expr_content_nodes
Ejemplo n.º 2
0
        def enter_handler(node):
            node_type = NodeType(node['type'])
            if node_type is not NodeType.CALL:
                return

            called_function_identifier = node['left']

            # Name node of the "map" or "filter" functions are always IDENTIFIER.
            if NodeType(called_function_identifier['type']) is not NodeType.IDENTIFIER:
                return

            is_map_or_function_call = called_function_identifier.get('value') in {
                'map': True,
                'filter': True,
            }

            if not is_map_or_function_call:
                return

            args = node['rlist']

            # Prevent crash. See https://github.com/Kuniwak/vint/issues/256.
            if len(args) < 2:
                return

            string_expr_node = args[1]

            # We can analyze only STRING nodes by static analyzing.
            if NodeType(string_expr_node['type']) is not NodeType.STRING:
                return

            parser = Parser()
            string_expr_content_nodes = parser.parse_string_expr(string_expr_node)
            node[STRING_EXPR_CONTENT] = string_expr_content_nodes
Ejemplo n.º 3
0
        def enter_handler(node):
            node_type = NodeType(node['type'])
            if node_type is not NodeType.CALL:
                return

            called_function_identifier = node['left']

            # Name node of the "map" or "filter" functions are always IDENTIFIER.
            if NodeType(called_function_identifier['type']) is not NodeType.IDENTIFIER:
                return

            is_map_or_function_call = called_function_identifier.get('value') in {
                'map': True,
                'filter': True,
            }

            if not is_map_or_function_call:
                return

            string_expr_node = node['rlist'][1]

            # We can analyze only STRING nodes by static analyzing.
            if NodeType(string_expr_node['type']) is not NodeType.STRING:
                return

            parser = Parser()
            string_expr_content_nodes = parser.parse_string_expr(string_expr_node)
            node[STRING_EXPR_CONTENT] = string_expr_content_nodes
Ejemplo n.º 4
0
    def _parse_string_expr_content_nodes(cls, string_expr_node):
        parser = Parser()
        string_expr_content_nodes = parser.parse_string_expr(string_expr_node)

        def enter_handler(node):
            # NOTE: We need this flag only string nodes, because this flag is only for
            # ProhibitUnnecessaryDoubleQuote.
            if NodeType(node['type']) is NodeType.STRING:
                node[STRING_EXPR_CONTEXT] = {
                    'is_on_str_expr_context': True,
                }

        for string_expr_content_node in string_expr_content_nodes:
            traverse(string_expr_content_node, on_enter=enter_handler)

        return string_expr_content_nodes
Ejemplo n.º 5
0
    def test_parse_string_expr(self):
        parser = Parser()
        redir_cmd_node = {
            'type': NodeType.STRING.value,
            'pos': {'col': 1, 'i': 1, 'lnum': 1},
            'value': '\'v:key ==# "a"\'',
        }
        nodes = parser.parse_string_expr(redir_cmd_node)

        expected_pos = {
            'col': 7,
            'i': 7,
            'lnum': 1,
        }
        expected_node_type = NodeType.EQUALCS

        self.assertEqual(expected_node_type, NodeType(nodes[0]['type']))
        self.assertEqual(expected_pos, nodes[0]['pos'])
Ejemplo n.º 6
0
    def _attach_string_expr_content_to_map_or_func(self, map_or_func_call_node):
        args = map_or_func_call_node['rlist']

        # Prevent crash. See https://github.com/Kuniwak/vint/issues/256.
        if len(args) < 2:
            return

        string_expr_node = args[1]

        # We can statically analyze only STRING nodes
        if NodeType(string_expr_node['type']) is not NodeType.STRING:
            return

        parser = Parser()
        string_expr_content_nodes = parser.parse_string_expr(string_expr_node)

        # Set a flag that means whether the expression is in other string literals.
        CallNodeParser._set_string_expr_context_flag(string_expr_content_nodes)

        string_expr_node[LAMBDA_STRING_EXPR_CONTENT] = string_expr_content_nodes
Ejemplo n.º 7
0
    def _attach_string_expr_content_to_call_or_function(self, call_call_node):
        args = call_call_node['rlist']

        if len(args) < 1:
            return

        # We can statically analyze only STRING node
        string_expr_node = args[0]

        if NodeType(string_expr_node['type']) is not NodeType.STRING:
            return

        parser = Parser()
        string_expr_content_nodes = parser.parse_string_expr(string_expr_node)

        func_ref_nodes = list(filter(
            lambda node: NodeType(node['type']) is NodeType.IDENTIFIER,
            string_expr_content_nodes
        ))

        string_expr_node[FUNCTION_REFERENCE_STRING_EXPR_CONTENT] = func_ref_nodes