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
4 changes: 2 additions & 2 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 28 additions & 17 deletions Sources/CodableToTypeScript/Generator/PackageGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ public final class PackageGenerator {

try withErrorCollector { collect in
while let source = targetSources.popLast() {
generatedSources.insert(source)
guard generatedSources.insert(source).inserted else {
continue
}
Comment on lines +90 to +92
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

本質的な変更はここだけです


collect(at: source.file.lastPathComponent) {
let tsSource = try convertedSources[source] ?? (codeGenerator.convert(source: source))

Expand All @@ -96,23 +99,25 @@ public final class PackageGenerator {
source: tsSource
)
try didConvertSource?(source, entry)
if entry.isEmpty {
return
}

generatedEntries.append(.init(
file: entry.file,
source: entry.source
))

if !entry.source.elements.isEmpty {
generatedEntries.append(.init(
file: entry.file,
source: entry.source
))

let imports = try tsSource.buildAutoImportDecls(
from: entry.file,
symbolTable: allSymbols,
fileExtension: importFileExtension
)
tsSource.replaceImportDecls(imports)
for importedDecl in imports.flatMap(\.names) {
if let source = symbolToSource[importedDecl], !generatedSources.contains(source) {
targetSources.append(source)
}
let imports = try tsSource.buildAutoImportDecls(
from: entry.file,
symbolTable: allSymbols,
fileExtension: importFileExtension
)
tsSource.replaceImportDecls(imports)
for importedSymbolName in imports.flatMap(\.names) {
// Add a file that is used but not included in the generation target
if let source = symbolToSource[importedSymbolName], !generatedSources.contains(source) {
targetSources.append(source)
}
}
}
Expand Down Expand Up @@ -166,3 +171,9 @@ public final class PackageGenerator {
}
}
#endif

extension PackageEntry {
fileprivate var isEmpty: Bool {
source.elements.isEmpty
}
}
40 changes: 40 additions & 0 deletions Tests/CodableToTypeScriptTests/PackageGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,44 @@ final class PackageGeneratorTests: XCTestCase {
})
XCTAssertFalse(hasTSAbsoluteURL)
}

func testGenerationsNotDuplicates() throws {
let context = Context()

let aModule = Reader(
context: context,
module: context.getOrCreateModule(name: "A")
).read(source: """
struct A: Codable {}
""", file: URL(fileURLWithPath: "A.swift")).module

let bModule = Reader(
context: context,
module: context.getOrCreateModule(name: "B")
).read(source: """
import A

struct B: Codable {
var a: A
}
""", file: URL(fileURLWithPath: "B.swift")).module

let generator = PackageGenerator(
context: context,
symbols: SymbolTable(),
importFileExtension: .js,
outputDirectory: URL(fileURLWithPath: "/dev/null", isDirectory: true)
)

var convertedEntries: [PackageEntry] = []
generator.didConvertSource = { (source, entry) in
convertedEntries.append(entry)
}
_ = try generator.generate(modules: [aModule, bModule]) // the order is important!

XCTAssertEqual(convertedEntries.map(\.file.lastPathComponent), [
"B.ts",
"A.ts",
])
}
}
Loading