Skip to content

Commit

Permalink
Merge pull request #211 from earloc/fix/quality-gate
Browse files Browse the repository at this point in the history
fix: quality-gate issues
  • Loading branch information
earloc authored May 15, 2024
2 parents e0766f9 + e34c571 commit ab9cb28
Show file tree
Hide file tree
Showing 5 changed files with 362 additions and 301 deletions.
50 changes: 25 additions & 25 deletions src/TypealizR.Tests/CLI.Tests/Abstractions/FileStorage.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
using FluentAssertions;
using TypealizR.CLI.Abstractions;

namespace TypealizR.Tests.CLI.Tests.Abstractions;
public class FileStorage_Tests
{

[Fact]
public async Task CanAdd_File_With_Content()
{
var sut = new FileStorage();

var fileName = $"{Guid.NewGuid()}.txt";
File.Exists(fileName).Should().BeFalse();

var expected = Guid.NewGuid().ToString();
await sut.AddAsync(fileName, expected);

File.Exists(fileName).Should().BeTrue();

var actual = File.ReadAllText(fileName);
actual.Should().Be(expected);
}

}
using FluentAssertions;
using TypealizR.CLI.Abstractions;

namespace TypealizR.Tests.CLI.Tests.Abstractions;
public class FileStorage_Tests
{

[Fact]
public async Task CanAdd_File_With_Content()
{
var sut = new FileStorage();

var fileName = $"{Guid.NewGuid()}.txt";
File.Exists(fileName).Should().BeFalse();

var expected = Guid.NewGuid().ToString();
await sut.AddAsync(fileName, expected);

File.Exists(fileName).Should().BeTrue();

var actual = await File.ReadAllTextAsync(fileName);
actual.Should().Be(expected);
}

}
168 changes: 84 additions & 84 deletions src/TypealizR/Core/GeneratorOptions.cs
Original file line number Diff line number Diff line change
@@ -1,84 +1,84 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using TypealizR.Diagnostics;

namespace TypealizR.Core;

