示例#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

            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
示例#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

            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
示例#3
0
    def test_parse_redir_with_dot(self):
        parser = Parser()
        redir_cmd_node = {
            'type': NodeType.EXCMD.value,
            'ea': {
                'argpos': {
                    'col': 7,
                    'i': 6,
                    'lnum': 1
                },
            },
            'str': 'redir => s:dict.redir',
        }
        ast = parser.parse_redir(redir_cmd_node)

        expected_pos = {
            'col': 16,
            'i': 15,
            'lnum': 1,
            'offset': 11,
        }
        expected_node_type = NodeType.DOT

        self.assertEqual(expected_node_type, NodeType(ast['type']))
        self.assertEqual(expected_pos, ast['pos'])
示例#4
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
示例#5
0
    def assertProcessing(self, file_path, expected_scope_tree):
        parser = Parser()
        ast = parser.parse_file(file_path)

        plugin = ScopePlugin()
        plugin.process(ast)

        self.assertScopeTree(ast['vint_scope_tree'], expected_scope_tree)
示例#6
0
    def create_ast(self, file_path):
        parser = Parser()
        ast = parser.parse_file(file_path)

        id_classifier = IdentifierClassifier()
        attached_ast = id_classifier.attach_identifier_attributes(ast)

        return attached_ast
示例#7
0
    def assertProcessing(self, file_path, expected_scope_tree):
        parser = Parser()
        ast = parser.parse_file(file_path)

        plugin = ScopePlugin()
        plugin.process(ast)

        self.assertScopeTree(ast['vint_scope_tree'], expected_scope_tree)
示例#8
0
    def create_ast(self, file_path):
        parser = Parser()
        ast = parser.parse(LintTargetFile(file_path))

        id_classifier = IdentifierClassifier()
        attached_ast = id_classifier.attach_identifier_attributes(ast)

        return attached_ast
示例#9
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
示例#10
0
    def build_parser(self):
        config_dict = self._config.get_config_dict()
        enable_neovim = get_config_value(config_dict,
                                         ['cmdargs', 'env', 'neovim'], False)

        parser = Parser(self._plugins, enable_neovim=enable_neovim)
        return parser
示例#11
0
        def enter_handler(node):
            node_type = NodeType(node['type'])
            if node_type is not NodeType.EXCMD:
                return

            is_redir_command = node['ea']['cmd'].get('name') == 'redir'
            if not is_redir_command:
                return

            redir_cmd_str = node['str']
            is_redir_assignment = '=>' in redir_cmd_str
            if not is_redir_assignment:
                return

            parser = Parser()
            redir_content_node = parser.parse_redir(node)
            node[REDIR_CONTENT] = redir_content_node
示例#12
0
        def enter_handler(node):
            node_type = NodeType(node['type'])
            if node_type is not NodeType.EXCMD:
                return

            is_redir_command = node['ea']['cmd'].get('name') == 'redir'
            if not is_redir_command:
                return

            redir_cmd_str = node['str']
            is_redir_assignment = '=>' in redir_cmd_str
            if not is_redir_assignment:
                return

            parser = Parser()
            redir_content_node = parser.parse_redir(node)
            node[REDIR_CONTENT] = redir_content_node
示例#13
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'])
示例#14
0
    def test_process_with_builtin(self):
        parser = Parser()
        ast = parser.parse_file(Fixtures['BUILTIN'])

        plugin = ScopePlugin()
        plugin.process(ast)

        expected_builtin_flags = {
            'abs': True,
            'sin': True,
            'strlen': True,
            'g:MyFunction': False,
        }

        # Keep identifier name that traverser visited
        identifiers_checking_map = {
            'abs': False,
            'sin': False,
            'strlen': False,
            'g:MyFunction': False,
        }

        def test_identifier(node):
            if NodeType(node['type']) is not NodeType.IDENTIFIER:
                return

            identifier = node

            # We focus to non-definition identifier
            if identifier[ScopePlugin.DEFINITION_IDENTIFIER_FLAG_KEY]:
                return

            identifier_name = identifier['value']
            identifiers_checking_map[identifier_name] = True

            is_builtin_identifier = identifier[
                ScopePlugin.BUILTIN_IDENTIFIER_FLAG_KEY]
            expected_builtin_flag = expected_builtin_flags[identifier_name]

            self.assertEqual(is_builtin_identifier, expected_builtin_flag)

        traverse(ast, on_enter=test_identifier)
        self.assertTrue(all(identifiers_checking_map.values()))
