static TemplateAliases createTemplateAliases ( SoyFileNode fileNode ) { Map < String , String > aliasMap = new HashMap <> ( ) ; Set < String > localTemplates = new HashSet <> ( ) ; int counter = 0 ; ImmutableList . Builder < TemplateNode > templates = ImmutableList . builder ( ) ; templates . addAll ( SoyTreeUtils . getAllNodesOfType ( fileNode , TemplateBasicNode . class ) ) . addAll ( SoyTreeUtils . getAllNodesOfType ( fileNode , TemplateElementNode . class ) ) ; // Go through templates first and just alias them to their local name. for ( TemplateNode templateNode : templates . build ( ) ) { String partialName = templateNode . getPartialTemplateName ( ) ; String fullyQualifiedName = templateNode . getTemplateName ( ) ; localTemplates . add ( fullyQualifiedName ) ; Preconditions . checkState ( partialName != null , "Aliasing not supported for V1 templates" ) ; // Need to start the alias with something that cannot be a part of a reserved // JavaScript identifier like 'function' or 'catch'. String alias = "$" + partialName . substring ( 1 ) ; aliasMap . put ( fullyQualifiedName , alias ) ; } // Go through all call sites looking for foreign template calls and create an alias for them. for ( CallBasicNode callBasicNode : SoyTreeUtils . getAllNodesOfType ( fileNode , CallBasicNode . class ) ) { String fullyQualifiedName = callBasicNode . getCalleeName ( ) ; // We could have either encountered the foreign fully qualified name before or it could belong // to a local template. if ( localTemplates . contains ( fullyQualifiedName ) || aliasMap . containsKey ( fullyQualifiedName ) ) { continue ; } String alias = TEMPLATE_ALIAS_PREFIX + ( ++ counter ) ; aliasMap . put ( fullyQualifiedName , alias ) ; } return new Aliases ( aliasMap ) ; }
Creates a mapping for assigning and looking up the variable alias for templates both within a file and referenced from external files . For local files the alias is just the local template s name . For external templates the alias is an identifier that is unique for that template .