Esempio n. 1
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
Esempio n. 2
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)
Esempio n. 3
0
    def _ProcessRootContext(self, root_context):
        """Processes all goog.scope blocks under the root context."""

        assert root_context.type is ecmametadatapass.EcmaContext.ROOT

        # Process aliases in statements in the root scope for goog.module-style
        # aliases.
        global_alias_map = {}
        for context in root_context.children:
            if context.type == ecmametadatapass.EcmaContext.STATEMENT:
                for statement_child in context.children:
                    if statement_child.type == ecmametadatapass.EcmaContext.VAR:
                        match = scopeutil.MatchModuleAlias(statement_child)
                        if match:
                            # goog.require aliases cannot use further aliases, the symbol is
                            # the second part of match, directly.
                            symbol = match[1]
                            if scopeutil.IsInClosurizedNamespace(
                                    symbol, self._closurized_namespaces):
                                global_alias_map[match[0]] = symbol

        # Process each block to find aliases.
        for context in root_context.children:
            self._ProcessBlock(context, global_alias_map)