Discard generated Fields from JustMyCode
A notmycode query: matched fields are removed from the JustMyCode code base view used by most default queries and rules.
It matches fields tagged System.CodeDom.Compiler.GeneratedCodeAttribute and Windows Form, WPF and UWP designer-generated fields. Enrich it to discard your own generated fields.
notmycode queries run before other queries and rules; modifying one re-runs the queries relying on JustMyCode. Several notmycode fields queries cumulate. Documentation: https://www.ndepend.com/docs/cqlinq-syntax#NotMyCode
It matches fields tagged System.CodeDom.Compiler.GeneratedCodeAttribute and Windows Form, WPF and UWP designer-generated fields. Enrich it to discard your own generated fields.
notmycode queries run before other queries and rules; modifying one re-runs the queries relying on JustMyCode. Several notmycode fields 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 Fields from JustMyCode</Name>
// <Id>ND3004:DiscardGeneratedFieldsFromJustMyCode</Id>
notmycode
// Define WindowsForm fields defined as fields assigned by the InitializeComponent() WindowsForm methods.
let winFormInitializeComponentsMethods =
Application.Methods.WithName("InitializeComponent()")
.Where(m => m.ParentType.BaseClass != null && m.ParentType.BaseClass.ParentNamespace.Name == "System.Windows.Forms")
let winFormFields = winFormInitializeComponentsMethods.SelectMany(m => m.FieldsAssigned).ToHashSetEx()
// Types deriving from a System.Web.UI class, computed once instead of once per field
let webUITypes = Application.Types.Where(t => t.BaseClasses.Any(bc => bc.ParentNamespace.Name.StartsWith("System.Web.UI"))).ToHashSetEx()
from a in Application.Assemblies
let asmSourceFilesPaths = a.SourceFileDeclAvailable ?
a.SourceDecls.Select(s => s.SourceFile.FilePath) :
new IAbsoluteFilePath[0]
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()
from f in a.ChildFields where
(f.SourceFileDeclAvailable &&
sourceFilesPathsToDiscard.Contains(f.SourceDecls.First().SourceFile.FilePath)) ||
// Discard WindowsForm fields
winFormFields.Contains(f) ||
// Eliminate "components" generated in Windows Form Control context
(f.Name == "components" && f.ParentType.DeriveFrom("System.Windows.Forms.Control".AllowNoMatch())) ||
// Eliminate tagHelper generated fields
f.Name == "_tagHelperStringValueBuffer" ||
// Eliminate XAML generated fields
// IComponentConnector is XAML specific and is automatically implemented for every Window, Page and UserControl.
f.ParentType.Implement( "System.Windows.Markup.IComponentConnector".AllowNoMatch()) || // WPF IComponentConnector
f.ParentType.Implement("Windows.UI.Xaml.Markup.IComponentConnector".AllowNoMatch()) || // UWP IComponentConnector
f.HasAttribute ("System.CodeDom.Compiler.GeneratedCodeAttribute".AllowNoMatch()) ||
// Property backing fields generated by the compiler
(f.IsGeneratedByCompiler && !f.IsEventDelegateObject) ||
// Match fields generated by the ASP.NET infrastructure
// in System.Web.UI classes like Page, Control, MasterPage...
(f.FieldType != null &&
webUITypes.Contains(f.ParentType) &&
(f.FieldType.ParentNamespace.Name.StartsWith("System.Web.UI") ||
f.FieldType.BaseClasses.Any(bc => bc.ParentNamespace.Name.StartsWith("System.Web.UI"))
)) ||
// Delegate type implementation is always generated
f.ParentType.IsDelegate ||
// Match fields named 'mappingSource' for DataContext classes
(f.Name == "mappingSource" && f.ParentType.DeriveFrom("System.Data.Linq.DataContext".AllowNoMatch()))
select f
//<Desc>
// A **notmycode** query: matched fields are removed from the *JustMyCode*
// code base view used by most default queries and rules.
//
// It matches fields tagged *System.CodeDom.Compiler.GeneratedCodeAttribute*
// and *Windows Form*, *WPF* and *UWP* designer-generated fields. Enrich it
// to discard your own generated fields.
//
// *notmycode* queries run before other queries and rules; modifying one
// re-runs the queries relying on *JustMyCode*. Several *notmycode* fields
// 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