public sealed class GeneratorOptions
{
public const string msBuildProjectDirectory_BuildProperty = "build_property.msbuildprojectdirectory";
public const string projectDir_BuildProperty = "build_property.projectdir";
public const string rootNamespace_BuildProperty = "build_property.rootnamespace";

public GeneratorOptions(string? projectDirectory, string rootNamespace, IDictionary<string, DiagnosticSeverity> severityConfig)
{
RootNamespace = rootNamespace;
SeverityConfig = severityConfig;
if (projectDirectory is not null)
{
ProjectDirectory = new DirectoryInfo(projectDirectory);
}
}

public DirectoryInfo? ProjectDirectory { get; }
public string RootNamespace { get; }
public IDictionary<string, DiagnosticSeverity> SeverityConfig { get; }

public static GeneratorOptions From(AnalyzerConfigOptions options)
{
if (options is null)
{
throw new ArgumentNullException(nameof(options));
}

if (!options.TryGetValue(msBuildProjectDirectory_BuildProperty, out var projectDirectory))
{
options.TryGetValue(projectDir_BuildProperty, out projectDirectory);
}

options.TryGetValue(rootNamespace_BuildProperty, out var rootNamespace);

var severityConfig = ReadSeverityConfig(options);

return new(
projectDirectory: projectDirectory,
rootNamespace: rootNamespace ?? "",
severityConfig: severityConfig
);
}

[SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "uppercase would not be valid")]
private static IDictionary<string, DiagnosticSeverity> ReadSeverityConfig(AnalyzerConfigOptions options)
{
var severityConfig = new Dictionary<string, DiagnosticSeverity>();

var availableDiagnostics = Enum.GetValues(typeof(DiagnosticsId))
.OfType<DiagnosticsId>()
.Select(x => x.ToString()
);

foreach (var diagnostic in availableDiagnostics)
{
var key = $"dotnet_diagnostic_{diagnostic.ToLowerInvariant()}_severity";

if (options.TryGetValue(key, out var rawValue))
{
if (Enum.TryParse<DiagnosticSeverity>(rawValue, true, out var severity))
{
severityConfig[diagnostic] = severity;
}
else
{
throw new InvalidOperationException($"'{key}' has invalid value '{rawValue}'");
}
}
}

return severityConfig;
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using TypealizR.Diagnostics;

namespace TypealizR.Core;

public sealed class GeneratorOptions
{
public const string msBuildProjectDirectory_BuildProperty = "build_property.msbuildprojectdirectory";
public const string projectDir_BuildProperty = "build_property.projectdir";
public const string rootNamespace_BuildProperty = "build_property.rootnamespace";

public GeneratorOptions(string? projectDirectory, string rootNamespace, IDictionary<string, DiagnosticSeverity> severityConfig)
{
RootNamespace = rootNamespace;
SeverityConfig = severityConfig;
if (projectDirectory is not null)
{
ProjectDirectory = new DirectoryInfo(projectDirectory);
}
}

public DirectoryInfo? ProjectDirectory { get; }
public string RootNamespace { get; }
public IDictionary<string, DiagnosticSeverity> SeverityConfig { get; }

public static GeneratorOptions From(AnalyzerConfigOptions options)
{
if (options is null)
{
throw new ArgumentNullException(nameof(options));
}

if (!options.TryGetValue(msBuildProjectDirectory_BuildProperty, out var projectDirectory))
{
options.TryGetValue(projectDir_BuildProperty, out projectDirectory);
}

options.TryGetValue(rootNamespace_BuildProperty, out var rootNamespace);

var severityConfig = ReadSeverityConfig(options);

return new(
projectDirectory: projectDirectory,
rootNamespace: rootNamespace ?? "",
severityConfig: severityConfig
);
}

[SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "uppercase would not be valid")]
private static Dictionary<string, DiagnosticSeverity> ReadSeverityConfig(AnalyzerConfigOptions options)
{
var severityConfig = new Dictionary<string, DiagnosticSeverity>();

var availableDiagnostics = Enum.GetValues(typeof(DiagnosticsId))
.OfType<DiagnosticsId>()
.Select(x => x.ToString()
);

foreach (var diagnostic in availableDiagnostics)
{
var key = $"dotnet_diagnostic_{diagnostic.ToLowerInvariant()}_severity";

if (options.TryGetValue(key, out var rawValue))
{
if (Enum.TryParse<DiagnosticSeverity>(rawValue, true, out var severity))
{
severityConfig[diagnostic] = severity;
}
else
{
throw new InvalidOperationException($"'{key}' has invalid value '{rawValue}'");
}
}
}

return severityConfig;
}
}
67 changes: 64 additions & 3 deletions src/TypealizR/Core/MemberName.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,66 @@
using System;using System.Linq;using Microsoft.CodeAnalysis;using Microsoft.CodeAnalysis.CSharp;using Microsoft.CodeAnalysis.Text;using TypealizR.Extensions;namespace TypealizR.Core;internal class MemberName{
using System;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
using TypealizR.Extensions;

namespace TypealizR.Core;
internal class MemberName
{
//refactor me
private readonly string name; private string? nameOverride; public MemberName(string raw) { var value = new string(raw.SkipWhile(x => !x.IsValidInIdentifier(true)).ToArray());
private readonly string name;
private string? nameOverride;
internal static readonly char[] separator = new[] { ' ' };

public MemberName(string raw)
{
var value = new string(raw.SkipWhile(x => !x.IsValidInIdentifier(true)).ToArray());

value = value.RemoveAndReplaceDuplicatesOf(" ", "@");
value = value.Replace(".", "");

value = new string(
value
.Trim('_')
.Select((x, i) => x.IsValidInIdentifier(i == 0) ? x : ' ')
.ToArray()
);

value = string.Join(" ",
value.Split(separator, StringSplitOptions.RemoveEmptyEntries)
.Where(x => !string.IsNullOrEmpty(x))
)
.Replace("___", "__")
.ReplaceInvalidForMemberNameWith('_');

name = value.Trim('_');
}

public static implicit operator string(MemberName that) => that.nameOverride ?? that.name;
public override string ToString() => nameOverride ?? name;

internal bool IsValidMethodName()
{
var method = SourceText.From($$"""

namespace ProbingSpace;

public class Programm {
static void main() {
}

void {{name}} () {}
}
""");
var syntax = CSharpSyntaxTree.ParseText(method);
var compilation = CSharpCompilation.Create("probe").AddSyntaxTrees(syntax);
var diagnostics = compilation.GetDiagnostics();

var error = diagnostics.FirstOrDefault(x => x.Id == "CS1519");

return error == null;
}

value = value.RemoveAndReplaceDuplicatesOf(" ", "@"); value = value.Replace(".", ""); value = new string( value .Trim('_') .Select((x, i) => x.IsValidInIdentifier(i == 0) ? x : ' ') .ToArray() ); value = string.Join(" ", value.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) .Where(x => !string.IsNullOrEmpty(x)) ) .Replace("___", "__") .ReplaceInvalidForMemberNameWith('_'); name = value.Trim('_'); } public static implicit operator string(MemberName that) => that.nameOverride ?? that.name; public override string ToString() => nameOverride ?? name; internal bool IsValidMethodName() { var method = SourceText.From($$""" namespace ProbingSpace; public class Programm { static void main() { } void {{name}} () {} }"""); var syntax = CSharpSyntaxTree.ParseText(method); var compilation = CSharpCompilation.Create("probe").AddSyntaxTrees(syntax); var diagnostics = compilation.GetDiagnostics(); var error = diagnostics.FirstOrDefault(x => x.Id == "CS1519"); return error == null; } internal void MakeCompilable() => nameOverride = $"_{name}";}
internal void MakeCompilable() => nameOverride = $"_{name}";
}
Loading

0 comments on commit ab9cb28

Please sign in to comment.