示例#15
0
    def test_process_with_builtin(self):
        parser = Parser()
        ast = parser.parse_file(Fixtures['BUILTIN'])

        plugin = ScopePlugin()
        plugin.process(ast)

        expected_builtin_flags = {
            'abs': True,
            'sin': True,
            'strlen': True,
            'g:MyFunction': False,
        }

        # Keep identifier name that traverser visited
        identifiers_checking_map = {
            'abs': False,
            'sin': False,
            'strlen': False,
            'g:MyFunction': False,
        }

        def test_identifier(node):
            if NodeType(node['type']) is not NodeType.IDENTIFIER:
                return

            identifier = node

            # We focus to non-definition identifier
            if identifier[ScopePlugin.DEFINITION_IDENTIFIER_FLAG_KEY]:
                return

            identifier_name = identifier['value']
            identifiers_checking_map[identifier_name] = True

            is_builtin_identifier = identifier[ScopePlugin.BUILTIN_IDENTIFIER_FLAG_KEY]
            expected_builtin_flag = expected_builtin_flags[identifier_name]

            self.assertEqual(is_builtin_identifier, expected_builtin_flag)

        traverse(ast, on_enter=test_identifier)
        self.assertTrue(all(identifiers_checking_map.values()))
示例#16
0
    def test_parse_redir_with_identifier(self):
        parser = Parser()
        redir_cmd_node = {
            'type': NodeType.EXCMD.value,
            'ea': {
                'argpos': {
                    'col': 6,
                    'i': 5,
                    'lnum': 1
                },
            },
            'str': 'redir=>redir',
        }
        ast = parser.parse_redir(redir_cmd_node)

        expected_pos = {'col': 8, 'i': 7, 'lnum': 1, 'offset': 5}
        expected_node_type = NodeType.IDENTIFIER

        self.assertEqual(expected_node_type, NodeType(ast['type']))
        self.assertEqual(expected_pos, ast['pos'])
示例#17
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
示例#18
0
    def test_parse_redir_with_identifier(self):
        parser = Parser()
        redir_cmd_node = {
            'type': NodeType.EXCMD.value,
            'ea': {
                'argpos': {'col': 6, 'i': 5, 'lnum': 1},
            },
            'str': 'redir=>redir',
        }
        ast = parser.parse_redir(redir_cmd_node)

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

        self.assertEqual(expected_node_type, NodeType(ast['type']))
        self.assertEqual(expected_pos, ast['pos'])
示例#19
0
    def test_parse_redir_with_dot(self):
        parser = Parser()
        redir_cmd_node = {
            'type': NodeType.EXCMD.value,
            'ea': {
                'argpos': {'col': 7, 'i': 6, 'lnum': 1},
            },
            'str': 'redir => s:dict.redir',
        }
        ast = parser.parse_redir(redir_cmd_node)

        expected_pos = {
            'col': 16,
            'i': 15,
            'lnum': 1,
        }
        expected_node_type = NodeType.DOT

        self.assertEqual(expected_node_type, NodeType(ast['type']))
        self.assertEqual(expected_pos, ast['pos'])
示例#20
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
示例#21
0
 def create_ast(self, file_path):
     parser = Parser()
     lint_target = LintTargetFile(file_path.value)
     ast = parser.parse(lint_target)
     return ast
示例#22
0
 def test_parse_file(self):
     parser = Parser()
     ast = parser.parse_file(FIXTURE_FILE)
     self.assertIs(ast['type'], 1)
示例#23
0
def prettify_node_type(node):
    node['type'] = NodeType(node['type'])


if __name__ == '__main__':
    arg_parser = ArgumentParser(prog='show_ast', description='Show AST')
    arg_parser.add_argument('--enable-neovim',
                            action='store_true',
                            help='Enable Neovim syntax')
    arg_parser.add_argument('files', nargs='*', help='File to parse')
    namespace = vars(arg_parser.parse_args(sys.argv[1:]))

    filepaths = map(Path, namespace['files'])
    enable_neovim = namespace['enable_neovim']

    scope_plugin = ScopePlugin()
    parser = Parser(plugins={'scope': scope_plugin},
                    enable_neovim=enable_neovim)

    for filepath in filepaths:
        ast = parser.parse_file(filepath)
        traverse(ast, on_enter=prettify_node_type)

        print("////////// AST //////////\n")
        pprint(ast)
        print("\n\n")

        print("////////// SCOPE TREE //////////\n")
        pprint(scope_plugin._ref_tester._scope_tree)
示例#24
0
 def create_ast(self, file_path):
     parser = Parser()
     lint_target = LintTargetFile(file_path.value)
     ast = parser.parse(lint_target)
     return ast
示例#25
0
from vint.ast.plugin.scope_plugin import ScopePlugin


