Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions Sources/CodableToTypeScript/Generator/PackageGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,17 @@ public final class PackageGenerator {
)

var symbolToSource: [String: SourceFile] = [:]
var convertedSources: [SourceFile: TSSourceFile] = [:]

// collect symbols included in for each swift source file
for module in context.modules.filter({ $0 !== context.swiftModule }) {
for source in module.sources {
for type in source.types {
guard
let typeConverter = try? codeGenerator.converter(for: type.declaredInterfaceType),
let declaredNames = try? typeConverter.decls().compactMap(\.declaredName)
else {
continue
}
for declaredName in declaredNames {
symbolToSource[declaredName] = source
}
guard let tsSource = try? codeGenerator.convert(source: source) else {
continue
}
convertedSources[source] = tsSource
for declaredName in tsSource.memberDeclaredNames {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確かに。
自動import文生成の仕組みはこんな感じでした。

symbolToSource[declaredName] = source
}
}
}
Expand All @@ -89,7 +86,7 @@ public final class PackageGenerator {
while let source = targetSources.popLast() {
generatedSources.insert(source)
collect(at: source.file.lastPathComponent) {
let tsSource = try codeGenerator.convert(source: source)
let tsSource = try convertedSources[source] ?? (codeGenerator.convert(source: source))

let entry = PackageEntry(
file: try tsPath(module: source.module, file: source.file),
Expand Down
42 changes: 42 additions & 0 deletions Tests/CodableToTypeScriptTests/PackageGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,46 @@ final class PackageGeneratorTests: XCTestCase {
return element.asDecl?.asType?.name == "D"
}))
}

func testCustomMappedTypeIngored() throws {
let context = Context()
let module = Reader(context: context).read(source: """
struct AbsoluteURL: Codable, RawRepresentable {
var rawValue: String

func encode(to encoder: any Encoder) throws {
var c = encoder.singleValueContainer()
try c.encode(rawValue)
}

init(from decoder: any Decoder) throws {
let c = try decoder.singleValueContainer()
self.rawValue = try c.decode(RawValue.self)
}
}
""", file: URL(fileURLWithPath: "A.swift")).module

let typeMap = TypeMap(mapFunction: { stype in
if stype.description == "AbsoluteURL" {
return .identity(name: "string")
}
return nil
})

let generator = PackageGenerator(
context: context,
typeConverterProvider: TypeConverterProvider(typeMap: typeMap),
symbols: SymbolTable(),
importFileExtension: .js,
outputDirectory: URL(fileURLWithPath: "/dev/null", isDirectory: true)
)
let result = try generator.generate(modules: [module])
XCTAssertEqual(result.entries.count, 1) // helper library only
let hasTSAbsoluteURL = result.entries.contains(where: { entry in
entry.source.memberDeclaredNames.contains(where: { name in
return name == "AbsoluteURL"
})
})
XCTAssertFalse(hasTSAbsoluteURL)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど。TypeMapで型が消える。

}
}