Skip to content

Commit

Permalink
Merge remote-tracking branch 'DotNetAnalyzers/stabilization'
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Feb 20, 2019
2 parents 773bbeb + cbec141 commit 09aa5be
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 15 deletions.
2 changes: 1 addition & 1 deletion StyleCop.Analyzers/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<ItemGroup>
<PackageReference Include="AsyncUsageAnalyzers" Version="1.0.0-alpha003" PrivateAssets="all" />
<PackageReference Include="DotNetAnalyzers.DocumentationAnalyzers" Version="1.0.0-beta.46" PrivateAssets="all" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.1-rc.101" PrivateAssets="all" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.1-rc.108" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeStyle" Version="2.11.0-beta2-63603-03" PrivateAssets="all" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ private static ImmutableArray<SyntaxNode> CreateQueryNodeList(QueryExpressionSyn
private static void ProcessQueryBody(QueryBodySyntax body, List<SyntaxNode> queryNodes)
{
queryNodes.AddRange(body.Clauses);
queryNodes.Add(body.SelectOrGroup);

if (!body.SelectOrGroup.IsMissing)
{
queryNodes.Add(body.SelectOrGroup);
}

if (body.Continuation != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,23 @@ public override FixAllProvider GetFixAllProvider()
}

/// <inheritdoc/>
public override Task RegisterCodeFixesAsync(CodeFixContext context)
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var syntaxRoot = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

foreach (var diagnostic in context.Diagnostics)
{
context.RegisterCodeFix(
CodeAction.Create(
ReadabilityResources.SA1134CodeFix,
cancellationToken => GetTransformedDocumentAsync(context.Document, diagnostic, cancellationToken),
nameof(SA1134CodeFixProvider)),
diagnostic);
// Do not offer the code fix if the error is found at an invalid node (like IncompleteMemberSyntax)
if (syntaxRoot.FindNode(diagnostic.Location.SourceSpan) is AttributeListSyntax)
{
context.RegisterCodeFix(
CodeAction.Create(
ReadabilityResources.SA1134CodeFix,
cancellationToken => GetTransformedDocumentAsync(context.Document, diagnostic, cancellationToken),
nameof(SA1134CodeFixProvider)),
diagnostic);
}
}

return SpecializedTasks.CompletedTask;
}

private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<title>$id$</title>
<authors>Sam Harwell et. al.</authors>
<owners>Sam Harwell</owners>
<licenseUrl>https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/$GitCommitIdShort$/LICENSE</licenseUrl>
<license type="expression">Apache-2.0</license>
<projectUrl>https://github.com/DotNetAnalyzers/StyleCopAnalyzers</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>An implementation of StyleCop's rules using Roslyn analyzers and code fixes</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<title>$id$</title>
<authors>Sam Harwell et. al.</authors>
<owners>Sam Harwell</owners>
<licenseUrl>https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/$GitCommitIdShort$/LICENSE</licenseUrl>
<license type="expression">Apache-2.0</license>
<projectUrl>https://github.com/DotNetAnalyzers/StyleCopAnalyzers</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>An implementation of StyleCop's rules using Roslyn analyzers and code fixes</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace StyleCop.Analyzers.Test.ReadabilityRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.ReadabilityRules;
using TestHelper;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.ReadabilityRules.SA110xQueryClauses,
Expand Down Expand Up @@ -365,5 +365,91 @@ public void TestMethod()

await VerifyCSharpFixAsync(testCode, expectedDiagnostics, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(2888, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2888")]
public async Task TestQueryExpressionWithMissingSelectAsync()
{
var testCode = @"namespace TestNamespace
{
using System.Linq;
public class TestClass
{
private int[] testArray = { 1, 2, 3, 4, 5 };
public void TestMethod()
{
var x = from element in testArray
where element > 1;
}
}
}
";

var expectedDiagnostics = new DiagnosticResult("CS0742", DiagnosticSeverity.Error).WithLocation(12, 38).WithMessage("A query body must end with a select clause or a group clause");
await VerifyCSharpFixAsync(testCode, expectedDiagnostics, testCode, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(2888, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2888")]
public async Task TestQueryExpressionWithMissingSelect2Async()
{
var testCode = @"namespace TestNamespace
{
using System.Linq;
public class TestClass
{
private int[] testArray = { 1, 2, 3, 4, 5 };
public void TestMethod()
{
var x = from element in testArray where element < 3
where element > 1;
}
}
}
";
var fixedTestCode = @"namespace TestNamespace
{
using System.Linq;
public class TestClass
{
private int[] testArray = { 1, 2, 3, 4, 5 };
public void TestMethod()
{
var x = from element in testArray
where element < 3
where element > 1;
}
}
}
";

await new CSharpTest
{
TestState =
{
Sources = { testCode },
ExpectedDiagnostics =
{
Diagnostic(SA110xQueryClauses.SA1103Descriptor).WithLocation(11, 21),
new DiagnosticResult("CS0742", DiagnosticSeverity.Error).WithLocation(12, 38).WithMessage("A query body must end with a select clause or a group clause"),
},
},
FixedState =
{
Sources = { fixedTestCode },
ExpectedDiagnostics =
{
new DiagnosticResult("CS0742", DiagnosticSeverity.Error).WithLocation(13, 38).WithMessage("A query body must end with a select clause or a group clause"),
},
},
CodeFixIndex = 1,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -409,5 +409,35 @@ public class TestClass<[Test(""Test1"")][Test(""Test2"")]T>

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
/// Verifies that passing an invalid member syntax into the codefix will not change the code.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
[WorkItem(2894, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2894")]
public async Task VerifyInvalidMemberSyntaxInCodeFixAsync()
{
string testCode = @"class Program
{
static void Main(string[] args)
{
{
}[;]
}
}
";

DiagnosticResult[] expected =
{
DiagnosticResult.CompilerError("CS1513").WithLocation(6, 10),
Diagnostic().WithLocation(6, 10),
DiagnosticResult.CompilerError("CS1001").WithLocation(6, 11),
DiagnosticResult.CompilerError("CS1001").WithLocation(6, 11),
DiagnosticResult.CompilerError("CS1022").WithLocation(8, 1),
};

await VerifyCSharpFixAsync(testCode, expected, testCode, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,11 @@ private static void HandleSelectOrGroup(SelectOrGroupClauseSyntax selectOrGroup,
{
case SyntaxKind.SelectClause:
var selectClause = (SelectClauseSyntax)selectOrGroup;
tokensToCheck.Add(selectClause.SelectKeyword);
if (!selectClause.IsMissing)
{
tokensToCheck.Add(selectClause.SelectKeyword);
}

break;
case SyntaxKind.GroupClause:
var groupClause = (GroupClauseSyntax)selectOrGroup;
Expand Down
5 changes: 5 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"sdk": {
"version": "2.1.504"
}
}

0 comments on commit 09aa5be

Please sign in to comment.