def prettify_node_type(node):
    node['type'] = NodeType(node['type'])


if __name__ == '__main__':
    arg_parser = ArgumentParser(prog='show_ast', description='Show AST')
    arg_parser.add_argument('--enable-neovim', action='store_true', help='Enable Neovim syntax')
    arg_parser.add_argument('files', nargs='*', help='File to parse')
    namespace = vars(arg_parser.parse_args(sys.argv[1:]))

    filepaths = map(Path, namespace['files'])
    enable_neovim = namespace['enable_neovim']

    scope_plugin = ScopePlugin()
    parser = Parser(plugins=[scope_plugin], enable_neovim=enable_neovim)

    for filepath in filepaths:
        ast = parser.parse_file(filepath)
        traverse(ast, on_enter=prettify_node_type)

        print("////////// SCOPE TREE //////////\n")
        pprint(scope_plugin._ref_tester._scope_linker.scope_tree)
        print("\n\n")

        print("////////// LINK REGISTRY //////////\n")
        pprint(scope_plugin._ref_tester._scope_linker.link_registry._vars_to_declarative_ids_map)
        pprint(scope_plugin._ref_tester._scope_linker.link_registry._ids_to_scopes_map)
示例#26
0
 def create_ast(self, file_path):
     parser = Parser()
     ast = parser.parse_file(file_path.value)
     return ast
示例#27
0
文件: show_ast.py 项目: rhysd/vint
#! /usr/bin/env python
import sys
from pprint import pprint
from pathlib import Path

vint_root = Path(__file__).parent.parent
sys.path.append(str(vint_root))

from vint.ast.node_type import NodeType
from vint.ast.parsing import Parser
from vint.ast.traversing import traverse


def prettify_node_type(node):
    node['type'] = NodeType(node['type'])


if __name__ == '__main__':
    parser = Parser()

    ast = parser.parse_file(Path(sys.argv[1]))

    traverse(ast, on_enter=prettify_node_type)

    pprint(ast)
示例#28
0
 def create_ast(self, file_path):
     parser = Parser()
     ast = parser.parse(LintTargetFile(file_path.value))
     return ast
示例#29
0
vint_root = Path(__file__).resolve().parent.parent
sys.path.append(str(vint_root))

from vint.ast.node_type import NodeType
from vint.ast.traversing import traverse
from vint.ast.parsing import Parser


def prettify_node_type(node):
    node['type'] = NodeType(node['type'])


if __name__ == '__main__':
    arg_parser = ArgumentParser(prog='show_ast', description='Show AST')
    arg_parser.add_argument('--enable-neovim',
                            action='store_true',
                            help='Enable Neovim syntax')
    arg_parser.add_argument('files', nargs='*', help='File to parse')
    namespace = vars(arg_parser.parse_args(sys.argv[1:]))

    filepaths = map(Path, namespace['files'])
    enable_neovim = namespace['enable_neovim']

    parser = Parser(enable_neovim=enable_neovim)

    for filepath in filepaths:
        ast = parser.parse_file(filepath)
        traverse(ast, on_enter=prettify_node_type)
        pprint(ast)
示例#30
0
 def test_parse_file_when_neovim_enabled(self):
     parser = Parser(enable_neovim=True)
     ast = parser.parse(LintTargetFile(FIXTURE_FILE_NEOVIM))
     self.assertIs(ast['type'], 1)
示例#31
0
 def test_parse_empty_file(self):
     parser = Parser()
     ast = parser.parse(LintTargetFile(FIXTURE_FILE_EMPTY))
     self.assertIs(ast['type'], 1)
示例#32
0
 def setUp(self):
     parser = Parser()
     self.ast = parser.parse(LintTargetFile(FIXTURE_FILE))
示例#33
0
 def setUp(self):
     parser = Parser()
     self.ast = parser.parse_file(FIXTURE_FILE)
示例#34
0
 def setUp(self):
     parser = Parser()
     self.ast = parser.parse(LintTargetFile(FIXTURE_FILE))
示例#35
0
 def setUp(self):
     parser = Parser()
     self.ast = parser.parse_file(FIXTURE_FILE)
示例#36
0
 def test_parse_file_on_ff_dos_and_fenc_cp932(self):
     parser = Parser()
     ast = parser.parse(LintTargetFile(FIXTURE_FILE_FF_DOS_FENC_CP932))
     self.assertIs(ast['type'], 1)
示例#37
0
 def create_ast(self, file_path):
     parser = Parser()
     ast = parser.parse_file(file_path.value)
     return ast
示例#38
0
    def build_parser(self):
        # plugins = [ScopePlugin()]

        parser = Parser()
        return parser