Skip to content
This repository has been archived by the owner on Jan 16, 2022. It is now read-only.

Commit

Permalink
#136: Fix retriving type of local symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarypiatek committed Sep 2, 2020
1 parent 621928d commit d83b65d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ private static IMappingSourceFinder GetMappingSourceFindersForQueryExpression(Se
}).Select(s =>
{
var type = semanticModel.GetTypeForSymbol(s);
if (ObjectHelper.IsSimpleType(type))

if (type == null || ObjectHelper.IsSimpleType(type))
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,20 @@ public static IReadOnlyList<ISymbol> GetLocalSymbols(this SemanticModel semantic
var symbolsToSelect = allowedSymbols ?? LocalSymbolKinds;
return semanticModel.LookupSymbols(nodeFromScope.GetLocation().SourceSpan.Start).Where(x => symbolsToSelect.Contains(x.Kind)).ToList();
}



public static ITypeSymbol GetTypeForSymbol(this SemanticModel semanticModel, ISymbol symbol)
{
if (symbol is IRangeVariableSymbol rangeVariableSymbol)
{
return GetRangeVariableType(semanticModel, rangeVariableSymbol);
}

var syntaxType = GetSyntaxType(symbol);
if (syntaxType == null)
return symbol switch
{
return null;
}

return semanticModel.GetTypeInfo(syntaxType).Type;
ILocalSymbol localSymbol => localSymbol.Type,
IFieldSymbol fieldSymbol => fieldSymbol.Type,
IPropertySymbol propertySymbol1 => propertySymbol1.Type,
IParameterSymbol parameterSymbol => parameterSymbol.Type,
IRangeVariableSymbol rangeVariableSymbol => GetRangeVariableType(semanticModel, rangeVariableSymbol),
_ => null
};
}


private static ITypeSymbol GetRangeVariableType(SemanticModel semanticModel, IRangeVariableSymbol symbol)
{
ITypeSymbol type = null;
Expand Down Expand Up @@ -86,25 +81,6 @@ private static QueryBodySyntax GetQueryBody(SyntaxToken token) =>
_ => null
};



private static SyntaxNode GetSyntaxType(ISymbol candidate)
{
var candidateSyntax = candidate.DeclaringSyntaxReferences.Select(x => x.GetSyntax()).FirstOrDefault();
if (candidateSyntax is VariableDeclaratorSyntax variableDeclarator)
{
var variableDeclaration = variableDeclarator.FindContainer<VariableDeclarationSyntax>();
return variableDeclaration.Type;
}
if (candidateSyntax is ParameterSyntax parameter)
{
return parameter.Type;
}


return null;
}

public static bool MatchType(this SemanticModel semanticModel, ISymbol source, ITypeSymbol targetType)
{
var sourceSymbolType = semanticModel.GetTypeForSymbol(source);
Expand Down

0 comments on commit d83b65d

Please sign in to comment.