Skip to content

Commit

Permalink
Fix bug in import sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryu0118 committed May 24, 2023
1 parent c3c339b commit 2d8640b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 5 additions & 3 deletions Sources/SorterCore/Rewriter/ImportSortRewriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ final class ImportSortRewriter: Rewriter, RuleNameContainable {
return first.localizedStandardCompare(second) == .orderedAscending
}
.inoutEnumeratedMap { index, element in
let importDecl = imports[index].importDecl
let importCodeBlock = imports[index]
let importDecl = importCodeBlock.importDecl
element.importDecl.leadingTrivia = importDecl.leadingTrivia
element.importDecl.trailingTrivia = importDecl.trailingTrivia
element.index = importCodeBlock.index
}

let formattedItems = node
.enumerated()
.map { index, codeBlockItem in
if let formattedImport = formattedImports[safe: index] {
if let formattedImport = formattedImports.first(where: { $0.index == index }) {
return codeBlockItem.with(\.item, formattedImport.importDecl.cast(CodeBlockItemSyntax.Item.self))
} else {
return codeBlockItem
Expand All @@ -47,7 +49,7 @@ final class ImportSortRewriter: Rewriter, RuleNameContainable {

extension ImportSortRewriter {
struct ImportCodeBlock {
let index: Int
var index: Int
var importDecl: ImportDeclSyntax
}
}
6 changes: 6 additions & 0 deletions Tests/SorterTests/SorterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ final class SorterTests: XCTestCase {
import SwiftUI
import ComposableArchitecture
import CoreData
struct Hoge {
let hoge: String
}
import class UIKit.UIImage
"""
let expected =
Expand All @@ -57,6 +60,9 @@ final class SorterTests: XCTestCase {
import CoreData
import SwiftUI
import UIKit
struct Hoge {
let hoge: String
}
import class UIKit.UIImage
"""

Expand Down

0 comments on commit 2d8640b

Please sign in to comment.