示例#1
0
    def testMatchAliasStatement(self):
        matches = set()
        for context in _FindContexts(self.start_token):
            match = scopeutil.MatchAlias(context)
            if match:
                matches.add(match)

        self.assertEquals(
            set([('bar', 'baz'), ('foo', 'this.foo_'),
                 ('Component', 'goog.ui.Component'),
                 ('MyClass', 'myproject.foo.MyClass'),
                 ('NonClosurizedClass', 'aaa.bbb.NonClosurizedClass')]),
            matches)
示例#2
0
    def _ProcessBlock(self, context, global_alias_map):
        """Scans a goog.scope block to find aliases and mark alias tokens."""
        alias_map = global_alias_map.copy()

        # Iterate over every token in the context. Each token points to one
        # context, but multiple tokens may point to the same context. We only want
        # to check each context once, so keep track of those we've seen.
        seen_contexts = set()
        token = context.start_token
        while token and self._IsTokenInParentBlock(token, context):
            token_context = token.metadata.context if token.metadata else None

            # Check to see if this token is an alias.
            if token_context and token_context not in seen_contexts:
                seen_contexts.add(token_context)

                # If this is a alias statement in the goog.scope block.
                if (token_context.type == ecmametadatapass.EcmaContext.VAR
                        and scopeutil.IsGoogScopeBlock(
                            token_context.parent.parent)):
                    match = scopeutil.MatchAlias(token_context)

                    # If this is an alias, remember it in the map.
                    if match:
                        alias, symbol = match
                        symbol = _GetAliasForIdentifier(symbol,
                                                        alias_map) or symbol
                        if scopeutil.IsInClosurizedNamespace(
                                symbol, self._closurized_namespaces):
                            alias_map[alias] = symbol

            # If this token is an identifier that matches an alias,
            # mark the token as an alias to the original symbol.
            if (token.type is
                    javascripttokens.JavaScriptTokenType.SIMPLE_LVALUE
                    or token.type is
                    javascripttokens.JavaScriptTokenType.IDENTIFIER):
                identifier = tokenutil.GetIdentifierForToken(token)
                if identifier:
                    aliased_symbol = _GetAliasForIdentifier(
                        identifier, alias_map)
                    if aliased_symbol:
                        token.metadata.aliased_symbol = aliased_symbol

            elif token.type == javascripttokens.JavaScriptTokenType.DOC_FLAG:
                flag = token.attached_object
                if flag and flag.HasType() and flag.jstype:
                    _SetTypeAlias(flag.jstype, alias_map)

            token = token.next  # Get next token
示例#3
0
    def testMatchAliasStatement_withClosurizedNamespaces(self):

        closurized_namepaces = frozenset(['goog', 'myproject'])

        matches = set()
        for context in _FindContexts(self.start_token):
            match = scopeutil.MatchAlias(context)
            if match:
                unused_alias, symbol = match
                if scopeutil.IsInClosurizedNamespace(symbol,
                                                     closurized_namepaces):
                    matches.add(match)

        self.assertEquals(
            set([('MyClass', 'myproject.foo.MyClass'),
                 ('Component', 'goog.ui.Component')]), matches)
示例#4
0
 def assertAlias(self, expected_match, script):
     start_token = testutil.TokenizeSourceAndRunEcmaPass(script)
     statement = _FindFirstContextOfType(start_token,
                                         ecmametadatapass.EcmaContext.VAR)
     match = scopeutil.MatchAlias(statement)
     self.assertEquals(expected_match, match)
 def assertAlias(self, expected_match, script):
     statement = _ParseAssignment(script)
     match = scopeutil.MatchAlias(statement)
     self.assertEquals(expected_match, match)