Esempio n. 1
0
    def test_attach_identifier_attributes_with_destructuring_assignment(self):
        ast = self.create_ast(Fixtures['DESTRUCTURING_ASSIGNMENT'])
        id_classifier = IdentifierClassifier()

        expected_id_attr_map = {
            'g:for_var1':
            self.create_id_attr(is_declarative=True),
            'g:for_var2':
            self.create_id_attr(is_declarative=True),
            'g:let_var1':
            self.create_id_attr(is_declarative=True),
            'g:let_var2':
            self.create_id_attr(is_declarative=True),
            'g:let_var3':
            self.create_id_attr(is_declarative=True),
            'g:rest':
            self.create_id_attr(is_declarative=True),
            'g:list':
            self.create_id_attr(is_declarative=False),
            '1':
            self.create_id_attr(is_declarative=True, is_member=True),
            'g:index_end':
            self.create_id_attr(is_declarative=False, is_dynamic=True),
        }

        attached_ast = id_classifier.attach_identifier_attributes(ast)

        self.assertAttributesInIdentifiers(attached_ast, expected_id_attr_map)
Esempio n. 2
0
    def test_attach_identifier_attributes_with_calling_func(self):
        ast = self.create_ast(Fixtures['CALLING_FUNC'])
        id_classifier = IdentifierClassifier()

        expected_id_attr_map = {
            'FunctionCall':
            self.create_id_attr(is_declarative=False, is_function=True),
            'autoload#AutoloadFunctionCall':
            self.create_id_attr(is_declarative=False,
                                is_autoload=True,
                                is_function=True),
            'dot':
            self.create_id_attr(is_declarative=False),
            'DotFunctionCall':
            self.create_id_attr(is_declarative=False,
                                is_member=True,
                                is_function=True),
            'subscript':
            self.create_id_attr(is_declarative=False),
            "'SubscriptFunctionCall'":
            self.create_id_attr(is_declarative=False,
                                is_member=True,
                                is_function=True),
            'FunctionCallInExpressionContext':
            self.create_id_attr(is_declarative=False, is_function=True),
            'FunctionToBeDeleted':
            self.create_id_attr(is_declarative=False, is_function=True),
        }

        attached_ast = id_classifier.attach_identifier_attributes(ast)

        self.assertAttributesInIdentifiers(attached_ast, expected_id_attr_map)
Esempio n. 3
0
    def test_attach_identifier_attributes_with_declaring_with_dict_key(self):
        ast = self.create_ast(Fixtures['DICT_KEY'])
        id_classifier = IdentifierClassifier()

        expected_id_attr_map = {
            'g:dict':
            self.create_id_attr(is_declarative=False),
            'Function1':
            self.create_id_attr(is_declarative=True,
                                is_member=True,
                                is_function=True),
            "'Function2'":
            self.create_id_attr(is_declarative=True,
                                is_member=True,
                                is_function=True),
            'key1':
            self.create_id_attr(is_declarative=True,
                                is_member=True,
                                is_function=False),
            "'key2'":
            self.create_id_attr(is_declarative=True,
                                is_member=True,
                                is_function=False),
            "g:key3":
            self.create_id_attr(is_declarative=False,
                                is_member=False,
                                is_function=False),
        }

        attached_ast = id_classifier.attach_identifier_attributes(ast)

        self.assertAttributesInIdentifiers(attached_ast, expected_id_attr_map)
Esempio n. 4
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
Esempio n. 5
0
    def test_attach_identifier_attributes_with_arithmetic_assignment(self):
        ast = self.create_ast(Fixtures['ARITHMETIC_ASSIGNMENT'])
        id_classifier = IdentifierClassifier()

        expected_id_attr_map = {
            'g:variable_defined': self.create_id_attr(),
        }

        attached_ast = id_classifier.attach_identifier_attributes(ast)

        self.assertAttributesInIdentifiers(attached_ast, expected_id_attr_map)
Esempio n. 6
0
    def test_attach_identifier_attributes_with_redir(self):
        ast = self.create_ast(Fixtures['REDIR'])
        id_classifier = IdentifierClassifier()

        expected_id_attr_map = {
            'g:var': self.create_id_attr(is_declarative=True),
        }

        attached_ast = id_classifier.attach_identifier_attributes(ast)

        self.assertAttributesInIdentifiers(attached_ast, expected_id_attr_map)
Esempio n. 7
0
    def test_attach_identifier_attributes_with_map_func(self):
        ast = self.create_ast(Fixtures['MAP_FUNC'])
        id_classifier = IdentifierClassifier()

        expected_id_attr_map = {
            'v:val': self.create_id_attr(is_on_str_expr_context=True),
            'g:pattern': self.create_id_attr(is_on_str_expr_context=True),
            'map': self.create_id_attr(is_function=True),
        }

        attached_ast = id_classifier.attach_identifier_attributes(ast)

        self.assertAttributesInIdentifiers(attached_ast, expected_id_attr_map)
Esempio n. 8
0
    def test_attach_identifier_attributes_with_loop_var(self):
        ast = self.create_ast(Fixtures['LOOP_VAR'])
        id_classifier = IdentifierClassifier()

        expected_id_attr_map = {
            'implicit_global_loop_var':
            self.create_id_attr(is_declarative=True),
            'g:array': self.create_id_attr(is_declarative=False),
        }

        attached_ast = id_classifier.attach_identifier_attributes(ast)

        self.assertAttributesInIdentifiers(attached_ast, expected_id_attr_map)
