Commit 1df576f
Fix: .UseMauiCommunityToolkit() Analyzer when wrapped in preprocessor directives (CommunityToolkit#2769)
* Improve tests and enhance analyzer detection logic
Refactor `UseCommunityToolkitInitializationAnalyzerTests.cs` for better formatting and readability. Added a new test method to check for `UseMauiCommunityToolkit` usage within preprocessor directives. Updated the `VerifyMauiToolkitAnalyzer` method to include a new expected type.
In `UseCommunityToolkitInitializationAnalyzer.cs`, modified the `AnalyzeNode` method to directly check the method's text for `UseMauiCommunityToolkit`, improving detection reliability even within preprocessor directives.
* Update dotnet-build.yml
* Update dotnet-build.yml
* Update UseCommunityToolkitInitializationAnalyzerTests.cs
* Update src/CommunityToolkit.Maui.Analyzers/UseCommunityToolkitInitializationAnalyzer.cs
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Refactor UseCommunityToolkitInitializationAnalyzer
- Removed the `category` constant from the analyzer class.
- Updated `AnalyzeNode` to retrieve `methodDeclaration` using `invocationExpression.Ancestors()`.
- Added a check to ensure `methodDeclaration` is not null and verifies the absence of `UseMauiCommunityToolkit` calls.
- Introduced `HasUseMauiCommunityToolkitCall` method to traverse the syntax tree for `UseMauiCommunityToolkit` calls.
- Added `UseMauiCommunityToolkitVisitor` class to efficiently check for invocation expressions.
- Improved diagnostic reporting logic to only report when `UseMauiCommunityToolkit` is not found.
* Update analyzer tests and simplify method logic
- Added necessary `using` directives for testing in
`UseCommunityToolkitInitializationAnalyzerTests.cs`.
- Refactored `HasUseMauiCommunityToolkitCall` in
`UseCommunityToolkitInitializationAnalyzer.cs` to remove
the visitor pattern, improving memory efficiency by
directly checking method text for the specified method name.
* Improve memory efficiency in HasUseMauiCommunityToolkitCall
Refactor the method to check for the presence of the method name by iterating through each line of the method's text instead of retrieving the entire text span. This change reduces memory usage and enhances performance by only examining relevant lines that overlap with the method's span.
* Optimize HasUseMauiCommunityToolkitCall method
Refactor the `HasUseMauiCommunityToolkitCall` method to enhance efficiency by searching for the method name character by character within the method's span. This change eliminates unnecessary string allocations and improves performance. The new implementation uses a boolean flag to quickly determine if a match is found, returning `true` immediately upon a match and `false` if no match is found after checking all characters.
* Add check for UseMauiCommunityToolkit calls in analyzer
Updated `AnalyzeNode` to report diagnostics if a method
contains a call to `UseMauiCommunityToolkit`. Introduced
`HasUseMauiCommunityToolkitCall` to efficiently search
for the method call in the source text without string
allocations.
* Refactor tests and improve analyzer efficiency
Reformatted `UseCommunityToolkitInitializationAnalyzerTests.cs` for consistency and readability, adding a new test method `VerifyErrorsWhenUseMauiCommunityToolkitIsCommentedOut`. Updated the `VerifyMauiToolkitAnalyzer` method to include this new test case. Enhanced the logic in `UseCommunityToolkitInitializationAnalyzer.cs` to check the syntax tree before performing character-by-character searches, improving analysis efficiency.
* Update Camera + MediaElement Analyzers
* `dotnet format`
* Remove empty line
* Improve Performance
* Use Static Anonymous Functions
---------
Co-authored-by: Brandon Minnick <13558917+TheCodeTraveler@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Brandon Minnick <13558917+brminnick@users.noreply.github.com>1 parent 28e030c commit 1df576f
File tree
6 files changed
+223
-22
lines changed- src
- CommunityToolkit.Maui.Analyzers.UnitTests
- CommunityToolkit.Maui.Analyzers
- CommunityToolkit.Maui.Camera.Analyzers
- CommunityToolkit.Maui.MediaElement.Analyzers
6 files changed
+223
-22
lines changedLines changed: 37 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
121 | 158 | | |
122 | 159 | | |
123 | 160 | | |
| |||
Lines changed: 73 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
121 | 194 | | |
122 | 195 | | |
123 | 196 | | |
| |||
Lines changed: 40 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
118 | 119 | | |
119 | 120 | | |
120 | 121 | | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
121 | 159 | | |
122 | 160 | | |
123 | 161 | | |
124 | 162 | | |
125 | 163 | | |
126 | | - | |
| 164 | + | |
127 | 165 | | |
128 | 166 | | |
129 | 167 | | |
| |||
Lines changed: 27 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
14 | 13 | | |
15 | 14 | | |
16 | 15 | | |
17 | 16 | | |
18 | 17 | | |
19 | 18 | | |
20 | 19 | | |
21 | | - | |
22 | 20 | | |
23 | 21 | | |
24 | 22 | | |
| |||
32 | 30 | | |
33 | 31 | | |
34 | 32 | | |
35 | | - | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
36 | 36 | | |
37 | | - | |
38 | | - | |
| 37 | + | |
39 | 38 | | |
40 | 39 | | |
41 | 40 | | |
42 | 41 | | |
43 | | - | |
44 | | - | |
45 | | - | |
| 42 | + | |
46 | 43 | | |
47 | 44 | | |
48 | 45 | | |
49 | 46 | | |
50 | 47 | | |
51 | 48 | | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
52 | 71 | | |
Lines changed: 23 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
40 | | - | |
| 39 | + | |
41 | 40 | | |
42 | 41 | | |
43 | 42 | | |
44 | 43 | | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
| 44 | + | |
49 | 45 | | |
50 | 46 | | |
51 | 47 | | |
52 | 48 | | |
53 | 49 | | |
54 | 50 | | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
55 | 72 | | |
Lines changed: 23 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
40 | | - | |
| 39 | + | |
41 | 40 | | |
42 | 41 | | |
43 | 42 | | |
44 | 43 | | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
| 44 | + | |
49 | 45 | | |
50 | 46 | | |
51 | 47 | | |
52 | 48 | | |
53 | 49 | | |
54 | 50 | | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
55 | 72 | | |
0 commit comments