Skip to content

シンボル収集の意味的なズレの修正#124

Merged
sidepelican merged 2 commits intomainfrom
fix_semantic_error
Apr 26, 2024
Merged

シンボル収集の意味的なズレの修正#124
sidepelican merged 2 commits intomainfrom
fix_semantic_error

Conversation

@sidepelican
Copy link
Collaborator

@sidepelican sidepelican commented Apr 26, 2024

機能的な変更ではなく、リファクタリングの意味合いの修正です。

#118 で導入されたPackageGeneratorのシンボル収集プロセスについてです。

ソースコードの変換処理はファイル単位で行われており、ファイル単位での成功と失敗があります。
ここでシンボル収集する際に、変換に失敗するファイルに含まれたシンボルは、結果的には参照できないので収集する必要がありません。

既存の実装は、ファイル内に含まれるSwift型1つずつについてシンボル収集するロジックになっていました。
これを、実際の変換処理に合わせてファイル単位の処理に書き換えます。
中の処理を展開するとやっていることは全く同じなので、機能的な変化はありません。

codeGenerator.convertの呼び出しが2箇所になって、同じ処理をしていることがわかりやすくなりました。


ついでにこの問題に気づくきっかけになったテストを追加しています。

@sidepelican
Copy link
Collaborator Author

sidepelican commented Apr 26, 2024

同じ処理をしていたことがわかったので、次のようなキャッシュ層をいれると2倍くらいの速さになった
→ というわけでついでにこのキャッシュも入れました

diff --git a/Sources/CodableToTypeScript/Generator/PackageGenerator.swift b/Sources/CodableToTypeScript/Generator/Packa>
index a81d197..f0d4c2b 100644
--- a/Sources/CodableToTypeScript/Generator/PackageGenerator.swift
+++ b/Sources/CodableToTypeScript/Generator/PackageGenerator.swift
@@ -53,6 +53,7 @@ public final class PackageGenerator {
         )
 
         var symbolToSource: [String: SourceFile] = [:]
+        var convertedTSSources: [SourceFile: TSSourceFile] = [:]
 
         // collect symbols included in for each swift source file
         for module in context.modules.filter({ $0 !== context.swiftModule }) {
@@ -60,6 +61,7 @@ public final class PackageGenerator {
                 guard let tsSource = try? codeGenerator.convert(source: source) else {
                     continue
                 }
+                convertedTSSources[source] = tsSource
                 for declaredName in tsSource.memberDeclaredNames {
                     symbolToSource[declaredName] = source
                 }
@@ -84,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 convertedTSSources[source] ?? (codeGenerator.convert(source: source))
 
                     let entry = PackageEntry(
                         file: try tsPath(module: source.module, file: source.file),
~

Copy link
Owner

@omochi omochi left a comment

Choose a reason for hiding this comment

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

なるほど。
結果的にcodeConverterの上層APIだけで済むようになってるのも良いですね。

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文生成の仕組みはこんな感じでした。

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で型が消える。

@sidepelican sidepelican merged commit 990a145 into main Apr 26, 2024
@sidepelican sidepelican deleted the fix_semantic_error branch April 26, 2024 07:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants