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
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
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'])
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
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)
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
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
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
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
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
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'])
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()))
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()))
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'])
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
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'])
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'])
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
def create_ast(self, file_path): parser = Parser() lint_target = LintTargetFile(file_path.value) ast = parser.parse(lint_target) return ast
def test_parse_file(self): parser = Parser() ast = parser.parse_file(FIXTURE_FILE) self.assertIs(ast['type'], 1)
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)
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)
def create_ast(self, file_path): parser = Parser() ast = parser.parse_file(file_path.value) return ast
#! /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)
def create_ast(self, file_path): parser = Parser() ast = parser.parse(LintTargetFile(file_path.value)) return ast
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)
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)
def test_parse_empty_file(self): parser = Parser() ast = parser.parse(LintTargetFile(FIXTURE_FILE_EMPTY)) self.assertIs(ast['type'], 1)
def setUp(self): parser = Parser() self.ast = parser.parse(LintTargetFile(FIXTURE_FILE))
def setUp(self): parser = Parser() self.ast = parser.parse_file(FIXTURE_FILE)
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)
def build_parser(self): # plugins = [ScopePlugin()] parser = Parser() return parser