Skip to content

Commit

Permalink
Improve generated code recognition (#911)
Browse files Browse the repository at this point in the history
  • Loading branch information
Amaury Levé authored Nov 7, 2017
1 parent fde0c9c commit 23019c0
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 13 deletions.
2 changes: 1 addition & 1 deletion its/projects/AutoGeneratedFiles/autogenerated_comment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System;
namespace SomeNamespace
{
class Class_01
class Class_01_1
{
static void Method(string[] args)
{
Expand Down
2 changes: 1 addition & 1 deletion its/projects/AutoGeneratedFiles/autogenerated_comment2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace SomeNamespace
{
class Class_02
class Class_01_2
{
static void Method(string[] args)
{
Expand Down
15 changes: 15 additions & 0 deletions its/projects/AutoGeneratedFiles/autogenerated_comment3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Generated by the protocol buffer compiler. DO NOT EDIT!

using System;

namespace SomeNamespace
{
class Class_01_3
{
static void Method(string[] args)
{
object obj = null;
Console.WriteLine(obj.ToString());
}
}
}
21 changes: 21 additions & 0 deletions its/projects/AutoGeneratedFiles/autogenerated_comment4.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
*
* OpenAPI spec version: 1.0.0
*
* Generated by: https://github.com/swagger-api/swagger-codegen.git
*/

using System;

namespace SomeNamespace
{
class Class_01_4
{
static void Method(string[] args)
{
object obj = null;
Console.WriteLine(obj.ToString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public virtual bool IsGenerated(SyntaxTree tree)
private static readonly string[] AutoGeneratedCommentParts =
{
"<AUTO-GENERATED",
"<AUTOGENERATED"
"<AUTOGENERATED",
"GENERATED BY"
};
private static readonly string[] GeneratedCodeAttributes =
{
Expand Down Expand Up @@ -83,14 +84,13 @@ private bool HasAutoGeneratedComment(SyntaxTree tree)
return false;
}

var trivia = firstToken.LeadingTrivia;

var comments = trivia.Where(IsTriviaComment);
return comments.Any(t =>
{
var commentText = t.ToString().ToUpperInvariant();
return AutoGeneratedCommentParts.Any(part => commentText.Contains(part));
});
return firstToken.LeadingTrivia
.Where(IsTriviaComment)
.Any(trivia =>
{
var commentText = trivia.ToString().ToUpperInvariant();
return AutoGeneratedCommentParts.Any(part => commentText.Contains(part));
});
}

private bool HasGeneratedCodeAttribute(SyntaxTree tree)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SonarAnalyzer.Helpers;
using System.Linq;
using System.Threading.Tasks;
using CS = SonarAnalyzer.Rules.CSharp;
using VB = SonarAnalyzer.Rules.VisualBasic;

Expand Down Expand Up @@ -130,6 +130,19 @@ void M()
}";
VerifyEmpty("test.cs", sourceCs, new CS.EmptyStatement());

sourceCs =
@"/*
* No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
*
* OpenAPI spec version: 1.0.0
*
* Generated by: https://github.com/swagger-api/swagger-codegen.git
*/";
VerifyEmpty("test.cs", sourceCs, new CS.EmptyStatement());

sourceCs = "// Generated by the protocol buffer compiler. DO NOT EDIT!";
VerifyEmpty("test.cs", sourceCs, new CS.EmptyStatement());


const string sourceVb =
@"'------------------------------------------------------------------------------
Expand Down

0 comments on commit 23019c0

Please sign in to comment.