User/ryanbod/enable codeanalysis for calculatorplugin (#5130)
* Turning on static analysis and removing warning for NoMages. * Fixing static analysis errors in NumberTranslator.cs * Fix: Severity Code Description Project File Line Suppression State Error CA1810 Initialize all static fields in 'Main' when those fields are declared and remove the explicit static constructor Microsoft.Plugin.Calculator C:\Repos\PowerToys\src\modules\launcher\Plugins\Microsoft.Plugin.Calculator\Main.cs 30 Active * Throwing exception if arguments are null to fix static analysis errors. * Ignoring CA1031 * Logging exceptions for Calculator queries.
This commit is contained in:
parent
b59ec5e78b
commit
25d43354b3
|
@ -6,6 +6,7 @@ using System.Text.RegularExpressions;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using Mages.Core;
|
using Mages.Core;
|
||||||
|
using Wox.Infrastructure.Logger;
|
||||||
using Wox.Plugin;
|
using Wox.Plugin;
|
||||||
|
|
||||||
namespace Microsoft.Plugin.Calculator
|
namespace Microsoft.Plugin.Calculator
|
||||||
|
@ -22,18 +23,18 @@ namespace Microsoft.Plugin.Calculator
|
||||||
@"[ei]|[0-9]|[\+\-\*\/\^\., ""]|[\(\)\|\!\[\]]" +
|
@"[ei]|[0-9]|[\+\-\*\/\^\., ""]|[\(\)\|\!\[\]]" +
|
||||||
@")+$", RegexOptions.Compiled);
|
@")+$", RegexOptions.Compiled);
|
||||||
private static readonly Regex RegBrackets = new Regex(@"[\(\)\[\]]", RegexOptions.Compiled);
|
private static readonly Regex RegBrackets = new Regex(@"[\(\)\[\]]", RegexOptions.Compiled);
|
||||||
private static readonly Engine MagesEngine;
|
private static readonly Engine MagesEngine = new Engine();
|
||||||
private PluginInitContext Context { get; set; }
|
private PluginInitContext Context { get; set; }
|
||||||
private string IconPath { get; set; }
|
private string IconPath { get; set; }
|
||||||
private bool _disposed = false;
|
private bool _disposed = false;
|
||||||
|
|
||||||
static Main()
|
|
||||||
{
|
|
||||||
MagesEngine = new Engine();
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Result> Query(Query query)
|
public List<Result> Query(Query query)
|
||||||
{
|
{
|
||||||
|
if(query == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(paramName: nameof(query));
|
||||||
|
}
|
||||||
|
|
||||||
if (query.Search.Length <= 2 // don't affect when user only input "e" or "i" keyword
|
if (query.Search.Length <= 2 // don't affect when user only input "e" or "i" keyword
|
||||||
|| !RegValidExpressChar.IsMatch(query.Search)
|
|| !RegValidExpressChar.IsMatch(query.Search)
|
||||||
|| !IsBracketComplete(query.Search)) return new List<Result>();
|
|| !IsBracketComplete(query.Search)) return new List<Result>();
|
||||||
|
@ -89,9 +90,12 @@ namespace Microsoft.Plugin.Calculator
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch
|
//We want to keep the process alive if any the mages library throws any exceptions.
|
||||||
|
#pragma warning disable CA1031 // Do not catch general exception types
|
||||||
|
catch(Exception e)
|
||||||
|
#pragma warning restore CA1031 // Do not catch general exception types
|
||||||
{
|
{
|
||||||
// ignored
|
Log.Exception($"|Microsoft.Plugin.Calculator.Main.Query|Exception when query for <{query}>", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new List<Result>();
|
return new List<Result>();
|
||||||
|
@ -118,6 +122,11 @@ namespace Microsoft.Plugin.Calculator
|
||||||
|
|
||||||
public void Init(PluginInitContext context)
|
public void Init(PluginInitContext context)
|
||||||
{
|
{
|
||||||
|
if(context == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(paramName: nameof(context));
|
||||||
|
}
|
||||||
|
|
||||||
Context = context;
|
Context = context;
|
||||||
Context.API.ThemeChanged += OnThemeChanged;
|
Context.API.ThemeChanged += OnThemeChanged;
|
||||||
UpdateIconPath(Context.API.GetCurrentTheme());
|
UpdateIconPath(Context.API.GetCurrentTheme());
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||||
<WarningLevel>4</WarningLevel>
|
<WarningLevel>4</WarningLevel>
|
||||||
<Optimize>false</Optimize>
|
<Optimize>false</Optimize>
|
||||||
|
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||||
|
@ -97,7 +98,13 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0" />
|
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0" />
|
||||||
<PackageReference Include="Mages" Version="1.6.0" />
|
<PackageReference Include="Mages" Version="1.6.0" >
|
||||||
|
<NoWarn>NU1701</NoWarn>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.6">
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
</PackageReference>
|
||||||
<PackageReference Include="System.Runtime" Version="4.3.1" />
|
<PackageReference Include="System.Runtime" Version="4.3.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
|
@ -36,6 +36,16 @@ namespace Microsoft.Plugin.Calculator
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static NumberTranslator Create(CultureInfo sourceCulture, CultureInfo targetCulture)
|
public static NumberTranslator Create(CultureInfo sourceCulture, CultureInfo targetCulture)
|
||||||
{
|
{
|
||||||
|
if (sourceCulture == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(paramName:nameof(sourceCulture));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (targetCulture == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(paramName: nameof(sourceCulture));
|
||||||
|
}
|
||||||
|
|
||||||
bool conversionRequired = sourceCulture.NumberFormat.NumberDecimalSeparator != targetCulture.NumberFormat.NumberDecimalSeparator
|
bool conversionRequired = sourceCulture.NumberFormat.NumberDecimalSeparator != targetCulture.NumberFormat.NumberDecimalSeparator
|
||||||
|| sourceCulture.NumberFormat.PercentGroupSeparator != targetCulture.NumberFormat.PercentGroupSeparator
|
|| sourceCulture.NumberFormat.PercentGroupSeparator != targetCulture.NumberFormat.PercentGroupSeparator
|
||||||
|| sourceCulture.NumberFormat.NumberGroupSizes != targetCulture.NumberFormat.NumberGroupSizes;
|
|| sourceCulture.NumberFormat.NumberGroupSizes != targetCulture.NumberFormat.NumberGroupSizes;
|
||||||
|
@ -51,7 +61,7 @@ namespace Microsoft.Plugin.Calculator
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public string Translate(string input)
|
public string Translate(string input)
|
||||||
{
|
{
|
||||||
return this.Translate(input, this.sourceCulture, this.targetCulture, this.splitRegexForSource);
|
return Translate(input, this.sourceCulture, this.targetCulture, this.splitRegexForSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -61,10 +71,10 @@ namespace Microsoft.Plugin.Calculator
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public string TranslateBack(string input)
|
public string TranslateBack(string input)
|
||||||
{
|
{
|
||||||
return this.Translate(input, this.targetCulture, this.sourceCulture, this.splitRegexForTarget);
|
return Translate(input, this.targetCulture, this.sourceCulture, this.splitRegexForTarget);
|
||||||
}
|
}
|
||||||
|
|
||||||
private string Translate(string input, CultureInfo cultureFrom, CultureInfo cultureTo, Regex splitRegex)
|
private static string Translate(string input, CultureInfo cultureFrom, CultureInfo cultureTo, Regex splitRegex)
|
||||||
{
|
{
|
||||||
var outputBuilder = new StringBuilder();
|
var outputBuilder = new StringBuilder();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue