<s> """<STR_LIT>""" <EOL> import re <EOL> from closure_linter import javascripttokens <EOL> from closure_linter import tokenutil <EOL> TokenType = javascripttokens . JavaScriptTokenType <EOL> DEFAULT_EXTRA_NAMESPACES = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> class UsedNamespace ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , namespace , identifier , token , alias_definition ) : <EOL> """<STR_LIT>""" <EOL> self . namespace = namespace <EOL> self . identifier = identifier <EOL> self . token = token <EOL> self . alias_definition = alias_definition <EOL> def GetLine ( self ) : <EOL> return self . token . line_number <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( <EOL> [ '<STR_LIT>' % ( k , repr ( v ) ) for k , v in self . __dict__ . iteritems ( ) ] ) <EOL> class ClosurizedNamespacesInfo ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , closurized_namespaces , ignored_extra_namespaces ) : <EOL> """<STR_LIT>""" <EOL> self . _closurized_namespaces = closurized_namespaces <EOL> self . _ignored_extra_namespaces = ( ignored_extra_namespaces + <EOL> DEFAULT_EXTRA_NAMESPACES ) <EOL> self . Reset ( ) <EOL> def Reset ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _provide_tokens = [ ] <EOL> self . _require_tokens = [ ] <EOL> self . _provided_namespaces = [ ] <EOL> self . _required_namespaces = [ ] <EOL> self . _created_namespaces = [ ] <EOL> self . _used_namespaces = [ ] <EOL> self . _suppressed_requires = [ ] <EOL> self . _duplicate_provide_tokens = [ ] <EOL> self . _duplicate_require_tokens = [ ] <EOL> self . _scopified_file = False <EOL> def GetProvidedNamespaces ( self ) : <EOL> """<STR_LIT>""" <EOL> return set ( self . _provided_namespaces ) <EOL> def GetRequiredNamespaces ( self ) : <EOL> """<STR_LIT>""" <EOL> return set ( self . _required_namespaces ) <EOL> def IsExtraProvide ( self , token ) : <EOL> """<STR_LIT>""" <EOL> namespace = tokenutil . GetStringAfterToken ( token ) <EOL> if self . GetClosurizedNamespace ( namespace ) is None : <EOL> return False <EOL> if token in self . _duplicate_provide_tokens : <EOL> return True <EOL> for created_namespace , created_identifier , _ in self . _created_namespaces : <EOL> if namespace == created_namespace or namespace == created_identifier : <EOL> return False <EOL> return True <EOL> def IsExtraRequire ( self , token ) : <EOL> """<STR_LIT>""" <EOL> namespace = tokenutil . GetStringAfterToken ( token ) <EOL> if self . GetClosurizedNamespace ( namespace ) is None : <EOL> return False <EOL> if namespace in self . _ignored_extra_namespaces : <EOL> return False <EOL> if token in self . _duplicate_require_tokens : <EOL> return True <EOL> if namespace in self . _suppressed_requires : <EOL> return False <EOL> parts = namespace . split ( '<STR_LIT:.>' ) <EOL> if len ( parts ) > <NUM_LIT:1> and parts [ - <NUM_LIT:2> ] [ <NUM_LIT:0> ] . isupper ( ) : <EOL> return True <EOL> for ns in self . _used_namespaces : <EOL> if ( not ns . alias_definition and ( <EOL> namespace == ns . namespace or namespace == ns . identifier ) ) : <EOL> return False <EOL> return True <EOL> def GetMissingProvides ( self ) : <EOL> """<STR_LIT>""" <EOL> missing_provides = dict ( ) <EOL> for namespace , identifier , line_number in self . _created_namespaces : <EOL> if ( not self . _IsPrivateIdentifier ( identifier ) and <EOL> namespace not in self . _provided_namespaces and <EOL> identifier not in self . _provided_namespaces and <EOL> namespace not in self . _required_namespaces and <EOL> namespace not in missing_provides ) : <EOL> missing_provides [ namespace ] = line_number <EOL> return missing_provides <EOL> def GetMissingRequires ( self ) : <EOL> """<STR_LIT>""" <EOL> external_dependencies = set ( self . _required_namespaces ) <EOL> external_dependencies . add ( '<STR_LIT>' ) <EOL> external_dependencies . add ( '<STR_LIT>' ) <EOL> created_identifiers = set ( ) <EOL> for unused_namespace , identifier , unused_line_number in ( <EOL> self . _created_namespaces ) : <EOL> created_identifiers . add ( identifier ) <EOL> missing_requires = dict ( ) <EOL> illegal_alias_statements = dict ( ) <EOL> def ShouldRequireNamespace ( namespace , identifier ) : <EOL> """<STR_LIT>""" <EOL> return ( <EOL> not self . _IsPrivateIdentifier ( identifier ) and <EOL> namespace not in external_dependencies and <EOL> namespace not in self . _provided_namespaces and <EOL> identifier not in external_dependencies and <EOL> identifier not in created_identifiers and <EOL> namespace not in missing_requires ) <EOL> for ns in self . _used_namespaces : <EOL> namespace = ns . namespace <EOL> identifier = ns . identifier <EOL> if ( not ns . alias_definition and <EOL> ShouldRequireNamespace ( namespace , identifier ) ) : <EOL> missing_requires [ namespace ] = ns . GetLine ( ) <EOL> for ns in self . _used_namespaces : <EOL> if ( not ns . alias_definition or <EOL> not ShouldRequireNamespace ( ns . namespace , ns . identifier ) ) : <EOL> continue <EOL> if self . _FindNamespace ( ns . identifier , self . _provided_namespaces , <EOL> created_identifiers , external_dependencies , <EOL> missing_requires ) : <EOL> continue <EOL> namespace = ns . identifier . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> illegal_alias_statements [ namespace ] = ns . token <EOL> return missing_requires , illegal_alias_statements <EOL> def _FindNamespace ( self , identifier , * namespaces_list ) : <EOL> """<STR_LIT>""" <EOL> identifier = identifier . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> identifier_prefix = identifier + '<STR_LIT:.>' <EOL> for namespaces in namespaces_list : <EOL> for namespace in namespaces : <EOL> if namespace == identifier or namespace . startswith ( identifier_prefix ) : <EOL> return namespace <EOL> return None <EOL> def _IsPrivateIdentifier ( self , identifier ) : <EOL> """<STR_LIT>""" <EOL> pieces = identifier . split ( '<STR_LIT:.>' ) <EOL> for piece in pieces : <EOL> if piece . endswith ( '<STR_LIT:_>' ) : <EOL> return True <EOL> return False <EOL> def IsFirstProvide ( self , token ) : <EOL> """<STR_LIT>""" <EOL> return self . _provide_tokens and token == self . _provide_tokens [ <NUM_LIT:0> ] <EOL> def IsFirstRequire ( self , token ) : <EOL> """<STR_LIT>""" <EOL> return self . _require_tokens and token == self . _require_tokens [ <NUM_LIT:0> ] <EOL> def IsLastProvide ( self , token ) : <EOL> """<STR_LIT>""" <EOL> return self . _provide_tokens and token == self . _provide_tokens [ - <NUM_LIT:1> ] <EOL> def IsLastRequire ( self , token ) : <EOL> """<STR_LIT>""" <EOL> return self . _require_tokens and token == self . _require_tokens [ - <NUM_LIT:1> ] <EOL> def ProcessToken ( self , token , state_tracker ) : <EOL> """<STR_LIT>""" <EOL> if token . type == TokenType . IDENTIFIER : <EOL> whole_identifier_string = tokenutil . GetIdentifierForToken ( token ) <EOL> if whole_identifier_string is None : <EOL> return <EOL> if token . string == '<STR_LIT>' and not state_tracker . InFunction ( ) : <EOL> self . _require_tokens . append ( token ) <EOL> namespace = tokenutil . GetStringAfterToken ( token ) <EOL> if namespace in self . _required_namespaces : <EOL> self . _duplicate_require_tokens . append ( token ) <EOL> else : <EOL> self . _required_namespaces . append ( namespace ) <EOL> if self . _HasSuppression ( state_tracker , '<STR_LIT>' ) : <EOL> self . _suppressed_requires . append ( namespace ) <EOL> self . _AddUsedNamespace ( state_tracker , namespace , token ) <EOL> elif token . string == '<STR_LIT>' : <EOL> self . _provide_tokens . append ( token ) <EOL> namespace = tokenutil . GetStringAfterToken ( token ) <EOL> if namespace in self . _provided_namespaces : <EOL> self . _duplicate_provide_tokens . append ( token ) <EOL> else : <EOL> self . _provided_namespaces . append ( namespace ) <EOL> if self . _HasSuppression ( state_tracker , '<STR_LIT>' ) : <EOL> self . _AddCreatedNamespace ( state_tracker , namespace , token . line_number ) <EOL> elif token . string == '<STR_LIT>' : <EOL> self . _scopified_file = True <EOL> elif token . string == '<STR_LIT>' : <EOL> for t in tokenutil . GetAllTokensInSameLine ( token ) : <EOL> if t . type == TokenType . STRING_TEXT : <EOL> message = t . string <EOL> if re . match ( r'<STR_LIT>' , message ) : <EOL> base_namespace = message . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> if base_namespace in self . _closurized_namespaces : <EOL> self . _AddCreatedNamespace ( state_tracker , message , <EOL> token . line_number ) <EOL> break <EOL> else : <EOL> jsdoc = state_tracker . GetDocComment ( ) <EOL> if token . metadata and token . metadata . aliased_symbol : <EOL> whole_identifier_string = token . metadata . aliased_symbol <EOL> elif ( token . string == '<STR_LIT>' and <EOL> not self . _HasSuppression ( state_tracker , '<STR_LIT>' ) ) : <EOL> namespace = tokenutil . GetStringAfterToken ( token ) <EOL> namespace = UsedNamespace ( namespace , namespace , token , <EOL> alias_definition = False ) <EOL> self . _used_namespaces . append ( namespace ) <EOL> if jsdoc and jsdoc . HasFlag ( '<STR_LIT>' ) : <EOL> self . _AddCreatedNamespace ( state_tracker , whole_identifier_string , <EOL> token . line_number , <EOL> namespace = self . GetClosurizedNamespace ( <EOL> whole_identifier_string ) ) <EOL> else : <EOL> is_alias_definition = ( token . metadata and <EOL> token . metadata . is_alias_definition ) <EOL> self . _AddUsedNamespace ( state_tracker , whole_identifier_string , <EOL> token , is_alias_definition ) <EOL> elif token . type == TokenType . SIMPLE_LVALUE : <EOL> identifier = token . values [ '<STR_LIT>' ] <EOL> start_token = tokenutil . GetIdentifierStart ( token ) <EOL> if start_token and start_token != token : <EOL> identifier = tokenutil . GetIdentifierForToken ( start_token ) <EOL> else : <EOL> start_token = token <EOL> if ( start_token and <EOL> start_token . metadata and <EOL> start_token . metadata . aliased_symbol and <EOL> not start_token . metadata . is_alias_definition ) : <EOL> identifier = start_token . metadata . aliased_symbol <EOL> if identifier : <EOL> namespace = self . GetClosurizedNamespace ( identifier ) <EOL> if state_tracker . InFunction ( ) : <EOL> self . _AddUsedNamespace ( state_tracker , identifier , token ) <EOL> elif namespace and namespace != '<STR_LIT>' : <EOL> self . _AddCreatedNamespace ( state_tracker , identifier , <EOL> token . line_number , namespace = namespace ) <EOL> elif token . type == TokenType . DOC_FLAG : <EOL> flag = token . attached_object <EOL> flag_type = flag . flag_type <EOL> if flag and flag . HasType ( ) and flag . jstype : <EOL> is_interface = state_tracker . GetDocComment ( ) . HasFlag ( '<STR_LIT>' ) <EOL> if flag_type == '<STR_LIT>' or ( flag_type == '<STR_LIT>' <EOL> and is_interface ) : <EOL> identifier = flag . jstype . alias or flag . jstype . identifier <EOL> self . _AddUsedNamespace ( state_tracker , identifier , token ) <EOL> def _AddCreatedNamespace ( self , state_tracker , identifier , line_number , <EOL> namespace = None ) : <EOL> """<STR_LIT>""" <EOL> if not namespace : <EOL> namespace = identifier <EOL> if self . _HasSuppression ( state_tracker , '<STR_LIT>' ) : <EOL> return <EOL> self . _created_namespaces . append ( [ namespace , identifier , line_number ] ) <EOL> def _AddUsedNamespace ( self , state_tracker , identifier , token , <EOL> is_alias_definition = False ) : <EOL> """<STR_LIT>""" <EOL> if self . _HasSuppression ( state_tracker , '<STR_LIT>' ) : <EOL> return <EOL> identifier = self . _GetUsedIdentifier ( identifier ) <EOL> namespace = self . GetClosurizedNamespace ( identifier ) <EOL> if namespace and not state_tracker . IsVariableInScope ( namespace ) : <EOL> namespace = UsedNamespace ( namespace , identifier , token , <EOL> is_alias_definition ) <EOL> self . _used_namespaces . append ( namespace ) <EOL> def _HasSuppression ( self , state_tracker , suppression ) : <EOL> jsdoc = state_tracker . GetDocComment ( ) <EOL> return jsdoc and suppression in jsdoc . suppressions <EOL> def _GetUsedIdentifier ( self , identifier ) : <EOL> """<STR_LIT>""" <EOL> for suffix in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if identifier . endswith ( suffix ) : <EOL> return identifier [ : - len ( suffix ) ] <EOL> return identifier <EOL> def GetClosurizedNamespace ( self , identifier ) : <EOL> """<STR_LIT>""" <EOL> if identifier . startswith ( '<STR_LIT>' ) : <EOL> return None <EOL> parts = identifier . split ( '<STR_LIT:.>' ) <EOL> for namespace in self . _closurized_namespaces : <EOL> if not identifier . startswith ( namespace + '<STR_LIT:.>' ) : <EOL> continue <EOL> namespace = [ ] <EOL> for part in parts : <EOL> if part == '<STR_LIT>' or part . isupper ( ) : <EOL> return '<STR_LIT:.>' . join ( namespace ) <EOL> namespace . append ( part ) <EOL> if part [ <NUM_LIT:0> ] . isupper ( ) : <EOL> return '<STR_LIT:.>' . join ( namespace ) <EOL> if parts [ - <NUM_LIT:1> ] in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> parts . pop ( ) <EOL> parts . pop ( ) <EOL> if parts and parts [ - <NUM_LIT:1> ] . endswith ( '<STR_LIT:_>' ) : <EOL> parts . pop ( ) <EOL> return '<STR_LIT:.>' . join ( parts ) <EOL> return None </s>
