Skip to content
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

Added fix to support spaces between the method name and its () to prevent a false positive MCTME001 error #1477

Merged
merged 42 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
ad9555e
Added fix to support spaces between the method name and its `()` to p…
SotoiGhost Oct 26, 2023
27113df
Merge branch 'main' into fix-1476
pictos Nov 6, 2023
0a5ed9e
Merge branch 'main' into fix-1476
brminnick Feb 26, 2024
504b72a
Merge branch 'main' into fix-1476
brminnick Mar 26, 2024
6498af2
Merge branch 'main' into fix-1476
brminnick Jul 24, 2024
147c9c3
Merge branch 'main' into fix-1476
pictos Jul 24, 2024
6142f3a
Update UseCommunityToolkitInitializationAnalyzer.cs
brminnick Sep 10, 2024
5a2df52
Update Analyzers
brminnick Sep 10, 2024
a88a055
Use `string.Concat`
brminnick Sep 10, 2024
c04bc90
Update MauiProgram.cs
brminnick Sep 10, 2024
dfbf076
Update CommunityToolkit.Maui.csproj
brminnick Sep 11, 2024
12696fe
Fix Naming
brminnick Sep 12, 2024
5ec6545
Add `FileContainsCommunityToolkitMauiNamespace`
brminnick Sep 12, 2024
53f1966
Remove whitespace when verifying method syntax
brminnick Sep 12, 2024
e50564d
Update CommunityToolkit.Maui.csproj
brminnick Sep 12, 2024
a770c8d
Refactor Analyzers
brminnick Sep 12, 2024
a2e4782
Add `GetAllMethodDelcarationsWithoutWhiteSpace`
brminnick Sep 12, 2024
1655b20
Update CommunityToolkit.Maui.csproj
brminnick Sep 12, 2024
56cd583
Merge branch 'main' into fix-1476
brminnick Sep 12, 2024
9ea2053
Add Verifiers
brminnick Sep 16, 2024
3c6d04b
Merge branch 'main' into fix-1476
brminnick Sep 17, 2024
dc8d548
Add UseCommunityToolkitCameraInitializationAnalyzerTests
brminnick Sep 17, 2024
a29a1d4
Merge branch 'fix-1476' of https://github.com/SotoiGhost/MauiCommunit…
brminnick Sep 17, 2024
ca1eb49
Remove Unnecessary Usings
brminnick Sep 17, 2024
2b1830f
Add `VerifyNoErrorsWhenUseMauiCommunityToolkit`
brminnick Sep 17, 2024
eef3483
Add `VerifyNoErrorsWhenUseMauiCommunityToolkitCamera` + `VerifyNoErro…
brminnick Sep 17, 2024
defffe0
Remove `BaseTest`
brminnick Sep 17, 2024
8d6bbe3
Refactor Analyzers
brminnick Sep 17, 2024
1e85821
Add Helper Methods
brminnick Sep 17, 2024
3162bc7
Add Tests With Additional Whitespace
brminnick Sep 17, 2024
cb1c497
`dotnet format`
brminnick Sep 17, 2024
0fa8e2b
Use `ReadOnlySpan<char>`
brminnick Sep 17, 2024
a5ca656
Merge`if` statements
brminnick Sep 17, 2024
90b8731
Refactor `GetInvocationExpressionSyntax(SyntaxNode)`
brminnick Sep 17, 2024
fe76df1
Enable analyzer release tracking
brminnick Sep 17, 2024
a9ab5c3
Merge branch 'main' into fix-1476
brminnick Sep 18, 2024
568549c
Optimize Analyzer
brminnick Sep 18, 2024
3b5c5af
Implement Collection Expressions
brminnick Sep 18, 2024
998cd4d
Optimize `UseCommunityToolkitInitializationAnalyzer`
brminnick Sep 18, 2024
451fa12
Allow `params Type[] assembliesUnderTest` for Tests
brminnick Sep 18, 2024
ef34650
Update Camera + MediaElement Analyzers and Tests
brminnick Sep 18, 2024
7ee1f0b
Fix SyntaxKind
brminnick Sep 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ csharp_style_conditional_delegate_call = true:suggestion

# Collection Expressions
dotnet_style_prefer_collection_expression = when_types_loosely_match:suggestion
dotnet_diagnostic.IDE0303.severity = suggestion

# Naming rules

Expand Down
17 changes: 8 additions & 9 deletions samples/CommunityToolkit.Maui.Sample/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@
using Microsoft.Extensions.Http.Resilience;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.LifecycleEvents;
using Microsoft.Maui.Platform;
using Polly;


#if WINDOWS10_0_17763_0_OR_GREATER
using Microsoft.UI;
using Microsoft.UI.Composition.SystemBackdrops;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml.Media;
using Microsoft.Maui.Platform;
#endif

[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
Expand All @@ -52,12 +51,12 @@ public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder()
#if DEBUG
.UseMauiCommunityToolkit(options =>
.UseMauiCommunityToolkit(static options =>
SotoiGhost marked this conversation as resolved.
Show resolved Hide resolved
{
options.SetShouldEnableSnackbarOnWindows(true);
})
#else
.UseMauiCommunityToolkit(options =>
.UseMauiCommunityToolkit(static options =>
{
options.SetShouldEnableSnackbarOnWindows(true);
options.SetShouldSuppressExceptionsInConverters(true);
Expand All @@ -70,7 +69,7 @@ public static MauiApp CreateMauiApp()
.UseMauiCommunityToolkitMediaElement()
.UseMauiCommunityToolkitMaps("KEY") // You should add your own key here from bingmapsportal.com
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
.ConfigureFonts(static fonts =>
{
fonts.AddFont("Font Awesome 6 Brands-Regular-400.otf", FontFamilies.FontAwesomeBrands);
});
Expand All @@ -79,13 +78,13 @@ public static MauiApp CreateMauiApp()
builder.ConfigureLifecycleEvents(events =>
{
#if WINDOWS10_0_17763_0_OR_GREATER
events.AddWindows(wndLifeCycleBuilder =>
events.AddWindows(static windowLifeCycleBuilder =>
{
wndLifeCycleBuilder.OnWindowCreated(window =>
windowLifeCycleBuilder.OnWindowCreated(window =>
{
window.SystemBackdrop = new MicaBackdrop { Kind = MicaKind.Base };

var titleBar = window.GetAppWindow()!.TitleBar;
var titleBar = window.GetAppWindow()?.TitleBar ?? throw new InvalidOperationException("App Window Cannot be Null");

titleBar.PreferredHeightOption = TitleBarHeightOption.Tall;

Expand All @@ -105,7 +104,7 @@ public static MauiApp CreateMauiApp()
});

builder.Services.AddHttpClient<ByteArrayToImageSourceConverterViewModel>()
.AddStandardResilienceHandler(options => options.Retry = new MobileHttpRetryStrategyOptions());
.AddStandardResilienceHandler(static options => options.Retry = new MobileHttpRetryStrategyOptions());

builder.Services.AddSingleton<PopupSizeConstants>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(NetVersion)</TargetFramework>
Expand All @@ -18,7 +18,7 @@
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" PrivateAssets="All" />
<PackageReference Include="coverlet.collector" Version="6.0.2" PrivateAssets="All" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.10.0" />
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiPackageVersion)"/>
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiPackageVersion)" />
</ItemGroup>

<ItemGroup>
Expand All @@ -30,7 +30,11 @@

<ItemGroup>
<ProjectReference Include="..\CommunityToolkit.Maui.Analyzers.CodeFixes\CommunityToolkit.Maui.Analyzers.CodeFixes.csproj" />
<ProjectReference Include="..\CommunityToolkit.Maui.Camera.Analyzers.CodeFixes\CommunityToolkit.Maui.Camera.Analyzers.CodeFixes.csproj" />
<ProjectReference Include="..\CommunityToolkit.Maui.Camera\CommunityToolkit.Maui.Camera.csproj" />
<ProjectReference Include="..\CommunityToolkit.Maui.MediaElement.Analyzers.CodeFixes\CommunityToolkit.Maui.MediaElement.Analyzers.CodeFixes.csproj" />
<ProjectReference Include="..\CommunityToolkit.Maui.MediaElement\CommunityToolkit.Maui.MediaElement.csproj" />
<ProjectReference Include="..\CommunityToolkit.Maui\CommunityToolkit.Maui.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using CommunityToolkit.Maui.Camera.Analyzers;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Testing;
using Xunit;
using static CommunityToolkit.Maui.Analyzers.UnitTests.CSharpCodeFixVerifier<CommunityToolkit.Maui.Camera.Analyzers.UseCommunityToolkitCameraInitializationAnalyzer, CommunityToolkit.Maui.Camera.Analyzers.UseCommunityToolkitCameraInitializationAnalyzerCodeFixProvider>;

namespace CommunityToolkit.Maui.Analyzers.UnitTests;
public class UseCommunityToolkitCameraInitializationAnalyzerTests
{
[Fact]
public void UseCommunityToolkitMediaElementInitializationAnalyzerId()
{
Assert.Equal("MCTC001", UseCommunityToolkitCameraInitializationAnalyzer.DiagnosticId);
}

[Fact]
public async Task VerifyNoErrorsWhenUseMauiCommunityToolkitCamera()
{
const string source = @"
namespace CommunityToolkit.Maui.Analyzers.UnitTests
{
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Hosting;
using CommunityToolkit.Maui;

public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder.UseMauiApp<Microsoft.Maui.Controls.Application>()
.UseMauiCommunityToolkitCamera()
.ConfigureFonts(fonts =>
{
fonts.AddFont(""OpenSans-Regular.ttf"", ""OpenSansRegular"");
fonts.AddFont(""OpenSans-Semibold.ttf"", ""OpenSansSemibold"");
});

return builder.Build();
}
}
}";

await VerifyCameraToolkitAnalyzer(source);
}

[Fact]
public async Task VerifyNoErrorsWhenUseMauiCommunityToolkitCameraHasAdditonalWhitespace()
{
const string source = @"
namespace CommunityToolkit.Maui.Analyzers.UnitTests
{
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Hosting;
using CommunityToolkit.Maui;

public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder ();
builder.UseMauiApp<Microsoft.Maui.Controls.Application> ()
.UseMauiCommunityToolkitCamera ()
.ConfigureFonts(fonts =>
{
fonts.AddFont(""OpenSans-Regular.ttf"", ""OpenSansRegular"");
fonts.AddFont(""OpenSans-Semibold.ttf"", ""OpenSansSemibold"");
});

return builder.Build ();
}
}
}";

await VerifyCameraToolkitAnalyzer(source);
}

[Fact]
public async Task VerifyErrorsWhenMissingUseMauiCommunityToolkitCamera()
{
const string source = @"
namespace CommunityToolkit.Maui.Analyzers.UnitTests
{
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Hosting;
using CommunityToolkit.Maui;

public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder.UseMauiApp<Microsoft.Maui.Controls.Application>()
.ConfigureFonts(fonts =>
{
fonts.AddFont(""OpenSans-Regular.ttf"", ""OpenSansRegular"");
fonts.AddFont(""OpenSans-Semibold.ttf"", ""OpenSansSemibold"");
});

return builder.Build();
}
}
}";

await VerifyCameraToolkitAnalyzer(source, Diagnostic().WithSpan(13, 4, 13, 61).WithSeverity(DiagnosticSeverity.Error));
}

static Task VerifyCameraToolkitAnalyzer(string source, params DiagnosticResult[] diagnosticResults)
{
return VerifyAnalyzerAsync(
source,
[
typeof(Views.CameraView) // CommunityToolkit.Maui.Camera
],
diagnosticResults);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Xunit;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Testing;
using Xunit;
using static CommunityToolkit.Maui.Analyzers.UnitTests.CSharpAnalyzerVerifier<CommunityToolkit.Maui.Analyzers.UseCommunityToolkitInitializationAnalyzer>;

namespace CommunityToolkit.Maui.Analyzers.UnitTests;

Expand All @@ -9,4 +12,107 @@ public void UseCommunityToolkitInitializationAnalyzerId()
{
Assert.Equal("MCT001", UseCommunityToolkitInitializationAnalyzer.DiagnosticId);
}

[Fact]
public async Task VerifyNoErrorsWhenUseMauiCommunityToolkit()
{
const string source = @"
namespace CommunityToolkit.Maui.Analyzers.UnitTests
{
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Hosting;
using CommunityToolkit.Maui;

public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder.UseMauiApp<Microsoft.Maui.Controls.Application>()
.UseMauiCommunityToolkit()
.ConfigureFonts(fonts =>
{
fonts.AddFont(""OpenSans-Regular.ttf"", ""OpenSansRegular"");
fonts.AddFont(""OpenSans-Semibold.ttf"", ""OpenSansSemibold"");
});

return builder.Build();
}
}
}";

await VerifyMauiToolkitAnalyzer(source);
}

[Fact]
public async Task VerifyNoErrorsWhenUseMauiCommunityToolkitHasAdditonalWhitespace()
{
const string source = @"
namespace CommunityToolkit.Maui.Analyzers.UnitTests
{
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Hosting;
using CommunityToolkit.Maui;

public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder ();
builder.UseMauiApp<Microsoft.Maui.Controls.Application> ()
.UseMauiCommunityToolkit ()
.ConfigureFonts (fonts =>
{
fonts.AddFont(""OpenSans-Regular.ttf"", ""OpenSansRegular"");
fonts.AddFont(""OpenSans-Semibold.ttf"", ""OpenSansSemibold"");
});

return builder.Build ();
}
}
}";

await VerifyMauiToolkitAnalyzer(source);
}

[Fact]
public async Task VerifyErrorsWhenMissingUseMauiCommunityToolkit()
{
const string source = @"
namespace CommunityToolkit.Maui.Analyzers.UnitTests
{
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Hosting;
using CommunityToolkit.Maui;

public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder.UseMauiApp<Microsoft.Maui.Controls.Application>()
.ConfigureFonts(fonts =>
{
fonts.AddFont(""OpenSans-Regular.ttf"", ""OpenSansRegular"");
fonts.AddFont(""OpenSans-Semibold.ttf"", ""OpenSansSemibold"");
});

return builder.Build();
}
}
}";

await VerifyMauiToolkitAnalyzer(source, Diagnostic().WithSpan(13, 4, 13, 61).WithSeverity(DiagnosticSeverity.Error));
}

static Task VerifyMauiToolkitAnalyzer(string source, params DiagnosticResult[] expected)
{
return VerifyAnalyzerAsync(
source,
[
typeof(Options), // CommunityToolkit.Maui
typeof(Core.Options), // CommunityToolkit.Maui.Core;
],
expected);
}
}
Loading