Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp;

namespace CodeCracker
{
Expand Down Expand Up @@ -48,9 +48,13 @@ public static bool HasAutoGeneratedComment(this SyntaxTree tree)
if (!firstToken.HasLeadingTrivia) return false;
trivia = firstToken.LeadingTrivia;
}
var commentLines = trivia.Where(t => t.IsKind(SyntaxKind.SingleLineCommentTrivia)).Take(2).ToList();
if (commentLines.Count != 2) return false;
return commentLines[1].ToString() == "// <auto-generated>";

var comments = trivia.Where(t => t.IsKind(SyntaxKind.SingleLineCommentTrivia) || t.IsKind(SyntaxKind.MultiLineCommentTrivia));
return comments.Any(t =>
{
var s = t.ToString();
return s.Contains("<auto-generated") || s.Contains("<autogenerated");
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,8 @@ public static void SyntaxNodeAnalysis_HasGeneratedCodeAttributeOnNestedClass(str
public static void SymbolicAnalysis_HasGeneratedCodeAttributeOnNestedClass(string source) =>
GetSymbolAnalysisContext<StructDeclarationSyntax>(source).IsGenerated().Should().BeTrue();

[Fact]
public static void SyntaxNodeAnalysis_WithAutoGeneratedCommentBasedOnWebForms() =>
GetSyntaxNodeAnalysisContext<ClassDeclarationSyntax>(
[Theory]
[InlineData(
@"//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
Expand All @@ -304,69 +303,55 @@ namespace WebApplication3
public partial class _Default
{
}
}").IsGenerated().Should().BeTrue();

[Fact]
public static void SyntaxTreeAnalysis_WithAutoGeneratedCommentBasedOnWebForms() =>
GetSyntaxTreeAnalysisContext(
}", false)]
[InlineData(
@"//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------", true)]
[InlineData(
@"// <auto-generated>
// This code was generated by a tool.
namespace WebApplication3
{
public partial class _Default
{
}
}").IsGenerated().Should().BeTrue();

[Fact]
public static void SyntaxTreeAnalysis_WithAutoGeneratedCommentEmpty() =>
GetSyntaxTreeAnalysisContext(
@"//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
}", false)]
[InlineData(
@"// <auto-generated>
// This code was generated by a tool.", true)]
[InlineData(
@"// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------").IsGenerated().Should().BeTrue();

[Fact]
public static void SymbolicAnalysis_WithAutoGeneratedCommentBasedOnWebForms() =>
GetSymbolAnalysisContext<ClassDeclarationSyntax>(
@"//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace WebApplication3
{
public partial class _Default
{
}
}").IsGenerated().Should().BeTrue();

[Fact]
public static void SyntaxNodeAnalysis_WithAutoGeneratedCommentEmpty() =>
GetSyntaxNodeAnalysisContext(
@"//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
}", false)]
[InlineData(
@"// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------").IsGenerated().Should().BeTrue();
// <auto-generated>", true)]
public static void SyntaxNodeAnalysis_WithAutoGeneratedComment(string source, bool isEmpty)
{
GetSyntaxTreeAnalysisContext(source).IsGenerated().Should().BeTrue();
if (isEmpty)
{
GetSyntaxNodeAnalysisContext(source).IsGenerated().Should().BeTrue();
}
else
{
GetSyntaxNodeAnalysisContext<ClassDeclarationSyntax>(source).IsGenerated().Should().BeTrue();
GetSymbolAnalysisContext<ClassDeclarationSyntax>(source).IsGenerated().Should().BeTrue();
}
}

private static SyntaxNodeAnalysisContext GetSyntaxNodeAnalysisContext(string code, string fileName = baseProjectPath + "a.cs") => GetSyntaxNodeAnalysisContext<CompilationUnitSyntax>(code, fileName);

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.