-
Notifications
You must be signed in to change notification settings - Fork 228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
.razor: implement the token type metrics #7924
Conversation
7d1038e
to
828dd74
Compare
@costin-zaharia-sonarsource The PR is still in draft and shouldn't be merged yet. |
analyzers/src/SonarAnalyzer.Common/Rules/Utilities/TokenTypeAnalyzerBase.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.Common/Rules/Utilities/UtilityAnalyzerBase.cs
Outdated
Show resolved
Hide resolved
3e38d81
to
cbbb4dd
Compare
@costin-zaharia-sonarsource : Back to you for a second round of review. |
@cristian-ambrosini-sonarsource As discussed offline, adding you for a first review of the second round of review. @costin-zaharia-sonarsource will then take over when I am off. |
Kudos, SonarCloud Quality Gate passed! |
e7695b5
to
25a123f
Compare
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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't return null if the filePath
is null or whitespace?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made that choice for two reasons:
- convenience: ClassifierTestHarness.AssertTokenTypes instantiates a
TokenClassifier
instance and aTriviaClassifier
instance without having at hand the file path, which is implicitly created by ParseTokens - safety: while it seems indeed reasonable, to always return
null
in absence offilePath
, I didn't know whether aSyntaxTree
may have, for any reason, no file path (as it was somehow fully compiled in-memory), and I didn't want to alter the behavior theTokenTypeAnalyzer
had before these changes
If we can/want to assume that the TokenTypeAnalyzer
should always require filePath
, we should do some refactoring to the ParseTokens
, to basically return snippet0.cs
, which would then be passed to the classifier.
// Handle preprocessor directives here | ||
_ => null, | ||
}; | ||
string.IsNullOrWhiteSpace(filePath) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also here, I think we should ignore tokens if they don't have a file path. Do you have maybe an example of when this happens?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Answered here
It looks good to me so far. As you mentioned, the test cases need to be finished, and we need to make some decisions about what we do with locations since they are wrongly mapped by Roslyn. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job! I left only one super nitpick comment
analyzers/tests/SonarAnalyzer.UnitTest/Rules/Utilities/TokenTypeAnalyzerTest.cs
Outdated
Show resolved
Hide resolved
@costin-zaharia-sonarsource @cristian-ambrosini-sonarsource I have addressed/replied to the comments made in the second round of review. I hand the PR over to @costin-zaharia-sonarsource for the next steps, which are described in the description of the PR. |
57be89d
to
6ab3261
Compare
analyzers/src/SonarAnalyzer.Common/Extensions/LocationExtensions.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.Common/Extensions/LocationExtensions.cs
Outdated
Show resolved
Hide resolved
4c4c1ba
to
b272354
Compare
Kudos, SonarCloud Quality Gate passed! |
Update: moved back to be a draft, since location mapping for nested Razor expressions should be fixed first, for the reasons expressed here. |
I am closing the PR, given that
I am adding a reference to the text in the description in the issue, so that a new implementation can benefit of the analysis done while implementing this PR. |
Fixes #7895
Draft: don't merge, this PR is still based on #7916 by @costin-zaharia-sonarsource, due to dependencies on lines and location span mapping.DONEOnce #7916 is merged to master, this PR should be rebased.DONEAlso, IT should be implemented.
Issue with numeric tokens
The current implementation for the
TokenTypeAnalyzer
in this branch has an issue: it reports numeric tokens that don't exist in the original razor file. The issue comes from a bug in location mapping.Take for example line 84 from the
RazorTokens.razor
file (defined here).The generated code for that line is the following:
Because
#line (84,5)-(84,17) 24
is not handled correctly when deriving the original location in the razor file from the location in the generated file, the numeric token9
(right beforeDateTime.Now
) is considered as coming from the original razor file, and not as a token in the generated code only.Therefore, the following tokens are reported for line 84:
instead of
The issue basically affects all lines that generate enhanced line directives, and doesn't affect lines that:
@code { ... }
or@{ ... }
etc.)@page "/razor"
,@namespace MyRazorNamespace
, etc.)@@page "/razor"
or<p>var x = 42;</p>
).Issue with tokens position asserting
Because:
we can't effectively use
[x:...]
syntax, introduced by @martin-strecker-sonarsource to assert tokens existence and positions.The square brackets for all tokens whose location is mapped with enhanced line directives would need to be placed some columns off, to be reported correctly. Moreover, non-existing, numeric, overlapping tokens would break assertion.
So, for the time being, and while positions are fixed, this PR is only asserting how many tokens are defined per line, and of what type (here).
Performance impact of this change
Tested on local SonarQube Development 10.2.0.76792 on Dell laptop, on power network.
Project tested: https://github.com/MudBlazor/MudBlazor/tree/1e7677c70fefa1ac440e9fadbdf1aed64a336653
sonar-dotnet commit: 392d38b (feature)
Results (total build time):
00:01:28.33
00:01:58.35
00:02:02.40
00:01:50.90
00:01:59.53
sonar-dotnet commit: TBD (merge-base)
TBD