Skip to content

Commit

Permalink
Released 1.1.0 (#30)
Browse files Browse the repository at this point in the history
* Released 1.1.0

* Fix Y0002 violations

* Fix more Y0002

* Simplify tests
  • Loading branch information
Youssef1313 authored Mar 14, 2021
1 parent 825db5f commit c55ba09
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="YAnalyzers" Version="1.0.0" />
<PackageReference Include="YAnalyzers" Version="1.1.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public class UseImplicitOrExplicitTypeCodeFix : CodeFixProvider

public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
SyntaxNode? root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
if (root is null)
{
return;
}

var diagnostic = context.Diagnostics.First();
var node = root.FindNode(diagnostic.Location.SourceSpan);
Diagnostic diagnostic = context.Diagnostics.First();
SyntaxNode node = root.FindNode(diagnostic.Location.SourceSpan);

TypeSyntax? type = null;
if (node is VariableDeclarationSyntax variableDeclaration)
Expand Down Expand Up @@ -78,19 +78,19 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)

private static Task<Document> UseImplicitType(Document document, SyntaxNode root, TypeSyntax typeSyntax)
{
var newNode = SyntaxFactory.IdentifierName(SyntaxFacts.GetText(SyntaxKind.VarKeyword)).WithTriviaFrom(typeSyntax);
var newDocument = document.WithSyntaxRoot(root.ReplaceNode(typeSyntax, newNode));
IdentifierNameSyntax newNode = SyntaxFactory.IdentifierName(SyntaxFacts.GetText(SyntaxKind.VarKeyword)).WithTriviaFrom(typeSyntax);
Document newDocument = document.WithSyntaxRoot(root.ReplaceNode(typeSyntax, newNode));
return Task.FromResult(newDocument);
}

private static async Task<Document> UseExplicitTypeAsync(Document document, SyntaxNode root, TypeSyntax typeSyntax, CancellationToken cancellationToken)
{
Debug.Assert(typeSyntax.IsVar);
var model = (await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false))!;
var typeInfo = model.GetTypeInfo(typeSyntax, cancellationToken);
var generator = SyntaxGenerator.GetGenerator(document);
var newNode = generator.TypeExpression(typeInfo.ConvertedType).WithTriviaFrom(typeSyntax);
SemanticModel model = (await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false))!;
TypeInfo typeInfo = model.GetTypeInfo(typeSyntax, cancellationToken);

SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
SyntaxNode newNode = generator.TypeExpression(typeInfo.ConvertedType).WithTriviaFrom(typeSyntax);

