You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Summary of the new feature
It would be nice with a rule to find and remove unused using namespace statements similar to what you can do in C# in editors like VS.
Cleaning up unused namespaces can slightly improve type resolution performance and of course it makes scripts more neat and clean.
Proposed technical implementation details (optional)
Use FindAll on the root AST to find all Asts of the following types: TypeExpressionAst, TypeConstraintAst, AttributeAst and CommandAst where the command name is New-Object and try to resolve the specified TypeNames with GetReflectionType. Compare the written typename with the resolved typename and see if part of the namespace is missing, and if so if that part is listed in the list of namespaces. If it is then that namespace is in use and should stay.
For New-Object we can't use GetReflectionType so I'm not sure how to handle that. Type accelerators and in the future type/namespace aliases should probably also be taken into consideration.
What is the latest version of PSScriptAnalyzer at the point of writing
1.22.0
The text was updated successfully, but these errors were encountered:
MartinGC94
changed the title
Add new rule for unused namespaces
Add rule for unused namespaces
Aug 29, 2024
I'd support this but limit to just namespaces built into .NET/PowerShell because otherwise PSSA would need to know about namespaces defined in your module or others and load it. One can technically do that by doing this before running PSSA cmdlet but that's not the case for the editor.
Summary of the new feature
It would be nice with a rule to find and remove unused
using namespace
statements similar to what you can do in C# in editors like VS.Cleaning up unused namespaces can slightly improve type resolution performance and of course it makes scripts more neat and clean.
Proposed technical implementation details (optional)
Use FindAll on the root AST to find all Asts of the following types:
TypeExpressionAst
,TypeConstraintAst
,AttributeAst
andCommandAst
where the command name isNew-Object
and try to resolve the specified TypeNames withGetReflectionType
. Compare the written typename with the resolved typename and see if part of the namespace is missing, and if so if that part is listed in the list of namespaces. If it is then that namespace is in use and should stay.For
New-Object
we can't useGetReflectionType
so I'm not sure how to handle that. Type accelerators and in the future type/namespace aliases should probably also be taken into consideration.What is the latest version of PSScriptAnalyzer at the point of writing
1.22.0
The text was updated successfully, but these errors were encountered: