Discard generated and designer Methods from JustMyCode
A notmycode query: matched methods are removed from the JustMyCode code base view used by most default queries and rules.
It matches several well-identified generated methods, like the ones tagged System.CodeDom.Compiler.GeneratedCodeAttribute or declared in a .designer.cs source file. Enrich it to discard your own generated methods.
notmycode queries run before other queries and rules; modifying one re-runs the queries relying on JustMyCode. Several notmycode methods queries cumulate. Documentation: https://www.ndepend.com/docs/cqlinq-syntax#NotMyCode
It matches several well-identified generated methods, like the ones tagged System.CodeDom.Compiler.GeneratedCodeAttribute or declared in a .designer.cs source file. Enrich it to discard your own generated methods.
notmycode queries run before other queries and rules; modifying one re-runs the queries relying on JustMyCode. Several notmycode methods queries cumulate. Documentation: https://www.ndepend.com/docs/cqlinq-syntax#NotMyCode
CQLinq source
This JustMyCode definition is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.
// <Name>Discard generated and designer Methods from JustMyCode</Name>
// <Id>ND3003:DiscardGeneratedAndDesignerMethodsFromJustMyCode</Id>
notmycode
//
// First define source files paths to discard
//
from a in Application.Assemblies
where a.SourceFileDeclAvailable
let asmSourceFilesPaths = a.SourceDecls.Select(s => s.SourceFile.FilePath)
let sourceFilesPathsToDiscard = (
from filePath in asmSourceFilesPaths
let filePathStr = filePath.ToString() where
filePathStr.EndsWithAny(
// Popular pattern to name generated files.
".g.cs",
".g.vb",
".generated.cs",
".generated.vb",
".xaml", // notmycode WPF xaml code
".cshtml", // notmycode Razor markup
".designer.cs", // notmycode C# Windows Forms designer code
".designer.vb", // notmycode VB.NET Windows Forms designer code
StringComparison.OrdinalIgnoreCase)
||
// notmycode methods in source files in a directory containing generated
filePathStr.IndexOf("generated", StringComparison.OrdinalIgnoreCase) >= 0
select filePath
).ToHashSetEx()
//
// Second: discard methods in sourceFilesPathsToDiscard
//
from m in a.ChildMethods
where (m.SourceFileDeclAvailable &&
sourceFilesPathsToDiscard.Contains(m.SourceDecls.First().SourceFile.FilePath)) ||
// Generated methods might be tagged with this attribute
m.HasAttribute ("System.CodeDom.Compiler.GeneratedCodeAttribute".AllowNoMatch()) ||
// This attributes identifies a type or member that is not part of the user code for an application.
m.HasAttribute ("System.Diagnostics.DebuggerNonUserCodeAttribute".AllowNoMatch()) ||
// Event adder/remover methods generated by the compiler.
((m.IsEventAdder || m.IsEventRemover) && !m.SourceFileDeclAvailable) ||
// Delegate type implementation is always generated
m.ParentType.IsDelegate ||
// Default/implicit constructor generated by the compiler on classes that don't have constructor
m.IsDefaultConstructor ||
// Methods of C#9 record types are considered as generated methods
// when they don't have source file decl.
// Doing so avoids some false positive issues on these methods.
(m.ParentType.IsRecord && (!m.SourceFileDeclAvailable || m.IsGeneratedByCompiler ||
m.IsConstructor || m.IsClassConstructor)) ||
// Razor BuildRenderTree() generated method
(m.IsProtected && m.IsVirtual && m.Name == "BuildRenderTree(RenderTreeBuilder)")
select new { m, m.NbLinesOfCode, m.SourceFileDeclAvailable, m.IsGeneratedByCompiler }
//<Desc>
// A **notmycode** query: matched methods are removed from the *JustMyCode*
// code base view used by most default queries and rules.
//
// It matches several well-identified generated methods, like the ones
// tagged *System.CodeDom.Compiler.GeneratedCodeAttribute* or declared in
// a *.designer.cs* source file. Enrich it to discard your own generated
// methods.
//
// *notmycode* queries run before other queries and rules; modifying one
// re-runs the queries relying on *JustMyCode*. Several *notmycode* methods
// queries cumulate. Documentation:
// https://www.ndepend.com/docs/cqlinq-syntax#NotMyCode
//</Desc>
Open in the interactive Rules Explorer · All default rules
Defining JustMyCode: all 7 items
- Discard generated Assemblies from JustMyCode ND3000
- Discard generated Namespaces from JustMyCode ND3001
- Discard generated Types from JustMyCode ND3002
- Discard generated and designer Methods from JustMyCode ND3003
- Discard generated Fields from JustMyCode ND3004
- JustMyCode code elements
- NotMyCode code elements