Skip to content

Commit a1ac7ca

Browse files
committed
[PackageManifest] Address NFC review feedback
Addresses minor comments like indentation, naming, and code style.
1 parent e6bd577 commit a1ac7ca

File tree

6 files changed

+32
-44
lines changed

6 files changed

+32
-44
lines changed

Sources/SwiftRefactor/PackageManifest/AddPackageDependency.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public struct AddPackageDependency: ManifestEditRefactoringProvider {
6666
) throws -> FunctionCallExprSyntax {
6767
try packageCall.appendingToArrayArgument(
6868
label: "dependencies",
69-
trailingLabels: Self.argumentLabelsAfterDependencies,
69+
labelsAfter: Self.argumentLabelsAfterDependencies,
7070
newElement: dependency.asSyntax()
7171
)
7272
}

Sources/SwiftRefactor/PackageManifest/AddPackageTarget.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ public struct AddPackageTarget: ManifestEditRefactoringProvider {
9797
target.dependencies.append(contentsOf: macroTargetDependencies)
9898

9999
default:
100-
break;
100+
break
101101
}
102102

103103
var newPackageCall = try packageCall.appendingToArrayArgument(
104104
label: "targets",
105-
trailingLabels: Self.argumentLabelsAfterTargets,
105+
labelsAfter: Self.argumentLabelsAfterTargets,
106106
newElement: target.asSyntax()
107107
)
108108

@@ -172,7 +172,7 @@ public struct AddPackageTarget: ManifestEditRefactoringProvider {
172172
}
173173
}
174174

175-
default: break;
175+
default: break
176176
}
177177

