-1

Is there any way to remove unused using statement in compile time? it can be any cli tool or dotnet tool.

The reason is follwing: We have code generator, and this code generator generates a lot of using, but in some cases the usings are need, and some not. But at generation time there is no way to decide which using is used and which is not used in later code.

So it wolud be great when we have tool to remove the unused using statements. The editor command are not working of course, beacuse when we rebuild the project then the generated files will be newly generated. Therefore the mechasnim must be run at compile time, after the generator has done.

I checked the dotnet-format tool, but there is no option for this.

EDIT 1: There is a simple sample for imput file, which is the input of the generator.

import valuedto.dml
import valuemodel.dml

service: SampleService
{
    controller: ValueController
    {
        public:
        {
            PublicValueDTO GetValue( string id );
        }
        public:
        {
            PrivateValueModel GetValuePrivate( string id );
        }
    }
}

As you can see the service file imports two different model files. The one dml-file contains the public dto-s the other contains the private models.

The generator uses this as imput, and generates a lots of code:

  • clinet interface
  • httmp clinet implenetation
  • controller implementation
  • client dll
  • etc...

The one of the generators are just collect the public functions, and create a assembly for 3rd-party clients, to use the public interface. But in this casae very hard to decide which imports must be use. In real world, there is mor import, the import is imported are other import recursively.

If the the public interface contains using-s from private models, the generated code is not compilable



Please concentrate the main question in your answer, not the actual implementations. Beacuse it just a very very small sample, and not a real one (missing versions, permissions, route, verb, multipart, etc...)



György Gulyás
  • 1,290
  • 11
  • 37

3 Answers3

4

Removing "during compilation" makes no sense, because of reason #1 below. I'll suspect you meant "outside of an IDE/not manually".

However, why do you care?

  1. Using directives are no IL artifacts and don't contribute to code size or such
  2. If the code generator generates ambiguous code because of extra usings, removing "unused" is not possible anyway and the generate code is invalid and the generator would need fixing.
  3. Typically, generated code should be treated as immutable and opaque (and don't look into it, except maybe debugging). Don't care too much about its beauty.

The last point is arguably open for discussion. Personally, I do care for nicely formatted and concise generated code, and if only for the sole reason of reasoning about it or debugging it in single step mode. But that would be the job of the generator though.

Having that said, there are ReSharper command line tools available that might be able to do that during a build (beware of question closure for "seeking for a tool" ;-))

One more thought:

But at generation time there is no way to decide which using is used and which is not used in later code.

I would challenge that. The generator should know which exact types it generates/uses later on. After all, you typically generate an AST or alike in memory first and then write it to a file. If not, the design of the generator is maybe... to simplistic? E.g. first blurting out a bunch of using-directives of could be stuff, and then the actual code? If you have the option to improve the actual generator - which would be the correct place to handle such things anyway - you should ask a specific question on that and explain, why in your case you think it cannot know things before generating the actual file.

Christian.K
  • 47,778
  • 10
  • 99
  • 143
  • 1
    You have right there is a possibility, to detect witch using used later, but it is very hard challange, and a lot of effort. I will edit my question, to make more clear. And next time, please try to answer the question, not provide an alternative way, beasue int the background has a lot more reason, but I want to simplify my question much as possible. – György Gulyás Aug 27 '21 at 05:06
4

The free jetbrains cli tool can do code cleanups. Removing unused using directives is one of the things it can do.

https://www.jetbrains.com/help/resharper/ReSharper_Command_Line_Tools.html#install-and-use-resharper-command-line-tools-as-net-core-tools

Morten Toudahl
  • 452
  • 1
  • 4
  • 13
-1

You can press Ctrl+R , Ctrl+G (visual studio) to remove unused using and sort your namespaces from the working file.

I too have been looking for a tool for this. This should be a special tool and should not be part of the compile stage.

ouflak
  • 2,458
  • 10
  • 44
  • 49
  • 1
    Dear @Sourabh Rustagi, I know the the editor tool, of cource but is compile on linux machine in pipeline. The whole compilation is automaticcaly, without any manual activity. – György Gulyás Sep 02 '21 at 12:24