Skip to content

Commit

Permalink
Exclude duplicate importS
Browse files Browse the repository at this point in the history
  • Loading branch information
rkodev committed Sep 27, 2023
1 parent 026c198 commit 2d5dcf7
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,17 @@ public override void WriteCodeElement(CodeFileDeclaration codeElement, LanguageW
}
);
conventions.WriteAutoGeneratedStart(writer);
_codeUsingWriter.WriteCodeElement(usings, ns, writer);

// remove duplicate using, keep a single using for each internal type in the same namespace
var enumeratedUsing = usings.ToArray();
var filteredUsing = enumeratedUsing.Where(x => x.IsExternal)
.Union(enumeratedUsing.ToArray()
.Where(x => x is { IsExternal: false, Declaration.TypeDefinition: not null })
.GroupBy(x =>
$"{x.Declaration!.TypeDefinition!.GetImmediateParentOfType<CodeNamespace>().Name}.{x.Declaration?.Name.ToLowerInvariant()}")
.Select(x => x.First()));

_codeUsingWriter.WriteCodeElement(filteredUsing, ns, writer);
}
}

Expand Down

0 comments on commit 2d5dcf7

Please sign in to comment.