Last active
October 29, 2016 23:39
-
-
Save controlflow/dc8d774b8bd92b62979201b65651aa28 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Pure, ContractAnnotation("null => null")] | |
public static ICSharpTreeNode GetCodeBody([CanBeNull] this ICSharpDeclaration declaration) | |
{ | |
if (declaration == null) return null; | |
var functionDeclaration = declaration as ICSharpFunctionDeclaration; | |
if (functionDeclaration != null && functionDeclaration.Body != null) | |
{ | |
return functionDeclaration.Body; | |
} | |
var localFunctionDeclaration = declaration as ILocalFunctionDeclaration; | |
if (localFunctionDeclaration != null) | |
{ | |
var body = localFunctionDeclaration.Body; | |
if (body != null) return body; | |
} | |
var expressionBodyOwner = declaration as IExpressionBodyOwnerDeclaration; | |
if (expressionBodyOwner != null) | |
{ | |
var clause = expressionBodyOwner.ArrowExpression; | |
if (clause != null) return clause.Expression; | |
} | |
var lambdaExpression = declaration as ILambdaExpression; | |
if (lambdaExpression != null) | |
{ | |
return lambdaExpression.BodyExpression | |
?? lambdaExpression.BodyBlock as ICSharpTreeNode; | |
} | |
var anonymousMethodExpression = declaration as IAnonymousMethodExpression; | |
if (anonymousMethodExpression != null) | |
{ | |
return anonymousMethodExpression.Body; | |
} | |
return null; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Pure, ContractAnnotation("null => null")] | |
public static ICSharpTreeNode GetCodeBody([CanBeNull] this ICSharpDeclaration declaration) | |
{ | |
switch (declaration) | |
{ | |
case ICSharpFunctionDeclaration functionDeclaration when functionDeclaration.Body != null: | |
return functionDeclaration.Body; | |
case ILocalFunctionDeclaration localFunctionDeclaration when localFunctionDeclaration.Body != null: | |
return localFunctionDeclaration.Body; | |
case IExpressionBodyOwnerDeclaration expressionBodyOwner when expressionBodyOwner.ArrowExpression != null: | |
return localFunctionDeclaration.ArrowExpression.Expression; | |
case ILambdaExpression lambdaExpression: | |
return lambdaExpression.BodyExpression ?? lambdaExpression.BodyBlock as ICSharpTreeNode; | |
case IAnonymousMethodExpression anonymousMethodExpression: | |
return anonymousMethodExpression.Body; | |
default: | |
return null; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Pure, ContractAnnotation("null => null")] | |
public static ICSharpTreeNode GetCodeBody([CanBeNull] this ICSharpDeclaration declaration) | |
{ | |
switch (declaration) | |
{ | |
case ICSharpFunctionDeclaration { Body is var! bodyBlock }: | |
return bodyBlock; | |
case ILocalFunctionDeclaration { Body is var! bodyBlock }: | |
return bodyBlock; | |
case IExpressionBodyOwnerDeclaration { ArrowExpression is { Expression is var bodyExpression } }: | |
return bodyExpression; | |
case ILambdaExpression lambdaExpression: | |
return lambdaExpression.BodyExpression ?? lambdaExpression.BodyBlock as ICSharpTreeNode; | |
case IAnonymousMethodExpression anonymousMethodExpression: | |
return anonymousMethodExpression.Body; | |
default: | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment