Skip to content

Commit

Permalink
Don't build artificial Location, return FileLinePositionSpan directly…
Browse files Browse the repository at this point in the history
… instead
  • Loading branch information
antonioaversa committed Aug 30, 2023
1 parent 1752d59 commit 3e38d81
Showing 1 changed file with 15 additions and 16 deletions.
Expand Up @@ -24,14 +24,11 @@

namespace SonarAnalyzer.Rules
{
internal static class LocationExtensions
// FIXME: this is temporary - remove it
public static class LocationExtensions
{
public static bool TryEnsureMappedLocation(this Location inputLocation, out Location mappedLocation)
{
// FIXME: implement this method
mappedLocation = inputLocation;
return true;
}
public static FileLinePositionSpan GetMappedLineSpanIfAvailable(this Location location) =>
!GeneratedCodeRecognizer.IsRazorGeneratedFile(location.SourceTree) ? location.GetLineSpan() : location.GetMappedLineSpan();
}

public abstract class TokenTypeAnalyzerBase<TSyntaxKind> : UtilityAnalyzerBase<TSyntaxKind, TokenTypeInfo>
Expand Down Expand Up @@ -144,13 +141,15 @@ token switch
_ => null,
};

protected TokenInfo TokenInfo(SyntaxToken token, TokenType tokenType) =>
tokenType == TokenType.UnknownTokentype
|| (string.IsNullOrWhiteSpace(token.Text) && tokenType != TokenType.StringLiteral)
|| !token.GetLocation().TryEnsureMappedLocation(out var mappedLocation)
|| !(string.IsNullOrWhiteSpace(filePath) || string.Equals(mappedLocation.GetLineSpan().Path, filePath, StringComparison.OrdinalIgnoreCase))
? null
: new() { TokenType = tokenType, TextRange = GetTextRange(token.GetLocation().GetLineSpan()) };
protected TokenInfo TokenInfo(SyntaxToken token, TokenType tokenType)
{
var span = token.GetLocation().GetMappedLineSpanIfAvailable();
return tokenType == TokenType.UnknownTokentype
|| (string.IsNullOrWhiteSpace(token.Text) && tokenType != TokenType.StringLiteral)
|| !(string.IsNullOrWhiteSpace(filePath) || string.Equals(span.Path, filePath, StringComparison.OrdinalIgnoreCase))
? null
: new() { TokenType = tokenType, TextRange = GetTextRange(span) };
}

protected virtual TokenInfo ClassifyIdentifier(SyntaxToken token)
{
Expand Down Expand Up @@ -194,8 +193,8 @@ protected TriviaClassifierBase(string filePath)
}

public TokenInfo ClassifyTrivia(SyntaxTrivia trivia) =>
trivia.GetLocation().TryEnsureMappedLocation(out var mappedLocation)
&& (string.IsNullOrWhiteSpace(filePath) || string.Equals(filePath, mappedLocation.GetLineSpan().Path, StringComparison.OrdinalIgnoreCase))
string.IsNullOrWhiteSpace(filePath)
|| string.Equals(filePath, trivia.GetLocation().GetMappedLineSpanIfAvailable().Path, StringComparison.OrdinalIgnoreCase)
? trivia switch
{
_ when IsRegularComment(trivia) => TokenInfo(trivia.SyntaxTree, TokenType.Comment, trivia.Span),
Expand Down

0 comments on commit 3e38d81

Please sign in to comment.