Skip to content

Commit d2ad8c0

Browse files
committed
[SwiftRefactor] PackageManifest: Address stylistic review feedback
1 parent 6a98663 commit d2ad8c0

File tree

7 files changed

+16
-30
lines changed

7 files changed

+16
-30
lines changed

Sources/SwiftRefactor/PackageManifest/AddPackageTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public struct AddPackageTarget: ManifestEditRefactoringProvider {
190190
}
191191

192192
let importDecls = importModuleNames.lazy.sorted().map { name in
193-
DeclSyntax("import \(raw: name)").with(\.trailingTrivia, .newline)
193+
DeclSyntax("import \(raw: name)\n")
194194
}
195195

196196
let imports = CodeBlockItemListSyntax {

Sources/SwiftRefactor/PackageManifest/ManifestEditRefactoringProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ where Self.Input == SourceFileSyntax {
1818
static func manifestRefactor(syntax: SourceFileSyntax, in context: Context) throws -> PackageEdit
1919
}
2020

21-
extension EditRefactoringProvider where Self: ManifestEditRefactoringProvider {
21+
extension ManifestEditRefactoringProvider {
2222
public static func textRefactor(syntax: Input, in context: Context) -> [SourceEdit] {
2323
return (try? manifestRefactor(syntax: syntax, in: context).manifestEdits) ?? []
2424
}

Sources/SwiftRefactor/PackageManifest/PackageDependency.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public enum PackageDependency: Sendable {
2222
case registry(Registry)
2323

2424
public struct FileSystem: Sendable {
25-
public let nameForTargetDependencyResolutionOnly: String?
2625
public let path: String
2726
}
2827

@@ -75,8 +74,6 @@ extension PackageDependency.FileSystem: ManifestSyntaxRepresentable {
7574

7675
extension PackageDependency.SourceControl: ManifestSyntaxRepresentable {
7776
func asSyntax() -> ExprSyntax {
78-
// TODO: Not handling identity, nameForTargetDependencyResolutionOnly,
79-
// or productFilter yet.
8077
".package(url: \(literal: location.description), \(requirement.asSyntax()))"
8178
}
8279
}
@@ -104,7 +101,7 @@ extension PackageDependency.SourceControl.Requirement: ManifestSyntaxRepresentab
104101

105102
case .range(let lowerBound, let upperBound):
106103
return LabeledExprSyntax(
107-
expression: "\(lowerBound.asSyntax())..<\(upperBound.asSyntax())" as ExprSyntax
104+
expression: "\(literal: lowerBound)..<\(literal: upperBound)" as ExprSyntax
108105
)
109106

110107
case .revision(let revision):

Sources/SwiftRefactor/PackageManifest/PackageEdit.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ public struct PackageEdit {
1919
public var manifestEdits: [SourceEdit] = []
2020

2121
/// Auxiliary files to write.
22-
public var auxiliaryFiles: [(String, SourceFileSyntax)] = []
22+
public var auxiliaryFiles: [(relativePath: String, contents: SourceFileSyntax)] = []
2323
}

Sources/SwiftRefactor/PackageManifest/ProductDescription.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ extension ProductDescription: ManifestSyntaxRepresentable {
7676
private var functionName: String {
7777
switch type {
7878
case .executable: return "executable"
79-
case .library(_): return "library"
79+
case .library: return "library"
8080
case .macro: return "macro"
8181
case .plugin: return "plugin"
8282
case .snippet: return "snippet"

Sources/SwiftRefactor/PackageManifest/StringUtils.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extension String {
1818
func mangledToC99ExtendedIdentifier() -> String {
1919
// Map invalid C99-invalid Unicode scalars to a replacement character.
2020
let replacementUnichar: UnicodeScalar = "_"
21-
var mangledUnichars: [UnicodeScalar] = self.unicodeScalars.map({
21+
var mangledUnichars: [UnicodeScalar] = self.unicodeScalars.map {
2222
switch $0.value {
2323
case // A-Z
2424
0x0041...0x005A,
@@ -194,10 +194,10 @@ extension String {
194194
default:
195195
return replacementUnichar
196196
}
197-
})
197+
}
198198

199199
// Apply further restrictions to the prefix.
200-
loop: for (idx, c) in mangledUnichars.enumerated() {
200+
LOOP: for (idx, c) in mangledUnichars.enumerated() {
201201
switch c.value {
202202
case // 0-9
203203
0x0030...0x0039,
@@ -208,16 +208,16 @@ extension String {
208208
0x0CE6...0x0CEF, 0x0D66...0x0D6F, 0x0E50...0x0E59,
209209
0x0ED0...0x0ED9, 0x0F20...0x0F33:
210210
mangledUnichars[idx] = replacementUnichar
211-
break loop
211+
break LOOP
212212
default:
213-
break loop
213+
break LOOP
214214
}
215215
}
216216

217217
// Combine the characters as a string again and return it.
218218
// FIXME: We should only construct a new string if anything changed.
219219
// FIXME: There doesn't seem to be a way to create a string from an
220220
// array of Unicode scalars; but there must be a better way.
221-
return mangledUnichars.reduce("") { $0 + String($1) }
221+
return String(decoding: mangledUnichars.flatMap { $0.utf8 }, as: UTF8.self)
222222
}
223223
}

Sources/SwiftRefactor/PackageManifest/SyntaxEditUtils.swift

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,12 @@ extension Trivia {
2323
var hasNewlines: Bool {
2424
contains(where: \.isNewline)
2525
}
26-
27-
/// Produce trivia from the last newline to the end, dropping anything
28-
/// prior to that.
29-
var onlyLastLine: Trivia {
30-
guard let lastNewline = pieces.lastIndex(where: { $0.isNewline }) else {
31-
return self
32-
}
33-
34-
return Trivia(pieces: pieces[lastNewline...])
35-
}
3626
}
3727

3828
/// Syntax walker to find the first occurrence of a given node kind that
3929
/// matches a specific predicate.
4030
private class FirstNodeFinder<Node: SyntaxProtocol>: SyntaxAnyVisitor {
41-
var predicate: (Node) -> Bool
31+
let predicate: (Node) -> Bool
4232
var found: Node? = nil
4333

4434
init(predicate: @escaping (Node) -> Bool) {
@@ -100,7 +90,7 @@ extension LabeledExprListSyntax {
10090
/// Find the index at which the one would insert a new argument given
10191
/// the set of argument labels that could come after the argument we
10292
/// want to insert.
103-
func findArgumentInsertionPosition(
93+
fileprivate func findArgumentInsertionPosition(
10494
labelsAfter: Set<String>
10595
) -> SyntaxChildrenIndex {
10696
firstIndex {
@@ -120,7 +110,7 @@ extension LabeledExprListSyntax {
120110
/// created by the `generator` function, which is provided with leading
121111
/// trivia and trailing comma it should use to match the surrounding
122112
/// context.
123-
func insertingArgument(
113+
fileprivate func insertingArgument(
124114
at position: SyntaxChildrenIndex,
125115
generator: (_ leadingTrivia: Trivia, _ trailingComma: TokenSyntax?) -> LabeledExprSyntax
126116
) -> LabeledExprListSyntax {
@@ -236,9 +226,8 @@ extension ArrayExprSyntax {
236226
// there.
237227
if last.trailingComma == nil {
238228
var newElements = Array(elements)
239-
newElements[newElements.count - 1].trailingComma = .commaToken()
229+
newElements[newElements.count - 1].trailingComma = .commaToken(trailingTrivia: last.expression.trailingTrivia)
240230
newElements[newElements.count - 1].expression.trailingTrivia = Trivia()
241-
newElements[newElements.count - 1].trailingTrivia = last.trailingTrivia
242231
elements = ArrayElementListSyntax(newElements)
243232
}
244233

@@ -294,7 +283,7 @@ extension Array<ArrayElementSyntax> {
294283
mutating func append(expression: ExprSyntax) {
295284
// Add a comma on the prior expression, if there is one.
296285
let leadingTrivia: Trivia?
297-
if count > 0 {
286+
if !isEmpty {
298287
self[count - 1].trailingComma = TokenSyntax.commaToken()
299288
leadingTrivia = .newline
300289

0 commit comments

Comments
 (0)