refactor: simplify Swift support code after review

- Move `pattern` node handling into shared extractVarName (like mut_pattern)
  instead of inline fallback in type-env — benefits all callers
- Remove non-null assertion (!) on firstNamedChild — defensive null check
- Avoid 100K wrapper object allocation: addSwiftImplicitImports now accepts
  string[] directly, eliminating allFileList.map(p => ({ path: p }))
- Remove duplicate comment block in for-loop test

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
marxo126 2026-03-23 11:44:11 +01:00
parent 956dfd0bb4
commit 1f4c4e77ab
No known key found for this signature in database
4 changed files with 13 additions and 14 deletions

View file

@ -143,15 +143,17 @@ function groupSwiftFilesByTarget(
* Swift has no file-level imports — all files in a module see each other.
*/
function addSwiftImplicitImports(
files: { path: string }[],
files: string[] | { path: string }[],
swiftPackageConfig: SwiftPackageConfig | null,
importMap: Map<string, Set<string>>,
addImportEdge: (src: string, target: string) => void,
logSuffix = '',
): void {
const swiftFiles = files
.filter(f => getLanguageFromFilename(f.path) === SupportedLanguages.Swift)
.map(f => f.path);
const paths = typeof files[0] === 'string'
? files as string[]
: (files as { path: string }[]).map(f => f.path);
const swiftFiles = paths
.filter(f => getLanguageFromFilename(f) === SupportedLanguages.Swift);
if (swiftFiles.length <= 1) return;
@ -382,7 +384,7 @@ export const processImports = async (
// Tree is now owned by the LRU cache — no manual delete needed
}
addSwiftImplicitImports(allFileList.map(p => ({ path: p })), configs.swiftPackageConfig, importMap, addImportEdge);
addSwiftImplicitImports(allFileList, configs.swiftPackageConfig, importMap, addImportEdge);
if (skippedByLang && skippedByLang.size > 0) {
for (const [lang, count] of skippedByLang.entries()) {

View file

@ -872,9 +872,7 @@ export const buildTypeEnv = (
?? node.childForFieldName('left')
?? node.childForFieldName('pattern');
if (nameNode) {
// Swift: pattern node wraps a simple_identifier — unwrap it
const varName = extractVarName(nameNode)
?? (nameNode.type === 'pattern' ? extractVarName(nameNode.firstNamedChild!) ?? nameNode.text : undefined);
const varName = extractVarName(nameNode);
if (varName && !declarationTypeNodes.has(`${scope}\0${varName}`)) {
declarationTypeNodes.set(`${scope}\0${varName}`, typeNode);
}

View file

@ -317,6 +317,11 @@ export const extractVarName = (node: SyntaxNode): string | undefined => {
const inner = node.firstNamedChild;
if (inner) return extractVarName(inner);
}
// Swift: pattern node wraps a simple_identifier
if (node.type === 'pattern') {
const inner = node.firstNamedChild;
if (inner) return extractVarName(inner);
}
return undefined;
};

View file

@ -467,12 +467,6 @@ describe.skipIf(!swiftAvailable)('Swift await / try expression unwrapping', () =
});
});
// ---------------------------------------------------------------------------
// for-in loop element type inference:
// extractForLoopBinding derives element type from the iterable's declared
// type annotation (e.g., [User] → User).
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// For-in loop element type inference: extractForLoopBinding derives element
// type from the iterable's declared type annotation (e.g., [User] → User).