Is it possible to exclude entire namespaces from NDepend analysis?
Asked Answered
A

2

11

I have a setup where Visual Studio 2010 runs test coverage analysis and it's output is absorbed by NDepend during an integration build.

A few assemblies contain generated code that needs to be ignored by NDepend.
Is there a way to do this? Preferably an entire namespace.

Ageratum answered 12/9, 2011 at 15:11 Comment(0)
W
5

Code Query and Rule over LINQ (CQLinq) indeed provides a facility to ignore generated code.

There is the convenient predefined domain named JustMyCode of type ICodeBaseView.

The domain JustMyCode represents a facility of CQLinq to eliminate generated code elements from CQLinq query results. For example the following query will only match large methods that are not generated by a tool (like a UI designer):

from m in JustMyCode.Methods where m.NbLinesOfCode > 30 select m

The set of generated code elements is defined by CQLinq queries prefixed with the CQLinq keyword notmycode. For example the query below matches methods defined in source files whose name ends up with ".designer.cs":

notmycode from m in Methods where
  m.SourceFileDeclAvailable && 
  m.SourceDecls.First().SourceFile.FileName.ToLower().EndsWith(".designer.cs")
select m

The CQLinq queries runner executes all notmycode queries before queries relying on JustMyCode, hence the domain JustMyCode is defined once for all. Obviously the CQLinq compiler emits an error if a notmycode query relies on the JustMyCode domain.

There are 4 default notmycode queries, easily adaptable to match your need. Notes that there is no default notmycode queries for namespaces but you can create your own one(s):

Wilma answered 13/9, 2011 at 9:16 Comment(5)
Patrick, could you elaborate on this and describe how one would go about modifying all the CQL queries used to generate the metrics for a project? I can see the encoded queries embedded in the project file, but for the life of me, I can't find where I would modify them in the UI!Anhanhalt
Just start VisualNDepend.exe, do an analysis of your NDepend project or load the most recent analysis result of your NDepend project, then look at the panel "CQL Query Explorer", this is where you can browse queries and rules and edit them.Wilma
I'm referring to the ability to change all the queries at once. Use case - Copy all the queries using the provided "Copy to Clipboard Default CQL Queries" in the "CQL Query Edit" options. Then, using the regex tool of choice, modify all the queries to ignore a particular namespace, or to honor the NDepend [Generated] attribute, or whatever other global change you want to make to the criteria of the CQL statements. Now, how do I easily replace the default queries for the NDepend project with these updated/modified queries?Anhanhalt
Actually copy/paste from/to "CQL Query Explorer">"Group" use a XML format in the clipboard, you could copy this XML, modify it and then paste it.Wilma
I made that work... and then ran into the issue of not being able to use a join against a type to exclude types with the System.CodeDom.Compiler.GeneratedCode attribute. However, for the OPs purposes, your suggestion will work just fine.Anhanhalt
A
1

Found this in the "Quick summary of methods to refactor"

// Here are some ways to avoid taking account of generated methods.
!( NameIs "InitializeComponent()" OR
   // NDepend.CQL.GeneratedAttribute is defined in 
   // the redistributable assembly $NDependInstallDir$\Lib\NDepend.CQL.dll
   // You can define your own attribute to mark "Generated".
   HasAttribute "OPTIONAL:NDepend.CQL.GeneratedAttribute") 

But that doesn't address the need to modify every CQL query to ensure they all ignore the generated code.

Anhanhalt answered 30/9, 2011 at 0:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.