178178
return PackageEdit(

Sources/SwiftRefactor/PackageManifest/AddPluginUsage.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public struct AddPluginUsage: ManifestEditRefactoringProvider {
5252

5353
let newTargetCall = try targetCall.appendingToArrayArgument(
5454
label: "plugins",
55-
trailingLabels: Self.argumentLabelsAfterPluginUsages,
55+
labelsAfter: Self.argumentLabelsAfterPluginUsages,
5656
newElement: pluginUsage.asSyntax()
5757
)
5858

Sources/SwiftRefactor/PackageManifest/AddProduct.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public struct AddProduct: ManifestEditRefactoringProvider {
5050

5151
let newPackageCall = try packageCall.appendingToArrayArgument(
5252
label: "products",
53-
trailingLabels: argumentLabelsAfterProducts,
53+
labelsAfter: argumentLabelsAfterProducts,
5454
newElement: product.asSyntax()
5555
)
5656

Sources/SwiftRefactor/PackageManifest/AddTargetDependency.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public struct AddTargetDependency: ManifestEditRefactoringProvider {
8383
) throws -> FunctionCallExprSyntax {
8484
try targetCall.appendingToArrayArgument(
8585
label: "dependencies",
86-
trailingLabels: Self.argumentLabelsAfterDependencies,
86+
labelsAfter: Self.argumentLabelsAfterDependencies,
8787
newElement: dependency.asSyntax()
8888
)
8989
}

Sources/SwiftRefactor/PackageManifest/SyntaxEditUtils.swift

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extension Trivia {
2626

2727
/// Produce trivia from the last newline to the end, dropping anything
2828
/// prior to that.
29-
func onlyLastLine() -> Trivia {
29+
var onlyLastLine: Trivia {
3030
guard let lastNewline = pieces.lastIndex(where: { $0.isNewline }) else {
3131
return self
3232
}
@@ -122,15 +122,13 @@ extension LabeledExprListSyntax {
122122
/// context.
123123
func insertingArgument(
124124
at position: SyntaxChildrenIndex,
125-
generator: (Trivia, TokenSyntax?) -> LabeledExprSyntax
125+
generator: (_ leadingTrivia: Trivia, _ trailingComma: TokenSyntax?) -> LabeledExprSyntax
126126
) -> LabeledExprListSyntax {
127127
// Turn the arguments into an array so we can manipulate them.
128128
var arguments = Array(self)
129129

130130
let positionIdx = distance(from: startIndex, to: position)
131131

132-
let commaToken = TokenSyntax.commaToken()
133-
134132
// Figure out leading trivia and adjust the prior argument (if there is
135133
// one) by adding a comma, if necessary.
136134
let leadingTrivia: Trivia
@@ -143,7 +141,7 @@ extension LabeledExprListSyntax {
143141

144142
// If the prior argument is missing a trailing comma, add one.
145143
if priorArgument.trailingComma == nil {
146-
arguments[positionIdx - 1].trailingComma = commaToken
144+
arguments[positionIdx - 1].trailingComma = .commaToken()
147145
}
148146
} else if positionIdx + 1 < count {
149147
leadingTrivia = arguments[positionIdx + 1].leadingTrivia
@@ -154,7 +152,7 @@ extension LabeledExprListSyntax {
154152
// Determine whether we need a trailing comma on this argument.
155153
let trailingComma: TokenSyntax?
156154
if position < endIndex {
157-
trailingComma = commaToken
155+
trailingComma = .commaToken()
158156
} else {
159157
trailingComma = nil
160158
}
@@ -194,9 +192,7 @@ extension FunctionCallExprSyntax {
194192
return false
195193
}
196194

197-
guard let stringLiteral = nameArgument.expression.as(StringLiteralExprSyntax.self),
198-
let literalValue = stringLiteral.representedLiteralValue
199-
else {
195+
guard let literalValue = nameArgument.expression.as(StringLiteralExprSyntax.self)?.representedLiteralValue else {
200196
return false
201197
}
202198

@@ -220,24 +216,21 @@ extension ArrayExprSyntax {
220216
) -> ArrayExprSyntax {
221217
var elements = self.elements
222218

223-
let commaToken = TokenSyntax.commaToken()
224-
225219
// If there are already elements, tack it on.
226220
let leadingTrivia: Trivia
227221
let trailingTrivia: Trivia
228222
let leftSquareTrailingTrivia: Trivia
229223
if let last = elements.last {
230224
// The leading trivia of the new element should match that of the
231225
// last element.
232-
leadingTrivia = last.leadingTrivia.onlyLastLine()
226+
leadingTrivia = last.leadingTrivia.onlyLastLine
233227

234228
// Add a trailing comma to the last element if it isn't already
235229
// there.
236230
if last.trailingComma == nil {
237231
var newElements = Array(elements)
238-
newElements[newElements.count - 1].trailingComma = commaToken
239-
newElements[newElements.count - 1].expression.trailingTrivia =
240-
Trivia()
232+
newElements[newElements.count - 1].trailingComma = .commaToken()
233+
newElements[newElements.count - 1].expression.trailingTrivia = Trivia()
241234
newElements[newElements.count - 1].trailingTrivia = last.trailingTrivia
242235
elements = ArrayElementListSyntax(newElements)
243236
}
@@ -257,7 +250,7 @@ extension ArrayExprSyntax {
257250
elements.append(
258251
ArrayElementSyntax(
259252
expression: element.with(\.leadingTrivia, leadingTrivia),
260-
trailingComma: commaToken.with(\.trailingTrivia, trailingTrivia)
253+
trailingComma: .commaToken().with(\.trailingTrivia, trailingTrivia)
261254
)
262255
)
263256

@@ -351,17 +344,16 @@ extension Array<LabeledExprSyntax> {
351344
/// Append a potentially labeled argument with a string literal, but only
352345
/// when the string literal is not nil.
353346
mutating func appendIf(label: String?, stringLiteral: String?) {
354-
if let stringLiteral {
355-
append(label: label, stringLiteral: stringLiteral)
356-
}
347+
guard let stringLiteral else { return }
348+
append(label: label, stringLiteral: stringLiteral)
357349
}
358350

359351
/// Append an array literal containing elements that can be rendered
360352
/// into expression syntax nodes.
361-
mutating func append<T>(
353+
mutating func append<T: ManifestSyntaxRepresentable>(
362354
label: String?,
363355
arrayLiteral: [T]
364-
) where T: ManifestSyntaxRepresentable, T.PreferredSyntax == ExprSyntax {
356+
) where T.PreferredSyntax == ExprSyntax {
365357
var elements: [ArrayElementSyntax] = []
366358
for element in arrayLiteral {
367359
elements.append(expression: element.asSyntax())
@@ -402,20 +394,20 @@ extension Array<LabeledExprSyntax> {
402394

403395
/// Append an array literal containing elements that can be rendered
404396
/// into expression syntax nodes.
405-
mutating func appendIf<T>(
397+
mutating func appendIf<T: ManifestSyntaxRepresentable>(
406398
label: String?,
407399
arrayLiteral: [T]?
408-
) where T: ManifestSyntaxRepresentable, T.PreferredSyntax == ExprSyntax {
400+
) where T.PreferredSyntax == ExprSyntax {
409401
guard let arrayLiteral else { return }
410402
append(label: label, arrayLiteral: arrayLiteral)
411403
}
412404

413405
/// Append an array literal containing elements that can be rendered
414406
/// into expression syntax nodes, but only if it's not empty.
415-
mutating func appendIfNonEmpty<T>(
407+
mutating func appendIfNonEmpty<T: ManifestSyntaxRepresentable>(
416408
label: String?,
417409
arrayLiteral: [T]
418-
) where T: ManifestSyntaxRepresentable, T.PreferredSyntax == ExprSyntax {
410+
) where T.PreferredSyntax == ExprSyntax {
419411
if arrayLiteral.isEmpty { return }
420412

421413
append(label: label, arrayLiteral: arrayLiteral)
@@ -453,22 +445,22 @@ fileprivate extension SyntaxProtocol {
453445
}
454446

455447
extension FunctionCallExprSyntax {
456-
/// Produce source edits that will add the given new element to the
448+
/// Perform source edits that will add the given new element to the
457449
/// array for an argument with the given label (if there is one), or
458450
/// introduce a new argument with an array literal containing only the
459451
/// new element.
460452
///
461453
/// - Parameters:
462454
/// - label: The argument label for the argument whose array will be
463455
/// added or modified.
464-
/// - trailingLabels: The argument labels that could follow the label,
456+
/// - labelsAfter: The argument labels that could follow the label,
465457
/// which helps determine where the argument should be inserted if
466458
/// it doesn't exist yet.
467459
/// - newElement: The new element.
468460
/// - Returns: the function call after making this change.
469461
func appendingToArrayArgument(
470462
label: String,
471-
trailingLabels: Set<String>,
463+
labelsAfter: Set<String>,
472464
newElement: ExprSyntax
473465
) throws -> FunctionCallExprSyntax {
474466
// If there is already an argument with this name, append to the array
@@ -483,7 +475,7 @@ extension FunctionCallExprSyntax {
483475

484476
// Format the element appropriately for the context.
485477
let indentation = Trivia(
486-
pieces: arg.leadingTrivia.filter { $0.isSpaceOrTab }
478+
pieces: arg.leadingTrivia.filter(\.isSpaceOrTab)
487479
)
488480
let format = BasicFormat(
489481
indentationWidth: [defaultIndent],
@@ -504,13 +496,13 @@ extension FunctionCallExprSyntax {
504496

505497
// Insert the new argument at the appropriate place in the call.
506498
let insertionPos = arguments.findArgumentInsertionPosition(
507-
labelsAfter: trailingLabels
499+
labelsAfter: labelsAfter
508500
)
509501
let newArguments = arguments.insertingArgument(
510502
at: insertionPos
511503
) { (leadingTrivia, trailingComma) in
512504
// Format the element appropriately for the context.
513-
let indentation = Trivia(pieces: leadingTrivia.filter { $0.isSpaceOrTab })
505+
let indentation = Trivia(pieces: leadingTrivia.filter(\.isSpaceOrTab))
514506
let format = BasicFormat(
515507
indentationWidth: [defaultIndent],
516508
initialIndentation: indentation.appending(defaultIndent)
@@ -521,9 +513,7 @@ extension FunctionCallExprSyntax {
521513
// Form the array.
522514
let newArgument = ArrayExprSyntax(
523515
leadingTrivia: .space,
524-
leftSquare: .leftSquareToken(
525-
trailingTrivia: .newline
526-
),
516+
leftSquare: .leftSquareToken(trailingTrivia: .newline),
527517
elements: ArrayElementListSyntax(
528518
[
529519
ArrayElementSyntax(
@@ -532,9 +522,7 @@ extension FunctionCallExprSyntax {
532522
)
533523
]
534524
),
535-
rightSquare: .rightSquareToken(
536-
leadingTrivia: leadingTrivia
537-
)
525+
rightSquare: .rightSquareToken(leadingTrivia: leadingTrivia)
538526
)
539527

540528
// Create the labeled argument for the array.

0 commit comments

Comments
 (0)