Skip to content

Commit 2fde0d7

Browse files
authored
fix conditional import (#620)
1 parent dcb75bf commit 2fde0d7

3 files changed

Lines changed: 24 additions & 10 deletions

File tree

Sources/JExtractSwiftLib/SwiftTypes/SwiftSymbolTable.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,13 @@ extension SwiftSymbolTable {
176176
continue
177177
}
178178

179-
// Try to print only on main module from relation chain as it has every other module.
180-
guard
181-
!mainSymbolSourceModules.isDisjoint(with: alternativeModules.moduleNames)
182-
|| alternativeModules.isMainSourceOfSymbols
183-
else {
184-
if !alternativeModules.isMainSourceOfSymbols {
179+
// Only the main source of symbols emits the conditional import block.
180+
// Secondary modules (e.g. FoundationEssentials when Foundation is the main source)
181+
// are skipped when their main source is already present, because the main source's
182+
// block already covers the import. If no main source is present, fall back to a
183+
// plain import so the module is still imported.
184+
guard alternativeModules.isMainSourceOfSymbols else {
185+
if mainSymbolSourceModules.isDisjoint(with: alternativeModules.moduleNames) {
185186
printer.print("import \(module)")
186187
}
187188
continue

Tests/JExtractSwiftTests/Asserts/TextAssertions.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func assertOutput(
3333
detectChunkByInitialLines _detectChunkByInitialLines: Int = 4,
3434
javaClassLookupTable: [String: String] = [:],
3535
expectedChunks: [String],
36+
notExpectedChunks: [String] = [],
3637
fileID: String = #fileID,
3738
filePath: String = #filePath,
3839
line: Int = #line,
@@ -83,6 +84,15 @@ func assertOutput(
8384
}
8485
output = printer.finalize()
8586

87+
let sourceLocation = SourceLocation(fileID: fileID, filePath: filePath, line: line, column: column)
88+
for notExpectedChunk in notExpectedChunks {
89+
#expect(
90+
!output.contains(notExpectedChunk),
91+
"Output must not contain:\n\(notExpectedChunk)\n\nGot output:\n\(output)",
92+
sourceLocation: sourceLocation
93+
)
94+
}
95+
8696
let gotLines = output.split(separator: "\n").filter { l in
8797
l.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).count > 0
8898
}

Tests/JExtractSwiftTests/FoundationImportTests.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import Testing
1919
struct FoundationImportTests {
2020
@Test("Import Foundation", arguments: [JExtractGenerationMode.jni, JExtractGenerationMode.ffm])
2121
func import_foundation(mode: JExtractGenerationMode) throws {
22-
2322
try assertOutput(
2423
input: "import Foundation",
2524
mode,
@@ -33,7 +32,6 @@ struct FoundationImportTests {
3332

3433
@Test("Import FoundationEssentials", arguments: [JExtractGenerationMode.jni, JExtractGenerationMode.ffm])
3534
func import_foundationEssentials(mode: JExtractGenerationMode) throws {
36-
3735
try assertOutput(
3836
input: "import FoundationEssentials",
3937
mode,
@@ -47,8 +45,7 @@ struct FoundationImportTests {
4745

4846
@Test("Import conditional foundation", arguments: [JExtractGenerationMode.jni, JExtractGenerationMode.ffm])
4947
func import_conditionalFoundation(mode: JExtractGenerationMode) throws {
50-
let ifConfigImport =
51-
"""
48+
let ifConfigImport = """
5249
#if canImport(FoundationEssentials)
5350
import FoundationEssentials
5451
#else
@@ -63,6 +60,12 @@ struct FoundationImportTests {
6360
detectChunkByInitialLines: 1,
6461
expectedChunks: [
6562
ifConfigImport
63+
],
64+
notExpectedChunks: [
65+
"""
66+
#if canImport(Foundation)
67+
import Foundation
68+
"""
6669
]
6770
)
6871
}

0 commit comments

Comments
 (0)