11// Copyright (c) Microsoft Corporation.
22// Licensed under the MIT License.
33using System ;
4+ using System . Collections ;
5+ using System . Collections . Generic ;
6+ using System . Diagnostics . CodeAnalysis ;
47using System . Linq ;
58using Arm . Expression . Expressions ;
69using Bicep . Core . Resources ;
@@ -71,29 +74,14 @@ public LanguageExpression ConvertExpression(SyntaxBase expression)
7174 Array . Empty < LanguageExpression > ( ) ) ;
7275
7376 case FunctionCallSyntax function :
74- var returnValueType = context . SemanticModel . GetTypeInfo ( function ) ;
75- if ( returnValueType is IResourceScopeType resourceScopeType && ! ScopeHelper . CanConvertToArmJson ( resourceScopeType ) )
76- {
77- // return an empty object - there is no ARM equivalent to return
78- return new FunctionExpression ( "json" , new LanguageExpression [ ] { new JTokenExpression ( "{}" ) } , new LanguageExpression [ 0 ] ) ;
79- }
80-
8177 return ConvertFunction (
8278 function . Name . IdentifierName ,
8379 function . Arguments . Select ( a => ConvertExpression ( a . Expression ) ) . ToArray ( ) ) ;
8480
8581 case InstanceFunctionCallSyntax instanceFunctionCall :
8682 var namespaceSymbol = context . SemanticModel . GetSymbolInfo ( instanceFunctionCall . BaseExpression ) ;
87-
8883 Assert ( namespaceSymbol is NamespaceSymbol , $ "BaseExpression must be a NamespaceSymbol, instead got: '{ namespaceSymbol ? . Kind } '") ;
8984
90- var returnValueType2 = context . SemanticModel . GetTypeInfo ( instanceFunctionCall ) ;
91- if ( returnValueType2 is IResourceScopeType resourceScopeType2 && ! ScopeHelper . CanConvertToArmJson ( resourceScopeType2 ) )
92- {
93- // return an empty object - there is no ARM equivalent to return
94- return new FunctionExpression ( "json" , new LanguageExpression [ ] { new JTokenExpression ( "{}" ) } , new LanguageExpression [ 0 ] ) ;
95- }
96-
9785 return ConvertFunction (
9886 instanceFunctionCall . Name . IdentifierName ,
9987 instanceFunctionCall . Arguments . Select ( a => ConvertExpression ( a . Expression ) ) . ToArray ( ) ) ;
@@ -374,9 +362,31 @@ private static LanguageExpression ConvertFunction(string functionName, LanguageE
374362 return arguments . Single ( ) ;
375363 }
376364
365+ if ( ShouldReplaceUnsupportedFunction ( functionName , arguments , out var replacementExpression ) )
366+ {
367+ return replacementExpression ;
368+ }
369+
377370 return new FunctionExpression ( functionName , arguments , Array . Empty < LanguageExpression > ( ) ) ;
378371 }
379372
373+ private static bool ShouldReplaceUnsupportedFunction ( string functionName , LanguageExpression [ ] arguments , [ NotNullWhen ( true ) ] out LanguageExpression ? replacementExpression )
374+ {
375+ switch ( functionName )
376+ {
377+ // These functions have not yet been implemented in ARM. For now, we will just return an empty object if they are accessed directly.
378+ case "tenant" :
379+ case "managementGroup" :
380+ case "subscription" when arguments . Length > 0 :
381+ case "resourceGroup" when arguments . Length > 0 :
382+ replacementExpression = GetCreateObjectExpression ( ) ;
383+ return true ;
384+ }
385+
386+ replacementExpression = null ;
387+ return false ;
388+ }
389+
380390 private FunctionExpression ConvertArray ( ArraySyntax syntax )
381391 {
382392 // we are using the createArray() function as a proxy for an array literal
@@ -402,12 +412,12 @@ private FunctionExpression ConvertObject(ObjectSyntax syntax)
402412 }
403413
404414 // we are using the createObject() funciton as a proy for an object literal
405- return new FunctionExpression (
406- "createObject" ,
407- parameters ,
408- Array . Empty < LanguageExpression > ( ) ) ;
415+ return GetCreateObjectExpression ( parameters ) ;
409416 }
410417
418+ private static FunctionExpression GetCreateObjectExpression ( params LanguageExpression [ ] parameters )
419+ => new FunctionExpression ( "createObject" , parameters , Array . Empty < LanguageExpression > ( ) ) ;
420+
411421 private LanguageExpression ConvertBinary ( BinaryOperationSyntax syntax )
412422 {
413423 LanguageExpression operand1 = ConvertExpression ( syntax . LeftExpression ) ;
0 commit comments