Skip to content

Replace strings with constants & Improve code quality #22

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

Merged
merged 1 commit into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Immutable;
using System.Linq;
using Flow.Launcher.Localization.Shared;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand All @@ -7,17 +8,12 @@

namespace Flow.Launcher.Localization.Analyzers.Localize
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]

Check warning on line 11 in Flow.Launcher.Localization.Analyzers/Localize/OldGetTranslateAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build

This compiler extension should not be implemented in an assembly containing a reference to Microsoft.CodeAnalysis.Workspaces. The Microsoft.CodeAnalysis.Workspaces assembly is not provided during command line compilation scenarios, so references to it could cause the compiler extension to behave unpredictably. (https://github.com/dotnet/roslyn-analyzers/blob/main/docs/rules/RS1038.md)

Check warning on line 11 in Flow.Launcher.Localization.Analyzers/Localize/OldGetTranslateAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build

This compiler extension should not be implemented in an assembly containing a reference to Microsoft.CodeAnalysis.Workspaces. The Microsoft.CodeAnalysis.Workspaces assembly is not provided during command line compilation scenarios, so references to it could cause the compiler extension to behave unpredictably. (https://github.com/dotnet/roslyn-analyzers/blob/main/docs/rules/RS1038.md)
public class OldGetTranslateAnalyzer : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
ImmutableArray.Create(AnalyzerDiagnostics.OldLocalizationApiUsed);

private static readonly string[] oldLocalizationClasses = { "IPublicAPI", "Internationalization" };
private const string OldLocalizationMethodName = "GetTranslation";

private const string StringFormatMethodName = "Format";
private const string StringFormatTypeName = "string";
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(
AnalyzerDiagnostics.OldLocalizationApiUsed
);

public override void Initialize(AnalysisContext context)
{
Expand Down Expand Up @@ -75,20 +71,20 @@
symbolInfo is SymbolInfo info && IsFormatStringCall(info.Symbol as IMethodSymbol);

private static bool IsFormatStringCall(IMethodSymbol methodSymbol) =>
methodSymbol?.Name is StringFormatMethodName &&
methodSymbol.ContainingType.ToDisplayString() is StringFormatTypeName;
methodSymbol?.Name is Constants.StringFormatMethodName &&
methodSymbol.ContainingType.ToDisplayString() is Constants.StringFormatTypeName;

private static InvocationExpressionSyntax GetFirstArgumentInvocationExpression(InvocationExpressionSyntax invocationExpr) =>
invocationExpr.ArgumentList.Arguments.FirstOrDefault()?.Expression as InvocationExpressionSyntax;

private static bool IsTranslateCall(SymbolInfo symbolInfo) =>
symbolInfo.Symbol is IMethodSymbol innerMethodSymbol &&
innerMethodSymbol.Name is OldLocalizationMethodName &&
oldLocalizationClasses.Contains(innerMethodSymbol.ContainingType.Name);
innerMethodSymbol.Name is Constants.OldLocalizationMethodName &&
Constants.OldLocalizationClasses.Contains(innerMethodSymbol.ContainingType.Name);

private static bool IsTranslateCall(IMethodSymbol methodSymbol) =>
methodSymbol?.Name is OldLocalizationMethodName &&
oldLocalizationClasses.Contains(methodSymbol.ContainingType.Name);
methodSymbol?.Name is Constants.OldLocalizationMethodName &&
Constants.OldLocalizationClasses.Contains(methodSymbol.ContainingType.Name);

private static string GetFirstArgumentStringValue(InvocationExpressionSyntax invocationExpr)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ namespace Flow.Launcher.Localization.Analyzers.Localize
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(OldGetTranslateAnalyzerCodeFixProvider)), Shared]
public class OldGetTranslateAnalyzerCodeFixProvider : CodeFixProvider
{
public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(AnalyzerDiagnostics.OldLocalizationApiUsed.Id);
public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(
AnalyzerDiagnostics.OldLocalizationApiUsed.Id
);

public sealed override FixAllProvider GetFixAllProvider()
{
Expand Down
4 changes: 4 additions & 0 deletions Flow.Launcher.Localization.Shared/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ public static class Constants
public const string IndexAttribute = "index";
public const string NameAttribute = "name";
public const string TypeAttribute = "type";
public const string OldLocalizationMethodName = "GetTranslation";
public const string StringFormatMethodName = "Format";
public const string StringFormatTypeName = "string";

public static readonly Regex LanguagesXamlRegex = new Regex(@"\\Languages\\[^\\]+\.xaml$", RegexOptions.IgnoreCase);
public static readonly string[] OldLocalizationClasses = { "IPublicAPI", "Internationalization" };
}
}
Loading