Esempio n. 9
0
    def test_issue_274_curlyname_complex(self):
        ast = self.create_ast(Fixtures['ISSUE_274_CURLYNAME_COMPLEX'])
        id_classifier = IdentifierClassifier()

        attached_ast = id_classifier.attach_identifier_attributes(ast)

        curlyname_node = attached_ast['body'][0]['body'][1]['right']

        # For debugging.
        pprint(curlyname_node)

        self.assertTrue(curlyname_node[IDENTIFIER_ATTRIBUTE]
                        [IDENTIFIER_ATTRIBUTE_DYNAMIC_FLAG])
Esempio n. 10
0
    def test_attach_identifier_attributes_with_declaring_func_in_func(self):
        ast = self.create_ast(Fixtures['DECLARING_FUNC_IN_FUNC'])
        id_classifier = IdentifierClassifier()

        expected_id_attr_map = {
            'FuncContext':
            self.create_id_attr(is_declarative=True, is_function=True),
            'ImplicitGlobalFunc':
            self.create_id_attr(is_declarative=True, is_function=True),
        }

        attached_ast = id_classifier.attach_identifier_attributes(ast)

        self.assertAttributesInIdentifiers(attached_ast, expected_id_attr_map)
Esempio n. 11
0
    def process(self, ast):  # type: (Dict[str, Any]) -> None
        """ Build a scope tree and links between scopes and identifiers by the
        specified ast. You can access the built scope tree and the built links
        by .scope_tree and .link_registry.
        """
        id_classifier = IdentifierClassifier()
        attached_ast = id_classifier.attach_identifier_attributes(ast)

        # We are already in script local scope.
        self._scope_tree_builder.enter_new_scope(ScopeVisibility.SCRIPT_LOCAL)

        traverse(attached_ast,
                 on_enter=self._enter_handler,
                 on_leave=self._leave_handler)

        self.scope_tree = self._scope_tree_builder.get_global_scope()
        self.link_registry = self._scope_tree_builder.link_registry
Esempio n. 12
0
    def test_attach_identifier_attributes_with_declaring_var(self):
        ast = self.create_ast(Fixtures['DECLARING_VAR'])
        id_classifier = IdentifierClassifier()

        expected_id_attr_map = {
            'g:explicit_global_var': self.create_id_attr(is_declarative=True),
            'b:buffer_local_var': self.create_id_attr(is_declarative=True),
            'w:window_local_var': self.create_id_attr(is_declarative=True),
            't:tab_local_var': self.create_id_attr(is_declarative=True),
            's:script_local_var': self.create_id_attr(is_declarative=True),
            'implicit_global_var': self.create_id_attr(is_declarative=True),
            '$ENV_VAR': self.create_id_attr(is_declarative=True),
            '@"': self.create_id_attr(is_declarative=True),
            '&opt_var': self.create_id_attr(is_declarative=True),
            'v:count': self.create_id_attr(is_declarative=True),
        }

        attached_ast = id_classifier.attach_identifier_attributes(ast)

        self.assertAttributesInIdentifiers(attached_ast, expected_id_attr_map)
Esempio n. 13
0
    def test_lambda(self):
        ast = self.create_ast(Fixtures['LAMBDA'])
        id_classifier = IdentifierClassifier()

        attached_ast = id_classifier.attach_identifier_attributes(ast)

        expected_id_attr_map = {
            'i':
            self.create_id_attr(is_declarative=True, is_lambda_argument=True),
            'y':
            self.create_id_attr(is_on_lambda_body=True),
            'map':
            self.create_id_attr(is_function=True),
            '...':
            self.create_id_attr(is_declarative=True,
                                is_lambda_argument=True,
                                is_variadic=True),
        }

        self.assertAttributesInIdentifiers(attached_ast, expected_id_attr_map)
Esempio n. 14
0
    def test_attach_identifier_attributes_with_func_param(self):
        ast = self.create_ast(Fixtures['FUNC_PARAM'])
        id_classifier = IdentifierClassifier()

        expected_id_attr_map = {
            'g:FunctionWithNoParams':
            self.create_id_attr(is_declarative=True, is_function=True),
            'g:FunctionWithOneParam':
            self.create_id_attr(is_declarative=True, is_function=True),
            'param':
            self.create_id_attr(is_declarative=True,
                                is_declarative_parameter=True),
            'g:FunctionWithTwoParams':
            self.create_id_attr(is_declarative=True, is_function=True),
            'param1':
            self.create_id_attr(is_declarative=True,
                                is_declarative_parameter=True),
            'param2':
            self.create_id_attr(is_declarative=True,
                                is_declarative_parameter=True),
            'g:FunctionWithVarParams':
            self.create_id_attr(is_declarative=True, is_function=True),
            'g:FunctionWithParamsAndVarParams':
            self.create_id_attr(is_declarative=True, is_function=True),
            'param_var1':
            self.create_id_attr(is_declarative=True,
                                is_declarative_parameter=True),
            'g:FunctionWithRange':
            self.create_id_attr(is_declarative=True, is_function=True),
            '...':
            self.create_id_attr(is_declarative=True,
                                is_declarative_parameter=True,
                                is_variadic=True),
            'g:FunctionWithDict':
            self.create_id_attr(is_declarative=True, is_function=True),
        }

        attached_ast = id_classifier.attach_identifier_attributes(ast)

        self.assertAttributesInIdentifiers(attached_ast, expected_id_attr_map)