Skip to content

Commit 9e08670

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

File tree

8 files changed

+30
-44
lines changed

8 files changed

+30
-44
lines changed

Sources/SwiftRefactor/PackageManifest/AddPackageTarget.swift

Lines changed: 6 additions & 6 deletions
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 {
@@ -298,17 +298,17 @@ fileprivate extension PackageTarget.Dependency {
298298
/// Retrieve the name of the dependency
299299
var name: String {
300300
switch self {
301-
case .target(name: let name),
302-
.byName(name: let name),
303-
.product(name: let name, package: _):
301+
case .target(let name),
302+
.byName(let name),
303+
.product(let name, package: _):
304304
return name
305305
}
306306
}
307307
}
308308

309309
/// The array of auxiliary files that can be added by a package editing
310310
/// operation.
311-
fileprivate typealias AuxiliaryFiles = [(String, SourceFileSyntax)]
311+
private typealias AuxiliaryFiles = [(String, SourceFileSyntax)]
312312

313313
fileprivate extension AuxiliaryFiles {
314314
/// Add a source file to the list of auxiliary files.
@@ -322,7 +322,7 @@ fileprivate extension AuxiliaryFiles {
322322

323323
/// The set of dependencies we need to introduce to a newly-created macro
324324
/// target.
325-
fileprivate let macroTargetDependencies: [PackageTarget.Dependency] = [
325+
private let macroTargetDependencies: [PackageTarget.Dependency] = [
326326
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
327327
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
328328
]

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/PackageTarget.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,16 @@ extension PackageTarget: ManifestSyntaxRepresentable {
114114
extension PackageTarget.Dependency: ManifestSyntaxRepresentable {
115115
func asSyntax() -> ExprSyntax {
116116
switch self {
117-
case .byName(name: let name):
117+
case .byName(let name):
118118
return "\(literal: name)"
119119

120-
case .target(name: let name):
120+
case .target(let name):
121121
return ".target(name: \(literal: name))"
122122

123-
case .product(name: let name, package: nil):
123+
case .product(let name, package: nil):
124124
return ".product(name: \(literal: name))"
125125

126-
case .product(name: let name, package: let package):
126+
case .product(let name, let package):
127127
return ".product(name: \(literal: name), package: \(literal: package))"
128128
}
129129
}
@@ -132,10 +132,10 @@ extension PackageTarget.Dependency: ManifestSyntaxRepresentable {
132132
extension PackageTarget.PluginUsage: ManifestSyntaxRepresentable {
133133
func asSyntax() -> ExprSyntax {
134134
switch self {
135-
case .plugin(name: let name, package: nil):
135+
case .plugin(let name, package: nil):
136136
return ".plugin(name: \(literal: name))"
137137

138-
case .plugin(name: let name, package: let package):
138+
case .plugin(let name, let package):
139139
return ".plugin(name: \(literal: name), package: \(literal: package))"
140140
}
141141
}

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: 8 additions & 19 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

@@ -289,12 +278,12 @@ extension ExprSyntax {
289278
}
290279

291280
// MARK: Utilities to oeprate on arrays of array literal elements.
292-
extension Array<ArrayElementSyntax> {
281+
extension [ArrayElementSyntax] {
293282
/// Append a new argument expression.
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

@@ -317,7 +306,7 @@ extension Array<ArrayElementSyntax> {
317306

318307
// MARK: Utilities to operate on arrays of call arguments.
319308

320-
extension Array<LabeledExprSyntax> {
309+
extension [LabeledExprSyntax] {
321310
/// Append a potentially labeled argument with the argument expression.
322311
mutating func append(label: String?, expression: ExprSyntax) {
323312
// Add a comma on the prior expression, if there is one.
@@ -422,7 +411,7 @@ extension Array<LabeledExprSyntax> {
422411
}
423412

424413
// MARK: Utilities for adding arguments into calls.
425-
fileprivate class ReplacingRewriter: SyntaxRewriter {
414+
private class ReplacingRewriter: SyntaxRewriter {
426415
let childNode: Syntax
427416
let newChildNode: Syntax
428417

0 commit comments

Comments
 (0)