return document.WithSyntaxRoot(root.ReplaceNode(typeSyntax, newNode));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ private static void AnalyzeVariableDeclaration(SyntaxNodeAnalysisContext context
{
var node = (VariableDeclarationSyntax)context.Node;

if (!ShouldAnalyze(node, out var initializer))
if (!ShouldAnalyze(node, out EqualsValueClauseSyntax? initializer))
{
return;
}

// TODO: Create a utilities project containing NotNullWhenAttribute and use it to avoid suppression.
TypeInfo typeInfo = context.SemanticModel.GetTypeInfo(initializer!.Value, context.CancellationToken);

var shouldUseVar = ShouldUseVar(node, initializer, typeInfo, context.SemanticModel);
bool shouldUseVar = ShouldUseVar(node, initializer, typeInfo, context.SemanticModel);
if (node.Type.IsVar && !shouldUseVar)
{
context.ReportDiagnostic(Diagnostic.Create(s_useExplicitTypeRule, node.GetLocation()));
Expand Down
34 changes: 10 additions & 24 deletions src/UnitTests/UseImplicitOrExplicitTypeAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,13 @@ class Program
{
void M()
{
{|#0:string s1 = """"|};
{|#1:string s1Interpolated = $""""|};
{|#2:string s1Verbatim = @""""|};
{|#3:string s1VerbatimInterpolated = @$""""|};
{|#4:string s1InterpolatedVerbatim = $@""""|};
{|Y0001:string s1 = """"|};
{|Y0001:string s1Interpolated = $""""|};
{|Y0001:string s1Verbatim = @""""|};
{|Y0001:string s1VerbatimInterpolated = @$""""|};
{|Y0001:string s1InterpolatedVerbatim = $@""""|};
{|#5:int i1 = 0|};
{|Y0001:int i1 = 0|};
uint ui1 = 0;
}
Expand Down Expand Up @@ -292,16 +292,7 @@ void M2()
}
}
";
var expectedDiagnostics = new[]
{
VerifyCS.Diagnostic(UseImplicitOrExplicitTypeAnalyzer.UseImplicitTypeDiagnosticId).WithLocation(0),
VerifyCS.Diagnostic(UseImplicitOrExplicitTypeAnalyzer.UseImplicitTypeDiagnosticId).WithLocation(1),
VerifyCS.Diagnostic(UseImplicitOrExplicitTypeAnalyzer.UseImplicitTypeDiagnosticId).WithLocation(2),
VerifyCS.Diagnostic(UseImplicitOrExplicitTypeAnalyzer.UseImplicitTypeDiagnosticId).WithLocation(3),
VerifyCS.Diagnostic(UseImplicitOrExplicitTypeAnalyzer.UseImplicitTypeDiagnosticId).WithLocation(4),
VerifyCS.Diagnostic(UseImplicitOrExplicitTypeAnalyzer.UseImplicitTypeDiagnosticId).WithLocation(5),
};
await VerifyCS.VerifyCodeFixAsync(code, expectedDiagnostics, fixedCode);
await VerifyCS.VerifyCodeFixAsync(code, fixedCode);
}

[TestMethod]
Expand Down Expand Up @@ -345,8 +336,8 @@ class C
{
public void Process()
{
{|#0:A a = new A()|};
{|#1:IInterface s = a as IInterface|};
{|Y0001:A a = new A()|};
{|Y0001:IInterface s = a as IInterface|};
IInterface i = a;
}
}
Expand Down Expand Up @@ -375,12 +366,7 @@ interface IInterface
{
}
";
var expectedDiagnostics = new[]
{
VerifyCS.Diagnostic(UseImplicitOrExplicitTypeAnalyzer.UseImplicitTypeDiagnosticId).WithLocation(0),
VerifyCS.Diagnostic(UseImplicitOrExplicitTypeAnalyzer.UseImplicitTypeDiagnosticId).WithLocation(1),
};
await VerifyCS.VerifyCodeFixAsync(code, expectedDiagnostics, fixedCode);
await VerifyCS.VerifyCodeFixAsync(code, fixedCode);
}

[TestMethod]
Expand Down
5 changes: 3 additions & 2 deletions src/UnitTests/Verifiers/CSharpAnalyzerVerifier`1+Test.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.CodeAnalysis.CSharp.Testing;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Testing;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Testing.Verifiers;

Expand All @@ -13,7 +14,7 @@ public Test()
{
SolutionTransforms.Add((solution, projectId) =>
{
var compilationOptions = solution.GetProject(projectId)!.CompilationOptions!;
CompilationOptions compilationOptions = solution.GetProject(projectId)!.CompilationOptions!;
compilationOptions = compilationOptions.WithSpecificDiagnosticOptions(
compilationOptions.SpecificDiagnosticOptions.SetItems(CSharpVerifierHelper.NullableWarnings));
solution = solution.WithProjectCompilationOptions(projectId, compilationOptions);
Expand Down
5 changes: 3 additions & 2 deletions src/UnitTests/Verifiers/CSharpCodeFixVerifier`2+Test.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Testing;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Testing.Verifiers;
Expand All @@ -15,7 +16,7 @@ public Test()
{
SolutionTransforms.Add((solution, projectId) =>
{
var compilationOptions = solution.GetProject(projectId)!.CompilationOptions!;
CompilationOptions compilationOptions = solution.GetProject(projectId)!.CompilationOptions!;
compilationOptions = compilationOptions.WithSpecificDiagnosticOptions(
compilationOptions.SpecificDiagnosticOptions.SetItems(CSharpVerifierHelper.NullableWarnings));
solution = solution.WithProjectCompilationOptions(projectId, compilationOptions);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.CSharp.Testing;
using Microsoft.CodeAnalysis.Testing.Verifiers;

Expand All @@ -13,7 +14,7 @@ public Test()
{
SolutionTransforms.Add((solution, projectId) =>
{
var compilationOptions = solution.GetProject(projectId)!.CompilationOptions!;
CompilationOptions compilationOptions = solution.GetProject(projectId)!.CompilationOptions!;
compilationOptions = compilationOptions.WithSpecificDiagnosticOptions(
compilationOptions.SpecificDiagnosticOptions.SetItems(CSharpVerifierHelper.NullableWarnings));
solution = solution.WithProjectCompilationOptions(projectId, compilationOptions);
Expand Down
4 changes: 2 additions & 2 deletions src/UnitTests/Verifiers/CSharpVerifierHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ internal static class CSharpVerifierHelper
private static ImmutableDictionary<string, ReportDiagnostic> GetNullableWarningsFromCompiler()
{
string[] args = { "/warnaserror:nullable" };
var commandLineArguments = CSharpCommandLineParser.Default.Parse(args, baseDirectory: Environment.CurrentDirectory, sdkDirectory: Environment.CurrentDirectory);
var nullableWarnings = commandLineArguments.CompilationOptions.SpecificDiagnosticOptions;
CSharpCommandLineArguments commandLineArguments = CSharpCommandLineParser.Default.Parse(args, baseDirectory: Environment.CurrentDirectory, sdkDirectory: Environment.CurrentDirectory);
ImmutableDictionary<string, ReportDiagnostic> nullableWarnings = commandLineArguments.CompilationOptions.SpecificDiagnosticOptions;

// Workaround for https://github.com/dotnet/roslyn/issues/41610
nullableWarnings = nullableWarnings
Expand Down
4 changes: 2 additions & 2 deletions src/YAnalyzers.Package/YAnalyzers.Package.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

<PropertyGroup>
<PackageId>*$(MSBuildProjectFile)*</PackageId>
<PackageVersion>2.0.0.0</PackageVersion>
<PackageVersion>1.2.0.0</PackageVersion>
<Authors>Youssef1313</Authors>
<PackageProjectUrl>https://github.com/Youssef1313/YAnalyzers</PackageProjectUrl>
<RepositoryUrl>https://github.com/Youssef1313/YAnalyzers</RepositoryUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<Description>YAnalyzers</Description>
<PackageReleaseNotes></PackageReleaseNotes>
<PackageTags>YAnalyzers analyzers var implicit typing</PackageTags>
<PackageTags>YAnalyzers analyzers var implicit typing roslyn</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);_AddAnalyzersToOutput</TargetsForTfmSpecificContentInPackage>
</PropertyGroup>
Expand Down

0 comments on commit c55ba09

Please sign in to comment.