Skip to content

Commit 94827bf

Browse files
authored
Remove internal using directives if no symbols from them are used (#120)
1 parent 5e53889 commit 94827bf

File tree

2 files changed

+32
-3
lines changed
  • packages/cli/src

2 files changed

+32
-3
lines changed

packages/cli/src/languagePlugins/csharp/invocationResolver/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import {
88
import { csharpParser } from "../../../helpers/treeSitter/parsers.js";
99
import {
1010
CSharpUsingResolver,
11+
ExternalSymbol,
1112
ResolvedImports,
13+
UsingDirective,
1214
} from "../usingResolver/index.js";
1315
import { CSharpProjectMapper } from "../projectMapper/index.js";
1416
import {
@@ -464,4 +466,25 @@ export class CSharpInvocationResolver {
464466
const invocations = this.getInvocationsFromFile(filepath);
465467
return invocations.resolvedSymbols.some((inv) => inv.name === symbol.name);
466468
}
469+
470+
/**
471+
* Checks if a using directive is useful in a file.
472+
* @param invocations - The invocations in the file.
473+
* @param using - The using directive to check for.
474+
* @returns True if the using directive is useful, false otherwise.
475+
*/
476+
public isUsingUseful(
477+
invocations: Invocations,
478+
usingD: UsingDirective,
479+
): boolean {
480+
const usedNamespace = this.usingResolver.resolveUsingDirective(usingD);
481+
if (usedNamespace instanceof ExternalSymbol) return true;
482+
return invocations.resolvedSymbols.some(
483+
(inv) =>
484+
(usedNamespace.namespace &&
485+
inv.namespace ===
486+
this.nsMapper.getFullNSName(usedNamespace.namespace)) ||
487+
(usedNamespace.symbol && inv.name === usedNamespace.symbol.name),
488+
);
489+
}
467490
}

packages/cli/src/symbolExtractor/csharp/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
InternalSymbol,
1818
ExternalSymbol,
1919
} from "../../languagePlugins/csharp/usingResolver/index.js";
20+
import { CSharpInvocationResolver } from "../../languagePlugins/csharp/invocationResolver/index.js";
2021

2122
/**
2223
* Extracts C# symbols from the given files.
@@ -86,20 +87,25 @@ export function extractCSharpSymbols(
8687
}
8788
}
8889
}
89-
// For each extracted file, check if the using directives are still valid
90+
// For each extracted file, check if the using directives are still valid and useful
9091
const nsMapper = new CSharpNamespaceMapper(parsedFiles);
9192
const pjMapper = new CSharpProjectMapper(csprojFiles);
9293
const usingResolver = new CSharpUsingResolver(nsMapper, pjMapper);
94+
const invocationResolver = new CSharpInvocationResolver(nsMapper, pjMapper);
9395
for (const extractedFile of extractedFiles) {
9496
const imports = extractedFile.imports;
97+
const invocations = invocationResolver.getInvocationsFromFile(
98+
extractedFile.name,
99+
);
95100
for (const importDirective of imports) {
96101
const resolvedInNewFile =
97102
usingResolver.resolveUsingDirective(importDirective);
98103
const resolvedInOldFile =
99104
extractor.usingResolver.resolveUsingDirective(importDirective);
100105
if (
101-
resolvedInNewFile instanceof ExternalSymbol &&
102-
resolvedInOldFile instanceof InternalSymbol
106+
(resolvedInNewFile instanceof ExternalSymbol &&
107+
resolvedInOldFile instanceof InternalSymbol) ||
108+
!invocationResolver.isUsingUseful(invocations, importDirective)
103109
) {
104110
extractedFile.imports = extractedFile.imports.filter(
105111
(imp) => imp !== importDirective,

0 commit comments

Comments
 (0)